scan函数

theano.scan(fnsequences=Noneoutputs_info=None,non_sequences=Nonen_steps=Nonetruncate_gradient=-1,go_backwards=Falsemode=Nonename=Noneprofile=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:

  1. Sequence1[t-3]
  2. Sequence1[t+2]
  3. Sequence1[t-1]
  4. Sequence2[t]
  5. Sequence3[t+3]
  6. Output1[t-3]
  7. Output1[t-5]
  8. Output3[t-1]
  9. Argument1
  10. 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笔记的更多相关文章

  1. Theano 学习笔记(一)

    Theano 学习笔记(一) theano 为什么要定义共享变量? 定义共享变量的原因在于GPU的使用,如果不定义共享的话,那么当GPU调用这些变量时,遇到一次就要调用一次,这样就会花费大量时间在数据 ...

  2. LSTM 分类器笔记及Theano实现

    相关讨论 http://tieba.baidu.com/p/3960350008 基于教程http://deeplearning.net/tutorial/lstm.html LSTM基本原理http ...

  3. Theano学习笔记(二)——逻辑回归函数解析

    有了前面的准备,能够用Theano实现一个逻辑回归程序.逻辑回归是典型的有监督学习. 为了形象.这里我们如果分类任务是区分人与狗的照片. 首先是生成随机数对象 importnumpy importth ...

  4. Theano学习笔记(三)——图结构

    图结构(Graph Structures)这是理解Theano该基金会的内部运作. Theano编程的核心是用符号占位符把数学关系表示出来. 图结构的组成部分 如图实现了这段代码: importthe ...

  5. Theano学习笔记(一)——代数

    标量相加 import theano.tensor as T from theano import function x = T.dscalar('x') y = T.dscalar('y') z = ...

  6. IMPLEMENTING A GRU/LSTM RNN WITH PYTHON AND THEANO - 学习笔记

    catalogue . 引言 . LSTM NETWORKS . LSTM 的变体 . GRUs (Gated Recurrent Units) . IMPLEMENTATION GRUs 0. 引言 ...

  7. Python学习笔记(三)windows下安装theano

    2016.6.28补充: 不论是实验室的电脑还是我的笔记本,只要是windows下,theano.test()都是不通过的.虽然能使用一些theano中的函数,但是我感觉很不好. 所以还是转Ubunt ...

  8. Theano学习笔记(四)——导数

    导数使用T.grad计算. 这里使用pp()打印梯度的符号表达式. 第3行输出是打印了经过优化器简化的符号梯度表达式,与第1个输出相比确实简单多了. fill((x** TensorConstant{ ...

  9. Theano安装笔记

    由于实验需要,近三个月来,安装过十几次Theano,基本上每次都是从最基本的nvidia driver装起.总结一些粗浅的安装心得. GPU:Nvidia K40, M40, M60 软件环境:Unb ...

随机推荐

  1. C/C++.全文件名全路径名分割拆分分解

    1._splitpath ZC:windows api的话 可以使用 PathFindFileNameA.PathFindExtensionA.PathFileExistsA等一系列函数 2.测试代码 ...

  2. EditPlus查找替换

    换行符\n,记得选择正则表达式 1]正则表达式应用——替换指定内容到行尾解决:① 在替换对话框,查找内容里输入“abc.*”② 同时勾选“正则表达式”复选框,然后点击“全部替换”按钮其中,符号的含义如 ...

  3. C#发起HTTP请求

    浏览器能看到的数据  用后端模拟请求都能获取到  如果拿不到 看看是不是请求参数哪里没设置 刚好服务器检查了这个参数 string url = ""; string para = ...

  4. python3+虹软2.0 离线人脸识别 demo

    python3+虹软2.0的所有功能整合测试完成,并对虹软所有功能进行了封装,现提供demo主要功能,1.人脸识别2.人脸特征提取3.特征比对4.特征数据存储与比对其他特征没有添加 虹软SDK下载戳这 ...

  5. PyMongo官方文档翻译——VNPY

    PyMongo是MongoDB数据库的python模块 VNPY默认的数据库,没有采用SQL类型的数据库,而是采用No-Sql类型的MongoDB数据库, 对于想了解VNPY内部结构的童鞋,多多少少会 ...

  6. google浏览器如何导出书签

     首先打开浏览器点右侧的自定义及控制Google chrome.  点击书签-书签管理器   打开书签管理器界面中·   点击书签管理器的整理  最下面的将书签导出到html文件..  弹出另存为对话 ...

  7. 雷林鹏分享: C# 教程

    C# 教程 C# 是一个简单的.现代的.通用的.面向对象的编程语言,它是由微软(Microsoft)开发的. 本教程将告诉您基础的 C# 编程,同时将向您讲解 C# 编程语言相关的各种先进理念. 现在 ...

  8. gitignore有时候为啥过滤不了文件或目录

    一.问题介绍 使用Git过程中,有时候我们想过滤项目中的部分文件,在.gitignore中加入该文件名称或该文件所在目录的名称,比如我们的项目日志文件(.log文件) 但是有时候发现不管用.不好使. ...

  9. 静默安装/ 普通安装与root权限获取相关

    静默安装 有时候使用第三方的插件时我们需要静默安装其提供的apk包,静默安装时我们需要获取root权限,如下代码 Process process = Runtime.getRuntime().exec ...

  10. 基于Lua语言的触动精灵脚本开发

    工具下载 官网地址 连接模拟器 studio连接 首先要先下载ADB模拟器连接IDE,注意,这里一定要用官网提供的ADB,安卓开发的adb不行!!! 下载好之后,打开studio,输入Access K ...