Theano笔记
scan函数
theano.scan(fn, sequences=None, outputs_info=None,non_sequences=None, n_steps=None, truncate_gradient=-1,go_backwards=False, mode=None, name=None, profile=False)
outputs_info is the list of Theano variables or dictionaries describing the initial state of the outputs computed recurrently.
fn是每一步所用的函数,sequences是输入,outputs_info是scan输出在起始的状态。sequences and outputs_info are all parameters of fn in ordered sequence.
scan(fn, sequences = [ dict(input= Sequence1, taps = [-3,2,-1])
, Sequence2
, dict(input = Sequence3, taps = 3) ]
, outputs_info = [ dict(initial = Output1, taps = [-3,-5])
, dict(initial = Output2, taps = None)
, Output3 ]
, non_sequences = [ Argument1, Argument2])
fn should expect the following arguments in this given order:
- Sequence1[t-3]
- Sequence1[t+2]
- Sequence1[t-1]
- Sequence2[t]
- Sequence3[t+3]
- Output1[t-3]
- Output1[t-5]
- Output3[t-1]
- Argument1
- Argument2
import theano
import theano.tensor as T
mode = theano.Mode(linker='cvm')
import numpy as np
def fun(a,b):
return a+b
input=T.vector("input")
output,update=theano.scan(fun,sequences=input,outputs_info=[T.as_tensor_variable(np.asarray(1,input.dtype))])
out=theano.function(inputs=[input],outputs=output)
in1=numpy.array([1,2,3])
print out(in1)
def fun(a,b):
return a+b
input=T.matrix("input")
output,update=theano.scan(fun,sequences=input,outputs_info=[T.as_tensor_variable(np.asarray([0,0,0],input.dtype))])
out=theano.function(inputs=[input,],outputs=output)
in1=numpy.array([[1,2,3],[4,5,6]])
print(in1)
print out(in1)
shared variables相当于全局变量,The value can be accessed and modified by the.get_value() and .set_value() methods. 在function里用updata来修改可以并行。
scan的输出是一个symbol,用来在后面的theano function里作为output和update的规则。当sequences=None时,n_steps应有一个值来限制对后面theano function里的input的循环次数。当sequences不为空时,theano function直接对sequences循环:
components, updates = theano.scan(fn=lambda coefficient, power, free_variable: coefficient * (free_variable ** power),
outputs_info=None,
sequences=[coefficients, theano.tensor.arange(max_coefficients_supported)],
non_sequences=x)
这个例子中,
theano.tensor.arange(max_coefficients_supported)类似于enumerate的index,coefficientes相当与enumerate里到序列值。这里根据顺序,x为free_variable.
Debug:
http://deeplearning.net/software/theano/tutorial/debug_faq.html
theano.config.compute_test_value = 'warn'
- off: Default behavior. This debugging mechanism is inactive.
- raise: Compute test values on the fly. Any variable for which a test value is required, but not provided by the user, is treated as an error. An exception is raised accordingly.
- warn: Idem, but a warning is issued instead of an Exception.
- ignore: Silently ignore the computation of intermediate test values, if a variable is missing a test value.
import theano def inspect_inputs(i, node, fn):
print i, node, "input(s) value(s):", [input[0] for input in fn.inputs], def inspect_outputs(i, node, fn):
print "output(s) value(s):", [output[0] for output in fn.outputs] x = theano.tensor.dscalar('x')
f = theano.function([x], [5 * x],
mode=theano.compile.MonitorMode(
pre_func=inspect_inputs,
post_func=inspect_outputs))
f(3)
mode = 'DEBUG_MODE' 很慢,无效?
使用print
x = theano.tensor.dvector('x')
x_printed = theano.printing.Print('this is a very important value')(x)
f = theano.function([x], x * 5)
f_with_print = theano.function([x], x_printed * 5)
#this runs the graph without any printing
assert numpy.all( f([1, 2, 3]) == [5, 10, 15])
#this runs the graph with the message, and value printed
assert numpy.all( f_with_print([1, 2, 3]) == [5, 10, 15])
Theano笔记的更多相关文章
- Theano 学习笔记(一)
Theano 学习笔记(一) theano 为什么要定义共享变量? 定义共享变量的原因在于GPU的使用,如果不定义共享的话,那么当GPU调用这些变量时,遇到一次就要调用一次,这样就会花费大量时间在数据 ...
- LSTM 分类器笔记及Theano实现
相关讨论 http://tieba.baidu.com/p/3960350008 基于教程http://deeplearning.net/tutorial/lstm.html LSTM基本原理http ...
- Theano学习笔记(二)——逻辑回归函数解析
有了前面的准备,能够用Theano实现一个逻辑回归程序.逻辑回归是典型的有监督学习. 为了形象.这里我们如果分类任务是区分人与狗的照片. 首先是生成随机数对象 importnumpy importth ...
- Theano学习笔记(三)——图结构
图结构(Graph Structures)这是理解Theano该基金会的内部运作. Theano编程的核心是用符号占位符把数学关系表示出来. 图结构的组成部分 如图实现了这段代码: importthe ...
- Theano学习笔记(一)——代数
标量相加 import theano.tensor as T from theano import function x = T.dscalar('x') y = T.dscalar('y') z = ...
- IMPLEMENTING A GRU/LSTM RNN WITH PYTHON AND THEANO - 学习笔记
catalogue . 引言 . LSTM NETWORKS . LSTM 的变体 . GRUs (Gated Recurrent Units) . IMPLEMENTATION GRUs 0. 引言 ...
- Python学习笔记(三)windows下安装theano
2016.6.28补充: 不论是实验室的电脑还是我的笔记本,只要是windows下,theano.test()都是不通过的.虽然能使用一些theano中的函数,但是我感觉很不好. 所以还是转Ubunt ...
- Theano学习笔记(四)——导数
导数使用T.grad计算. 这里使用pp()打印梯度的符号表达式. 第3行输出是打印了经过优化器简化的符号梯度表达式,与第1个输出相比确实简单多了. fill((x** TensorConstant{ ...
- Theano安装笔记
由于实验需要,近三个月来,安装过十几次Theano,基本上每次都是从最基本的nvidia driver装起.总结一些粗浅的安装心得. GPU:Nvidia K40, M40, M60 软件环境:Unb ...
随机推荐
- PostgreSQL安装及使用教程一(exe安装方式)
下载安装 百度搜索PostgreSQL,进入官网,选择相应版本的图形化安装程序(BigSQL)安装即可 连接数据库 对数据库操作有两种方式,一种是通过命令行工具psql,另一种是通过图形化界面pgAd ...
- php递归方法
<?phpheader("Content-type:text/html;charset=utf-8");$city=array( array('id'=>1,'name ...
- [jQuery] 判断复选框checkbox是否选中checked
返回值是true/false method 1: $("#register").click(function(){ if($("#accept").get(0) ...
- change color1
private void Form1_Load(object sender, EventArgs e) { string str = "server= ...
- tomcat ----> 启动,关闭和配置等等
1.启动 在tomcat安装目录的bin文件中双击startup.bat. 2.关闭 在tomcat安装目录的bin文件中双击shutdown.bat. 3.配置tomcat-user.xml文件 ( ...
- js时间字符串转为标准时间
//将字符串转换为时间格式,适用各种浏览器,格式如2016-09-09T17:02:37.227"function GetTimeByTimeStr(dateString) { var ti ...
- P4426 [HNOI/AHOI2018]毒瘤
挺不错的一个题. 题意即为求一个图的独立集方案数. 如果原图是一棵树,可以直接大力f[x][0/1]来dp. 由于非树边很少,考虑2^11容斥,强制某些点必选,然后再O(n)dp,这样应该过不了. 发 ...
- 【洛谷p2822】组合数问题
(突然想 ??忘掉了wdt) (行吧那就%%%hmr) 组合数问题[传送门] (因为清明要出去培训数学知识所以一直在做数论) 组合数<=>杨辉三角形(从wz那拐来的技能 ...
- 【洛谷p1601】A+B Problem(高精)
高精度加法的思路还是很简单容易理解的 A+B Problem(高精)[传送门] 洛谷算法标签: 附上代码(最近懒得一批) #include<iostream> #include<cs ...
- Linux中man手册的使用
Linux中man手册的使用 ——以CentOS6.8为例 本文旨在介绍在Linux中如何快速入手新命令,毕竟在Linux系统中,可以通过命令完成一切操作. 相关命令:help man whatis ...