theano —— shared, function(outputs, updates, givens)

  • theano 下的 functions (from theano import function)的两个重要性质:
    • 多个 outputs;
    • 多个 updates;

1. shared

  • shared 与 theano.tensor.matrix()/vector() 的区别在于,tensor.matrix/vector 符号变量不需要初始化,而 shared 变量则需要初始化赋值
>>> import theano 
>>> import theano.tensor as T  

>>> state = theano.shared(0) 
>>> type(state)
theano.tensor.sharedvar.ScalarSharedVariable
>>> state.get_value(borrow=True)
array(0)
            # shared 型变量,通过 get_value 取出其值;

>>> x = T.iscalar('x')
>>> type(x)
theano.tensor.var.TensorVariable
    # tensor value(thenao.tensor模块下的变量?)是没有get_value成员函数的

>>> type(x+state)
theano.tensor.var.TensorVariable

2. updates

>>> counter = function([x], state, updates=[(state, state+x)])
>>> counter(5)
>>> state.get_value(borrow=True)
5
>>> counter(6)
>>> state.get_value(borrow=True)
11

不使用可选参数updates:

>>> state += x
                        # 此时state的类型已自动转换为tensor value,不再具有get_value的成员
>>> counter = theano.function([x], state)
>>> counter(5)
array(5)
>>> state.get_value()
AttributeError: 'TensorVariable' object has no attribute 'get_value'

theano.function 的 updates, 二元 tuple 构成的 list

[(a, a+1), (b, b+1)]

其含义其实为,a = a+1, b = b+1;

所以常见与,梯度下降算法中的权值的更新,如下:

updates = [(W, W-eta*delta_W),
           (b, b-eta*delta_b)]

3. givens

test_model = theano.function(
    inputs=[idx],
    outputs=layer3.error(y),
    givens={
        x: test_set_x[idx*batch_size:(idx+1)*batch_size],
        y: test_set_y[idx*batch_size:(idx+1)*batch_size]
    }
)
valid_model = theano.function(
    inputs=[idx],
    outputs=layer3.error(y),
    givens={
        x: valid_set_x[idx*batch_size:(idx+1)*batch_size],
        y: valid_set_y[idx*batch_size:(idx+1)*batch_size]
    }
)
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

五道口纳什

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值