In [1]:
import numpy
import numpy.random as rng
import theano
import theano.tensor as T
import theano.printing as pr
import theano.d3printing as d3p
from theano.d3printing.formatting import GraphFormatter
from theano import ProfileMode
import pydot as pd

from IPython.display import SVG
Couldn't import dot_parser, loading of dot files will not be possible.
In [2]:
%load_ext autoreload
%autoreload 2

Model

In [3]:
n = 100
m = 50
h = 10

W = theano.shared(np.random.normal(0, 1, (m, h)).astype(theano.config.floatX), borrow=True)
X = T.dmatrix('X')
Z = T.nnet.sigmoid(T.dot(X, W))
zm = T.mean(Z)

predict_profiled = theano.function([X], [Z, zm], profile=True)
data = rng.rand(n, m)
dZ, dzm = predict_profiled(data)

predict_unprofiled = theano.function([X], [Z, zm], profile=False)
In [4]:
gf = GraphFormatter()

Profiled graphed

In [5]:
g = gf.to_pydot(predict_profiled)
d3p.d3print(predict_profiled, 'predict_profiled.html')
The output file is available at predict_profiled.html
In [6]:
SVG(g.create_svg())
Out[6]:
G Shape_i{1} Shape_i{1} MakeVector MakeVector Shape_i{1}->MakeVector 1 TensorType(int64, scalar) TensorType(float64, matrix) TensorType(float64, matrix) TensorType(float64, matrix)->Shape_i{1} TensorType(float64, matrix) Dot22 Dot22 TensorType(float64, matrix)->Dot22 1 TensorType(float64, matrix) Shape_i{0} Shape_i{0} Shape_i{0}->MakeVector 0 TensorType(int64, scalar) name=X TensorType(float64, matrix) name=X TensorType(float64, matrix) name=X TensorType(float64, matrix)->Shape_i{0} TensorType(float64, matrix) name=X TensorType(float64, matrix)->Dot22 0 TensorType(float64, matrix) Elemwise{ScalarSigmoid}[(0, 0)] Elemwise{ScalarSigmoid}[(0, 0)] Dot22->Elemwise{ScalarSigmoid}[(0, 0)] TensorType(float64, matrix) Elemwise{Cast{float64}} Elemwise{Cast{float64}} MakeVector->Elemwise{Cast{float64}} TensorType(int64, vector) TensorType(float64, matrix) id=6 TensorType(float64, matrix) id=6 Elemwise{ScalarSigmoid}[(0, 0)]->TensorType(float64, matrix) id=6 TensorType(float64, matrix) Sum{acc_dtype=float64} Sum{acc_dtype=float64} Elemwise{ScalarSigmoid}[(0, 0)]->Sum{acc_dtype=float64} TensorType(float64, matrix) Subtensor{int64} Subtensor{int64} Elemwise{Cast{float64}}->Subtensor{int64} 0 TensorType(float64, vector) Subtensor{int64} id=8 Subtensor{int64} id=8 Elemwise{Cast{float64}}->Subtensor{int64} id=8 0 TensorType(float64, vector) Elemwise{Composite{((i0 / i1) / i2)}}[(0, 0)] Elemwise{Composite{((i0 / i1) / i2)}}[(0, 0)] Sum{acc_dtype=float64}->Elemwise{Composite{((i0 / i1) / i2)}}[(0, 0)] 0 TensorType(float64, scalar) Subtensor{int64}->Elemwise{Composite{((i0 / i1) / i2)}}[(0, 0)] 2 TensorType(float64, scalar) val=1 int64 val=1 int64 val=1 int64->Subtensor{int64} 1 int64 Subtensor{int64} id=8->Elemwise{Composite{((i0 / i1) / i2)}}[(0, 0)] 1 TensorType(float64, scalar) val=0 int64 val=0 int64 val=0 int64->Subtensor{int64} id=8 1 int64 TensorType(float64, scalar) id=13 TensorType(float64, scalar) id=13 Elemwise{Composite{((i0 / i1) / i2)}}[(0, 0)]->TensorType(float64, scalar) id=13 TensorType(float64, scalar)

Unprofiled graph

In [7]:
g = gf.to_pydot(predict_unprofiled)
d3p.d3print(predict_unprofiled, 'predict_unprofiled.html')
The output file is available at predict_unprofiled.html
In [8]:
SVG(g.create_svg())
Out[8]:
G Shape_i{1} Shape_i{1} MakeVector MakeVector Shape_i{1}->MakeVector 1 TensorType(int64, scalar) TensorType(float64, matrix) TensorType(float64, matrix) TensorType(float64, matrix)->Shape_i{1} TensorType(float64, matrix) Dot22 Dot22 TensorType(float64, matrix)->Dot22 1 TensorType(float64, matrix) Shape_i{0} Shape_i{0} Shape_i{0}->MakeVector 0 TensorType(int64, scalar) name=X TensorType(float64, matrix) name=X TensorType(float64, matrix) name=X TensorType(float64, matrix)->Shape_i{0} TensorType(float64, matrix) name=X TensorType(float64, matrix)->Dot22 0 TensorType(float64, matrix) Elemwise{ScalarSigmoid}[(0, 0)] Elemwise{ScalarSigmoid}[(0, 0)] Dot22->Elemwise{ScalarSigmoid}[(0, 0)] TensorType(float64, matrix) Elemwise{Cast{float64}} Elemwise{Cast{float64}} MakeVector->Elemwise{Cast{float64}} TensorType(int64, vector) TensorType(float64, matrix) id=6 TensorType(float64, matrix) id=6 Elemwise{ScalarSigmoid}[(0, 0)]->TensorType(float64, matrix) id=6 TensorType(float64, matrix) Sum{acc_dtype=float64} Sum{acc_dtype=float64} Elemwise{ScalarSigmoid}[(0, 0)]->Sum{acc_dtype=float64} TensorType(float64, matrix) Subtensor{int64} Subtensor{int64} Elemwise{Cast{float64}}->Subtensor{int64} 0 TensorType(float64, vector) Subtensor{int64} id=8 Subtensor{int64} id=8 Elemwise{Cast{float64}}->Subtensor{int64} id=8 0 TensorType(float64, vector) Elemwise{Composite{((i0 / i1) / i2)}}[(0, 0)] Elemwise{Composite{((i0 / i1) / i2)}}[(0, 0)] Sum{acc_dtype=float64}->Elemwise{Composite{((i0 / i1) / i2)}}[(0, 0)] 0 TensorType(float64, scalar) Subtensor{int64}->Elemwise{Composite{((i0 / i1) / i2)}}[(0, 0)] 2 TensorType(float64, scalar) val=1 int64 val=1 int64 val=1 int64->Subtensor{int64} 1 int64 Subtensor{int64} id=8->Elemwise{Composite{((i0 / i1) / i2)}}[(0, 0)] 1 TensorType(float64, scalar) val=0 int64 val=0 int64 val=0 int64->Subtensor{int64} id=8 1 int64 TensorType(float64, scalar) id=13 TensorType(float64, scalar) id=13 Elemwise{Composite{((i0 / i1) / i2)}}[(0, 0)]->TensorType(float64, scalar) id=13 TensorType(float64, scalar)