来自:http://deeplearning.net/software/theano/tutorial/printing_drawing.html

Printing/Drawing Theano graphs

Theano提供的函数theano.printing.pprint() 和 theano.printing.debugprint() 可以用来在编译前和后打印一个graph到终端上。 pprint() 该函数更紧凑而且更偏向于数学形式, debugprint() 更为的详细。
Theano同样提供pydotprint() 来生成一张有关该函数的图片。更详细的可以看看 printing
– Graph Printing and Symbolic Print Statement
.

note:当打印theano函数的时候,有时候会比较难读懂。为了简化过程,可以禁止一些theano优化,只要使用theano的flag: optimizer_excluding=fusion:inplace.
不要在工作执行的时候使用这个flag,这会使得graph更慢而且使用更多的内存。

考虑逻辑回归的例子:

>>> import numpy
>>> import theano
>>> import theano.tensor as T
>>> rng = numpy.random
>>> # Training data
>>> N = 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]
>>> # 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
>>> # Compute gradients
>>> 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])
>>> # Training and prediction function
>>> train = 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")

友好的打印结果:

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

调试打印

预编译图:

>>> theano.printing.debugprint(prediction)
Elemwise{gt,no_inplace} [@A] ''
|Elemwise{true_div,no_inplace} [@B] ''
| |DimShuffle{x} [@C] ''
| | |TensorConstant{1} [@D]
| |Elemwise{add,no_inplace} [@E] ''
| |DimShuffle{x} [@F] ''
| | |TensorConstant{1} [@D]
| |Elemwise{exp,no_inplace} [@G] ''
| |Elemwise{sub,no_inplace} [@H] ''
| |Elemwise{neg,no_inplace} [@I] ''
| | |dot [@J] ''
| | |x [@K]
| | |w [@L]
| |DimShuffle{x} [@M] ''
| |b [@N]
|DimShuffle{x} [@O] ''
|TensorConstant{0.5} [@P]

编译后的图:

>>> theano.printing.debugprint(predict)
Elemwise{Composite{GT(scalar_sigmoid((-((-i0) - i1))), i2)}} [@A] '' 4
|CGemv{inplace} [@B] '' 3
| |Alloc [@C] '' 2
| | |TensorConstant{0.0} [@D]
| | |Shape_i{0} [@E] '' 1
| | |x [@F]
| |TensorConstant{1.0} [@G]
| |x [@F]
| |w [@H]
| |TensorConstant{0.0} [@D]
|InplaceDimShuffle{x} [@I] '' 0
| |b [@J]
|TensorConstant{(1,) of 0.5} [@K]

graph的图片打印

预编译图

>>> theano.printing.pydotprint(prediction, outfile="pics/logreg_pydotprint_prediction.png", var_with_name_simple=True)
The output file is available at pics/logreg_pydotprint_prediction.png





编译后的图

>>> theano.printing.pydotprint(predict, outfile="pics/logreg_pydotprint_predict.png", var_with_name_simple=True)
The output file is available at pics/logreg_pydotprint_predict.png



优化后的训练图:

>>> theano.printing.pydotprint(train, outfile="pics/logreg_pydotprint_train.png", var_with_name_simple=True)
The output file is available at pics/logreg_pydotprint_train.png

参考资料:

[1] 官网:http://deeplearning.net/software/theano/tutorial/printing_drawing.html



Theano2.1.5-基础知识之打印出theano的图的更多相关文章

  1. IP 基础知识全家桶,45 张图一套带走

    前言 前段时间,有读者希望我写一篇关于 IP 分类地址.子网划分等的文章,他反馈常常混淆,摸不着头脑. 那么,说来就来!而且要盘就盘全一点,顺便挑战下小林的图解功力,所以就来个 IP 基础知识全家桶. ...

  2. 基础知识《零》---一张图读懂JDK,JRE,JVM的区别与联系

  3. 如何看K线图基础知识

    在日K线图中一般白线.黄线.紫线.绿线依次分别表示:5.10.20.60日移动平均线,但这并不是固定的,会根据设置的不同而不同,比如你也可以在系统里把它们设为5.15.30.60均线. 你看K线图的上 ...

  4. LLDB基础知识

    LLDB基础知识 LLDB控制台 Xcode中内嵌了LLDB控制台,在Xcode中代码的下方,我们可以看到LLDB控制台. LLDB控制台平时会输出一些log信息.如果我们想输入命令调试,必须让程序进 ...

  5. 1.进入debug模式(基础知识列表)

    1.进入debug模式(基础知识列表)1.设置断点 2.启动servers端的debug模式 3.运行程序,在后台遇到断点时,进入debug调试状态 ========================= ...

  6. 【考试】java基础知识测试,看你能得多少分?

    1 前言 共有5道java基础知识的单项选择题,每道20分,共计100分.解析和答案在最后. 2 试题 2.1 如下程序运行结果是什么? class Parent { public Parent(St ...

  7. python基础知识小结-运维笔记

    接触python已有一段时间了,下面针对python基础知识的使用做一完整梳理:1)避免‘\n’等特殊字符的两种方式: a)利用转义字符‘\’ b)利用原始字符‘r’ print r'c:\now' ...

  8. Pyhton基础知识(一)

    Pyhton基础知识(一)一.cpu 内存 硬盘 操作系统之间的关系1.cpu 中央处理器 运算中心与控制中心 相当于人的大脑.2.内存 暂时存储数据 将应用程序加载到内存 以便于cpu进行数据传输交 ...

  9. java第九节 网络编程的基础知识

    /** * * 网络编程的基础知识 * 网络协议与TCP/IP * IP地址和Port(端口号) * 本地回路的IP地址:127.0.0.1 * 端口号的范围为0-65535之间,0-1023之间的端 ...

随机推荐

  1. centos7 ssh 设置key认证

    vi /etc/ssh/sshd_config 查找RSAAuthentication.StrictModes.PubkeyAuthentication.AuthorizedKeysFile把所在行修 ...

  2. mysql错误一例:ERROR 1030 (HY000): Got error 28 from storage engine

    在使用mysqldump导出一份建库脚本是,发生了下面的错误: 当执行 desc table_name; 时也报错: tag为表名,show index from tag;倒是可以执行. 其实真正的错 ...

  3. SQL Server求解最近多少销售记录的销售额占比总销售额的指定比例

    看园中SQL Server大V潇潇隐者的博文,发现一边文就是描述了如标题描述的问题.   具体的问题描述我通过潇潇隐者的博文的截图来阐释: 注意:如果以上截取有所侵权,也请作者告知,再次感谢.   当 ...

  4. my_ls

    #include<stdio.h> #include<dirent.h> #include<string.h> #include<sys/types.h> ...

  5. Android之drawable state各个属性详解

    android:drawable 放一个drawable资源android:state_pressed 是否按下,如一个按钮触摸或者点击.android:state_focused 是否取得焦点,比如 ...

  6. ixgbe rx_missed_errors

    https://communities.intel.com/thread/54600?start=0&tstart=0 I am acquiring 800Mb/sec+ multicast ...

  7. 用字符串模拟两个大数相加——java实现

    问题: 大数相加不能直接使用基本的int类型,因为int可以表示的整数有限,不能满足大数的要求.可以使用字符串来表示大数,模拟大数相加的过程. 思路: 1.反转两个字符串,便于从低位到高位相加和最高位 ...

  8. 网页中显示pdf

    1.<embed width="800" height="600" src="test_pdf.pdf"> </embed ...

  9. 贤者时间太久了么?--MySQL继续玩

    hi 给自己放了大概三天的假,没有一点点防备,没有一点点准备,无意的 是不是贤者时间过不去了我不知道啊...继续看东西吧 1.MySQL -----运算符和函数----- 字符函数,数值运算符,比较运 ...

  10. [HOOLOO] zizaco/entrust 5.2.x-dev Class name must be a valid object or a string

    在使用laravel 5.1权限管理,使用  安装 zizaco/entrust 5.2.x-dev的时候执行 php artisan entrust:migration的时候报以下错误: [Symf ...