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

Ameeruddin Mohammed

ETL (Abintio) developer at IBM
linkedin profile url

I come from a background in Marketing and Analytics and when I developed an interest in Machine Learning algorithms, I did multiple in-class courses from reputed institutions though I got good... Read More

Relevant Projects

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Azure Text Analytics for Medical Search Engine Deployment
Microsoft Azure Project - Use Azure text analytics cognitive service to deploy a machine learning model into Azure Databricks

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

Topic modelling using Kmeans clustering to group customer reviews
In this Kmeans clustering machine learning project, you will perform topic modelling in order to group customer reviews based on recurring patterns.

Build Regression Models in Python for House Price Prediction
In this Machine Learning Regression project, you will build and evaluate various regression models in Python for house price prediction.

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP

Hands-On Approach to Causal Inference in Machine Learning
In this Machine Learning Project, you will learn to implement various causal inference techniques in Python to determine, how effective the sprinkler is in making the grass wet.

Build CNN for Image Colorization using Deep Transfer Learning
Image Processing Project -Train a model for colorization to make grayscale images colorful using convolutional autoencoders.

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.

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.