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 ...
随机推荐
- IIS上部署MVC网站,打开后ExtensionlessUrlHandler-4.0
IIS上部署MVC网站,打开后ExtensionlessUrlHandler-Integrated-4.0解决方法IIS上部署MVC网站,打开后500错误 IS上部署MVC网站,打开后Extensio ...
- psql常用命令
cmd命令 pg_ctl --version:查看pgsl版本 pg_ctl -D /xx/pgdata start:启动pgsl数据库 注:必须在环境变量中设置了PGDATA后才能省略-D参数 ,可 ...
- 贪心 —— 今年暑假不AC
贪心基本题, 有助于理解贪心算法的思想 #include <cstdio> #include <algorithm> using namespace std; struct P ...
- 解决Android Studio卡在Gradle:Resolve dependecies 'app:_debugCompile'问题
转载: http://blog.csdn.net/callzjy/article/details/53662073 该死的破墙,我被恶心了一个晚上. 做墙的技术员,TMD的能不能上点心,至少把技术网站 ...
- pointer-events
在做移动端的页面时,经常会遇到点击(touch)一个弹出的层,在上面触发点击(touch)事件,当弹出层关闭之后点击(touch)事件会穿透到下面的层,这时候如果下一层的某个元素也绑定了点击(touc ...
- Hibernate二级缓存 --Hibernate框架
Hibernate本身只提供了二级缓存的规范,但并未实现,所以需要第三方缓存插件的支持.常用的二级缓存第三方插件有:EHCache.Memcached.OSCache.SwarmCache.JBoss ...
- java开学第一周测试代码
Account.java /* 信1705-3 20173612 陈阳阳 */package ATM; public class Account { private String accountID ...
- 配置Tomcat配置路径
<Host name="localhost" appBase="webapps" unpackWARs="true" autoDepl ...
- Post四种Content-Type
application/x-www-form-urlencoded 这应该是最常见的 POST 提交数据的方式了.浏览器的原生 form 表单,如果不设置 enctype 属性,那么最终就会以 app ...
- 【洛谷p1319】压缩技术
(许久不见,甚是想念) 压缩技术[传送门] 洛谷上滴算法标签: 然而这是一道入门难度的题.(不管不管,就写它了) 好的先说一下思路吧wait!我忘记了咋做的当时. 首先做题第一道坎儿,如何输入若干个( ...