打印/绘画 Theano 图表

打印/绘画 Theano 图表

    Theano 提供了两个函数(theano.pp() and theano.printing.debugprint()),在终端上 打印出图表. 这两个函数以不同的方式来表示画图: pp()更简单, debugprint()显得更繁琐. Theano 同时提供函数pydotprint()来创建函数的png图像, You can read about them in printing – Graph Printing and Symbolic Print Statement.

    考虑回归函数,增加打印函数,:

import theano
import theano.tensor as T
import numpy
import os
rng = numpy.randomN = 400
feats = 784
D = (rng.randn(N, feats).astype(theano.config.floatX),
rng.randint(size=N,low=0, high=2).astype(theano.config.floatX))
training_steps = 10000
# Declare Theano symbolic variables
x = T.matrix("x")y = T.vector("y")
w = theano.shared(rng.randn(feats).astype(theano.config.floatX), name="w")
b = theano.shared(numpy.asarray(0., dtype=theano.config.floatX), name="b")
x.tag.test_value = D[0]
y.tag.test_value = D[1]
#print "Initial model:"
#print w.get_value(), b.get_value()

# Construct Theano expression graph
p_1 = 1 / (1 + T.exp(-T.dot(x, w) - b)) # Probability of having a one
prediction = p_1 > 0.5 # The prediction that is done: 0 or 1
xent = -y * T.log(p_1) - (1 - y) * T.log(1 - p_1) # Cross-entropy
cost = xent.mean() + 0.01 * (w ** 2).sum() # The cost to optimize
gw,gb = T.grad(cost, [w, b])# Compile expressions to function
strain = theano.function(
                    inputs=[x, y],
                    outputs=[prediction, xent],
                    updates=[(w, w - 0.01 * gw), (b, b - 0.01 * gb)],
                    name="train")
predict = theano.function(inputs=[x], outputs=prediction,
                    name="predict")
if any([x.op.__class__.__name__ in ['Gemv', 'CGemv'] for x in train.maker.fgraph.toposort()]):
    print 'Used the cpu'
elif any([x.op.__class__.__name__ == 'GpuGemm' for x in train.maker.fgraph.toposort()]):
    print 'Used the gpu'
else:
    print 'ERROR, not able to tell if theano used the cpu or the gpu'
    print train.maker.fgraph.toposort()
for i in range(training_steps):
    pred, err = train(D[0], D[1])
#print "Final model:"
#print w.get_value(), b.get_value()
print "target values for D"
print D[1]
print "prediction on D"
print predict(D[0])
# Print the picture graphs
# after compilation
if not os.path.exists('pics'):
    os.mkdir('pics')
theano.printing.pydotprint(predict,
                           outfile="pics/logreg_pydotprint_predic.png",
                           var_with_name_simple=True)
# before compilation
theano.printing.pydotprint_variables(prediction,
                           outfile="pics/logreg_pydotprint_prediction.png",
                           var_with_name_simple=True)
theano.printing.pydotprint(train,
                           outfile="pics/logreg_pydotprint_train.png",
                           var_with_name_simple=True)

Pretty Printing

    theano.printing.pprint(variable)

>>> theano.printing.pprint(prediction)  # (pre-compilation)
gt((TensorConstant{1} / (TensorConstant{1} + exp(((-(x \\dot w)) - b)))),TensorConstant{0.5})

Debug Printing

theano.printing.debugprint({fct, variable, list of variables})

>>> theano.printing.debugprint(prediction)  # (pre-compilation)Elemwise{gt,no_inplace} [@181772236] '' |Elemwise{true_div,no_inplace} [@181746668] '' | |InplaceDimShuffle{x} [@181746412] '' | | |TensorConstant{1} [@181745836] | |Elemwise{add,no_inplace} [@181745644] '' | | |InplaceDimShuffle{x} [@181745420] '' | | | |TensorConstant{1} [@181744844] | | |Elemwise{exp,no_inplace} [@181744652] '' | | | |Elemwise{sub,no_inplace} [@181744012] '' | | | | |Elemwise{neg,no_inplace} [@181730764] '' | | | | | |dot [@181729676] '' | | | | | | |x [@181563948] | | | | | | |w [@181729964] | | | | |InplaceDimShuffle{x} [@181743788] '' | | | | | |b [@181730156] |InplaceDimShuffle{x} [@181771788] '' | |TensorConstant{0.5} [@181771148]>>> theano.printing.debugprint(predict)  # (post-compilation)Elemwise{Composite{neg,{sub,{{scalar_sigmoid,GT},neg}}}} [@183160204] ''   2 |dot [@183018796] ''   1 | |x [@183000780] | |w [@183000812] |InplaceDimShuffle{x} [@183133580] ''   0 | |b [@183000876] |TensorConstant{[ 0.5]} [@183084108]


转载于:https://my.oschina.net/xushizhe/blog/358850

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值