How to draw or print theano graphs?

This recipe helps you to draw or print theano graphs.

Recipe Objective - How to draw or print theano graphs?

Theano provides the functions theano.printing.pprint() and theano.printing.debugprint() to print a graph to the terminal before or after compilation.

For more related projects -

/projects/data-science-projects/neural-network-projects
/projects/data-science-projects/tensorflow-projects

Example -

Let's try to implement logistic regression:

# Importing libraries
import numpy
import theano
from theano import tensor
random = numpy.random

# Training data
N = 500
feats = 800
data = (random.randn(N, feats).astype(theano.config.floatX), random.randint(size=N,low=0, high=2).astype(theano.config.floatX))
training_steps = 20000

# Creating variables
x = tensor.matrix("x")
y = tensor.vector("y")
w = theano.shared(random.randn(feats).astype(theano.config.floatX), name="w")
b = theano.shared(numpy.asarray(0., dtype=theano.config.floatX), name="b")
x_test_value = data[0]
y_test_value = data[1]

# Creating Theano expression graph
prob_1 = 1 / (1 + tensor.exp(-tensor.dot(x, w)-b)) # Probability of having a one

prediction = prob_1 > 0.5 # The prediction that is done: 0 or 1

# Compute gradients
cross_entropy = -y*tensor.log(prob_1) - (1-y)*tensor.log(1-prob_1) # Cross-entropy

cost = cross_entropy.mean() + 0.01*(w**2).sum() # Cost

gw,gb = tensor.grad(cost, [w,b])

# Training and prediction function
train_model = theano.function(inputs=[x,y], outputs=[prediction, cross_entropy], updates=[[w, w-0.01*gw], [b, b-0.01*gb]], name = "train_model")

predict_model = theano.function(inputs=[x], outputs=prediction, name = "predict_model")
# Pretty printing
theano.printing.pprint(prediction)

Output -
'gt((TensorConstant{1} / (TensorConstant{1} + exp(((-(x \\dot w)) - b)))), TensorConstant{0.5})'

# Debug Print (The pre-compilation graph)
theano.printing.debugprint(prediction)

Output -
Elemwise{gt,no_inplace} [id A] ''   
 |Elemwise{true_div,no_inplace} [id B] ''   
 | |InplaceDimShuffle{x} [id C] ''   
 | | |TensorConstant{1} [id D]
 | |Elemwise{add,no_inplace} [id E] ''   
 |   |InplaceDimShuffle{x} [id F] ''   
 |   | |TensorConstant{1} [id G]
 |   |Elemwise{exp,no_inplace} [id H] ''   
 |     |Elemwise{sub,no_inplace} [id I] ''   
 |       |Elemwise{neg,no_inplace} [id J] ''   
 |       | |dot [id K] ''   
 |       |   |x [id L]
 |       |   |w [id M]
 |       |InplaceDimShuffle{x} [id N] ''   
 |         |b [id O]
 |InplaceDimShuffle{x} [id P] ''   
   |TensorConstant{0.5} [id Q]

In this way, we can print the theano graphs.

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Build a Customer Churn Prediction Model using Decision Trees
Develop a customer churn prediction model using decision tree machine learning algorithms and data science on streaming service data.

Build a Text Classification Model with Attention Mechanism NLP
In this NLP Project, you will learn to build a multi class text classification model with attention mechanism.

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.

Word2Vec and FastText Word Embedding with Gensim in Python
In this NLP Project, you will learn how to use the popular topic modelling library Gensim for implementing two state-of-the-art word embedding methods Word2Vec and FastText models.

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

Learn How to Build PyTorch Neural Networks from Scratch
In this deep learning project, you will learn how to build PyTorch neural networks from scratch.

Time Series Analysis with Facebook Prophet Python and Cesium
Time Series Analysis Project - Use the Facebook Prophet and Cesium Open Source Library for Time Series Forecasting in Python

MLOps Project to Deploy Resume Parser Model on Paperspace
In this MLOps project, you will learn how to deploy a Resume Parser Streamlit Application on Paperspace Private Cloud.

Ola Bike Rides Request Demand Forecast
Given big data at taxi service (ride-hailing) i.e. OLA, you will learn multi-step time series forecasting and clustering with Mini-Batch K-means Algorithm on geospatial data to predict future ride requests for a particular region at a given time.

Deep Learning Project- Real-Time Fruit Detection using YOLOv4
In this deep learning project, you will learn to build an accurate, fast, and reliable real-time fruit detection system using the YOLOv4 object detection model for robotic harvesting platforms.