In [1]:
import theano.tensor as T
x = T.dmatrix("x")
y = T.dmatrix("y")
z = x + y

In [2]:
type(z)


Out[2]:
theano.tensor.var.TensorVariable

In [5]:
type(z.owner)


Out[5]:
theano.gof.graph.Apply

In [7]:
z.owner.op.name


Out[7]:
'Elemwise{add,no_inplace}'

In [8]:
w = x * 2
w.owner.op.name


Out[8]:
'Elemwise{mul,no_inplace}'

In [13]:
print(w.owner)
print(len(w.owner.inputs))
print(w.owner.inputs) # second input is not 2


Elemwise{mul,no_inplace}(x, DimShuffle{x,x}.0)
2
[x, DimShuffle{x,x}.0]

In [14]:
print(z.owner)
print(len(z.owner.inputs))
print(z.owner.inputs)


Elemwise{add,no_inplace}(x, y)
2
[x, y]

In [18]:
w.owner.inputs[1].owner.inputs


Out[18]:
[TensorConstant{2}]

In [1]:
# graph visualization
import theano
import pydot
v = theano.tensor.vector()
from IPython.display import SVG
SVG(theano.printing.pydotprint(v*2, return_image=True,
                               format='svg'))


Out[1]:
G DimShuffle{x} DimShuffle{x} Elemwise{mul,no_inplace} Elemwise{mul,no_inplace} DimShuffle{x}->Elemwise{mul,no_inplace} 1 TensorType(int8, (True,)) val=2 TensorType(int8, scalar) val=2 TensorType(int8, scalar) val=2 TensorType(int8, scalar)->DimShuffle{x} TensorType(int8, scalar) TensorType(float64, vector) id=3 TensorType(float64, vector) id=3 Elemwise{mul,no_inplace}->TensorType(float64, vector) id=3 TensorType(float64, vector) TensorType(float64, vector) TensorType(float64, vector) TensorType(float64, vector)->Elemwise{mul,no_inplace} 0 TensorType(float64, vector)

In [2]:
# optimization visualization
import theano
import pydot
a = theano.tensor.vector("a")
b = a + a ** 10
f = theano.function([a], b)
print(f([0,1,2]))


[    0.     2.  1026.]

In [6]:
theano.printing.pydotprint(b, outfile = './grap.png', var_with_name_simple=True)


The output file is available at ./grap.png

In [8]:
theano.printing.pydotprint(f, outfile="./graph_opt.png", var_with_name_simple=True)


The output file is available at ./graph_opt.png

In [ ]: