Theano 初探(一)

设定T.dscalar设定 theano标量, 转换函数中 string 'x'并没有实质性作用,
其作用在于调试。

这里生成function f后,f就是一个“一般的”python函数,
这与不设定function的另一个调用形式相对应,
z.eval({x: 16.3, y:12.1})

theano 函数还支持张量输出,即多维因变量。
亦可以使用诸如 T.dmatrices('a', 'b')这样的方法一次初始化多个自变量矩阵。

类似于Python functools 中提供的partial函数,theano的数值函数也提供了
类似于partial的默认参数绑定方法,In.
但其行为与partial的实现不同,后者是永久性对函数对象参数进行修改,而前者
提供函数的默认参数。
甚至这种方法可以对函数提供默认参数的别名,这为改变参数的序提供了方便。

theano还提供了可以在若干个(可以是不同)函数调用间共享的变量,shared
这类似于C++中的静态变量。(在机理上)
设定了shared变量后,可以利用update参数实现shared变量的改变。(更新)
这里值得注意的一点是变量的更新是在函数返回之后,否则初值的设定就没有意义
了。
shared变量的意义不仅仅在于如同全局常量的意义,theano定义了这个变量可以
作为输出等,这使得其具备微分等数学性质。这也有运行速度上考虑的意义。

当不希望使用state变量时,可以使用given参数重载使用state的行为,
但这里要注意应当将用于替换的变量与state变量声明为相同类型。(state.dtype)

theano提供了用于函数复制的copy方法,结合swap参数可以对原函数的参数进行替换。
设定copy参数delete_update为True可以将拷贝的函数的update行为删掉。

下面是示例代码:
#from theano import *
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)

print f(2, 3)
print type(f(2, 3))

x = T.dmatrix('x')
y = T.dmatrix('y')
z = x + y
f = function([x, y], z)

print f([[1, 2], [3, 4]], [[10, 20], [30, 40]])

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

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

import theano
import theano.tensor as T 
x = T.dmatrix('x')
s = 1 / (1 + T.exp(-x))
logistic = theano.function([x], s)
print logistic([[0, 1], [-1, -2]])

s2 = (1 + T.tanh(x / 2)) / 2
logistic2 = theano.function([x], s2)
print logistic2([[0, 1], [-1, -2]])

a, b = T.dmatrices('a', 'b')
diff = a - b 
abs_diff = abs(diff)
diff_squared = diff ** 2 
f = theano.function([a, b], [diff, abs_diff, diff_squared])

print f([[1, 1], [1, 1]], [[0, 1], [2, 3]])

from theano import In 
x, y = T.dscalars('x', 'y')
z = x + y 
f = function([x, In(y, value = 1)], z)
print f(33)
print f(33, 2)

x, y, w = T.dscalars('x', 'y', 'w')
z = (x + y) * w 
f = function([x, In(y, value = 1), In(w, value = 2, name = 'w_by_name')], z)
print f(33)
print f(33, 2)
print f(33, 0, 1)
print f(33, w_by_name = 1)
print f(33, w_by_name = 1, y = 0)

from theano import shared 
state = shared(0)
inc = T.iscalar('inc')
accumulator = function([inc], state, updates = [(state, state + inc)],  on_unused_input='ignore')

print state.get_value() 
print accumulator(1)
print state.get_value() 
print accumulator(300)
print state.get_value()

state.set_value(-1)
accumulator(3)
print (state.get_value())

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

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()

state.set_value(10)
new_state = theano.shared(0)
new_accumulator = accumulator.copy(swap = {state: new_state})
print new_accumulator(100)
print new_state.get_value() 
print state.get_value()

null_accumulator = accumulator.copy(delete_updates=True)
print null_accumulator(9000)
print state.get_value()












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值