Printing/Drawing Theano graphs
pprint()更紧凑,更像数学
o theano.printing.pprint(prediction)
’gt((TensorConstant{1} / (TensorConstant{1} + exp(
TensorConstant{0.5})’
debugprint()更冗长。
o theano.printing.debugprint(prediction)
预编译图:
后编译图:
pydotprint(),用于创建函数的图像。
o预编译图:
theano.printing.pydotprint(prediction, outfile
考虑逻辑回归示例:
o>>> import numpy
o>>> import theano
o>>> import theano.tensor as T
o>>> rng = numpy.random
o>>> # Training data
>>> N = 400
>>> feats = 784
>>> D = (rng.randn(N, feats).astype(theano.config.
>>> training_steps = 10000
o>>> # Declare Theano symbolic variables
>>> x = T.matrix(“x”)
>>> x.tag.test_value = D[0]
o.tag.test_value 作用?
那在调试过程中,要如何查看TensorVariable值呢?可以通过tag.test_value,即定义theano变量x时,赋值给x.tag.test_value。
o>>> y = T.vector(“y”)
>>> y.tag.test_value = D[1]
o>>> w = theano.shared(rng.randn(feats).astype(thea
o>>> b = theano.shared(numpy.asarray(0., dtype=thea
>>> # Construct Theano expression graph
o>>> p_1 = 1 / (1 + T.exp(-T.dot(x, w)-b)) # Probab
o>>> prediction = p_1 > 0.5 # The prediction that i
>>> # Compute gradients
o>>> xent = -y*T.log(p_1) - (1-y)T.log(1-p_1) # Cr
o>>> cost = xent.mean() + 0.01(w**2).sum() # The c
o>>> gw,gb = T.grad(cost, [w,b])
>>> # Training and prediction function
o>>> train = theano.function(inputs=[x,y], outputs=
o>>> predict = theano.function(inputs=[x], outputs=
后编译图:
o theano.printing.pydotprint(predict, outfile="p
优化的训练图:
o theano.printing.pydotprint(train, outfile="pic
Interactive Graph Visualization
o新的d3viz模块
pip install pydot-ng
predict_profiled = th.function([x], y, profile=True)
x_val = rng.normal(0, 1, (ninputs, nfeatures))
y_val = predict_profiled(x_val)
d3v.d3viz(predict_profiled, ‘examples/mlp2.html’)
它不是创建静态图像,而是生成一个HTML文件
预编译图:
theano.printing.pydotprint(predict, outfile="p
theano.printing.pydotprint(train, outfile="pic
后编译图:
theano.printing.pydotprint(prediction, outfile
它不是创建静态图像,而是生成一个HTML文件