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

标量相加

import theano.tensor as T
from theano import function
x = T.dscalar('x')
y = T.dscalar('y')
z = x + y
f = function([x, y], z)

输入定义两个符号变量来代替数值,输出是一个0维的numpy.ndarray数组。

 

矩阵相加

把输入类型换一下就行了,矩阵如果维数不同,会遵循NumPy的广播规则。

import theano.tensor as T
from theano import function
x = T.dmatrix('x')
y = T.dmatrix('y')
z = x + y
f = function([x, y], z)

 

定义一个公式如:a ** 2 + b ** 2 + 2 * a* b

这里每个变量都需要单独申明。

import theano
a = theano.tensor.vector()
b = theano.tensor.vector()
out = a ** 2 + b ** 2 + 2 * a * b
f = theano.function([a,b],out)
print f([0, 1],[1,2])
>>> 
[ 1. 9.]

 

支持多输出

import theano.tensor as T
from theano import function
a, b = T.dmatrices('a', 'b')
diff = a - b
abs_diff = abs(diff)
diff_squared = diff**2
f = function([a, b], [diff, abs_diff,diff_squared])
print f([[1, 1], [1, 1]], [[0, 1], [2,3]])
>>> 
[array([[ 1.,  0.],
      [-1., -2.]]), array([[ 1.,  0.],
      [ 1.,  2.]]), array([[ 1.,  0.],
      [ 1.,  4.]])]

 

设置默认参数

和标准Python一样,缺省参数必须在非缺省之后,也可以定义缺省变量名。

import theano.tensor as T
from theano import function
from theano import Param
x, y = T.dscalars('x', 'y')
z = x + y
f = function([x, Param(y, default=1,name='by_name')],z)
print f(33)
print f(33, 2)
print f(33,by_name=3)
>>> 
34.0
35.0
36.0

 

共享变量

为了在GPU上更好的性能,引入共享变量,以累加器为例。

import theano.tensor as T
from theano import function
from theano import shared
state = shared(0)
inc = T.iscalar('inc')
accumulator = function([inc], state,updates=[(state, state+inc)])
print state.get_value()
accumulator(1)
print state.get_value()
accumulator(300)
print state.get_value()
state.set_value(-1)
print accumulator(3)
print state.get_value()
>>> 
0
1
301
-1
2

state的值在调用函数之后才刷新。而且可以定义多个函数共用同一个共享变量,例如这个减法器。

decrementor = function([inc], state,updates=[(state, state-inc)])
print decrementor(2)
print state.get_value()
>>> 
2
0

如果在某个函数中,共用了这个共享变量,但是又不想变动它的值,那么可以使用given参数替代这个变量。而旧的state不发生变化。

fn_of_state = state * 2 + inc
foo = T.scalar(dtype=state.dtype)
skip_shared = function([inc, foo],fn_of_state,
                           givens=[(state,foo)])
print skip_shared(1, 3)
print state.get_value()
>>> 
7
0

 

产生随机数

和C中的srand()一样,都是伪随机数。

from theano import function
from theano.tensor.shared_randomstreamsimport RandomStreams
srng = RandomStreams(seed=234)#种子
rv_u = srng.uniform((2,2))#均匀分布
rv_n = srng.normal((2,2))#正态分布
f = function([], rv_u)#每次调用,每次都会更新
g = function([], rv_n,no_default_updates=True)#如果以后一直用这组随机数,就不再更新
nearly_zeros = function([], rv_u + rv_u- 2 * rv_u)
print nearly_zeros()#函数每次执行只获得一个随机数,即使表达式里面有3个随机数

种子流:上述2个随机变量,可以全局设定同一个种子,也可以是分别设定。

#分别设置,使用.rng.set_value()函数
rng_val =rv_u.rng.get_value(borrow=True) # Get the rng for rv_u
rng_val.seed(89234) # seeds thegenerator
rv_u.rng.set_value(rng_val,borrow=True)
#全局设置,使用.seed()函数
srng.seed(902340)

函数间共享流

state_after_v0 =rv_u.rng.get_value().get_state()#保存调用前的state
nearly_zeros()       # this affects rv_u's generator
v1 = f()#第一个调用,之后state会变化
rng = rv_u.rng.get_value(borrow=True)
rng.set_state(state_after_v0)#为其state还原
rv_u.rng.set_value(rng, borrow=True)
v2 = f()             # v2 != v1输出更新后state对应的随机数
v3 = f()             # v3 == v1再次更新又还原成原来的state了

 

在2张Theano图间复制状态

import theano
import numpy
import theano.tensor as T
from theano.sandbox.rng_mrg importMRG_RandomStreams
from theano.tensor.shared_randomstreamsimport RandomStreams
 
class Graph():
   def __init__(self, seed=123):
       self.rng = RandomStreams(seed)
       self.y = self.rng.uniform(size=(1,))
 
g1 = Graph(seed=123)
f1 = theano.function([], g1.y)
 
g2 = Graph(seed=987)
f2 = theano.function([], g2.y)
 
print 'By default, the two functionsare out of sync.'
print 'f1() returns ', f1()
print 'f2() returns ', f2()
#输出不同的随机值
def copy_random_state(g1, g2):
   if isinstance(g1.rng, MRG_RandomStreams):
#类型判断:其第一个参数为对象,第二个为类型名或类型名的一个列表。其返回值为布尔型。
       g2.rng.rstate = g1.rng.rstate
   for (su1, su2) in zip(g1.rng.state_updates, g2.rng.state_updates):#打包
       su2[0].set_value(su1[0].get_value())#赋值
 
print 'We now copy the state of thetheano random number generators.'
copy_random_state(g1, g2)
print 'f1() returns ', f1()
print 'f2() returns ', f2()
#输出相同的随机值
>>> 
By default, the two functions are outof sync.
f1() returns  [ 0.72803009]
f2() returns  [ 0.55056769]
We now copy the state of the theanorandom number generators.
f1() returns  [ 0.59044123]
f2() returns  [ 0.59044123]


欢迎参与讨论并关注本博客微博以及知乎个人主页后续内容继续更新哦~

转载请您尊重作者的劳动,完整保留上述文字以及文章链接,谢谢您的支持!

发布了54 篇原创文章 · 获赞 86 · 访问量 47万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章