来自: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. Java Se :线性表

    Java的集合框架分为两个系列,Collection和Map系列.在大学期间,学习数据结构时,好像学习了线性表.非线性表.树,哎,都给忘了.其实,在Collection系列内部又可以分为线性表.集合两 ...

  2. spring hibernate摘记

    一.spring 1.ContextLoaderListener    它作用就是启动Web容器时,自动装配ApplicationContext的配置信息.因为它实现了ServletContextLi ...

  3. 使用eclipse查看源码的方法

    打开eclipse,建立项目:Test,将struts2相关jar包导入到其中.在Package Explorer标签栏下操作. 如下图: 在此,以查阅struts2中,struts2-core-2. ...

  4. PostgreSQL-安装9.2

    一.环境 VM虚拟机 NAME="Ubuntu" VERSION="12.04.4 LTS, Precise Pangolin" 二.过程  1.安装make ...

  5. od

    $od [-t type]查看非文本文件 a 使用默认字符输出 c 使用ASC II字符输出 d[size] 使用十进制来输出数据,每个整数占用size byte o ..八 x ..十六 f ..浮 ...

  6. JAVA学习网址收藏

    什么是JDK?http://baike.baidu.com/subview/25214/5047948.htm?fr=aladdin Java经典入门教程(环境说明) http://wenku.bai ...

  7. UVALive 5066 Fire Drill --BFS+DP

    题意:有一个三维的地图,有n个人被困住,现在消防队员只能从1楼的一个入口进入,营救被困者,每一个被困者有一个价值,当消防队员找到一个被困者之后,他可以营救或者见死不救,如果救的话,他必须马上将其背到入 ...

  8. KSFramework:Unity3D开发框架快速入门

    KSFramework知识 https://github.com/mr-kelly/KSFramework KSFramework是一个整合KEngine.SLua和一些开发组件组成的全功能Unity ...

  9. java 22 - 14 JDK1.5以后的Lock锁

    在之前解决线程安全的过程中,虽然我们可以理解同步代码块和同步方法的锁对象问题, 但是我们并没有直接看到在哪里加上了锁,在哪里释放了锁, 为了更清晰的表达如何加锁和释放锁,JDK5以后提供了一个新的锁对 ...

  10. curl 工具收集

    注意:curl 目标地址不能使用 0.0.0.0: port, 这样会curl返回结果显示不正常: 实际上有返回,但是curl提示没有数据.