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. SpringBoot的文件上传

    先在src/main/resources下新建一个static目录用以存放html页面,简单的html页面如下 <!DOCTYPE html> <html> <head& ...

  2. Codeforces 600 E - Lomsat gelral

    E - Lomsat gelral 思路1: 树上启发式合并 代码: #include<bits/stdc++.h> using namespace std; #define fi fir ...

  3. 最常出现的字符串 Most Common Word

    2018-10-26 00:32:05 问题描述: 问题求解: 方法一.Trie 最长出现的字符串,最容易想到的解法就是Trie树了,于是首先使用Trie树进行了实现,代码量有点大,当然了是可以A掉的 ...

  4. app和bootloader跳转 MSP与PSP

    1.不要把跳转函数放在中断中,如此导致在跳转后的app或者bootloder都是在中断状态,只要你一开启该中断,就可能出现硬件中断了 2.如果你的APP使用了ucos系统,在跳转函数中还需要增加__s ...

  5. centos php5.4 升级 php7

    接上篇,edusoho需要php5.5以上版本,于是需要升级本地php php是通过yum默认安装的.以下安装参考 link https://blog.csdn.net/u012569217/arti ...

  6. for...in和for...of循环的区别

    使用for...in和for...of分别对Array,Set,Map做测试 var a=["A","B","C"]; var b=new ...

  7. 守护进程函数——内部的小范围try catch 增强了 while死循环执行的 可靠性

    void Watch()        {            try            {                LogHelper.WriteLog("WatchServi ...

  8. OSPF - 3,OSPF区域和LSA

    1,四种末端区域骨干区域和标准区域:1,2,3,4,5,包含5类LSA,为了减少某些普通区域的LSA(主要就是4类和5类,有时做绝到连3类也不要了),引入了末梢区域.同时为了确保数据能出去,一般ABR ...

  9. LeetCode--374--猜数字大小

    问题描述: 我们正在玩一个猜数字游戏. 游戏规则如下: 我从 1 到 n 选择一个数字. 你需要猜我选择了哪个数字. 每次你猜错了,我会告诉你这个数字是大了还是小了. 你调用一个预先定义好的接口 gu ...

  10. LeetCode--278--第一个错误的版本

    问题描述: 你是产品经理,目前正在带领一个团队开发新的产品.不幸的是,你的产品的最新版本没有通过质量检测.由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所有版本都是错的. 假设你有 n 个 ...