Theano-共享变量

Theano-共享变量 (Theano - Shared Variables)

Many a times, you would need to create variables which are shared between different functions and also between multiple calls to the same function. To cite an example, while training a neural network you create weights vector for assigning a weight to each feature under consideration. This vector is modified on every iteration during the network training. Thus, it has to be globally accessible across the multiple calls to the same function. So we create a shared variable for this purpose. Typically, Theano moves such shared variables to the GPU, provided one is available. This speeds up the computation.

很多时候,您将需要创建在不同函数之间以及在同一函数的多个调用之间共享的变量。 举一个例子,在训练神经网络时,您将创建权重向量,以为要考虑的每个特征分配权重。 在网络训练期间的每次迭代中都会修改此向量。 因此,必须在对同一函数的多次调用中全局访问它。 因此,我们为此目的创建一个共享变量。 通常,Theano将此类共享变量移至GPU(如果有)。 这样可以加快计算速度。

句法 (Syntax)

You create a shared variable you use the following syntax −

您使用以下语法创建共享变量-


import numpy
W = theano.shared(numpy.asarray([0.1, 0.25, 0.15, 0.3]), 'W')

(Example)

Here the NumPy array consisting of four floating point numbers is created. To set/get the W value you would use the following code snippet −

在这里,将创建由四个浮点数组成的NumPy数组。 要设置/获取W值,您将使用以下代码片段-


import numpy
W = theano.shared(numpy.asarray([0.1, 0.25, 0.15, 0.3]), 'W')
print ("Original: ", W.get_value())
print ("Setting new values (0.5, 0.2, 0.4, 0.2)")
W.set_value([0.5, 0.2, 0.4, 0.2])
print ("After modifications:", W.get_value())

输出量 (Output)


Original: [0.1 0.25 0.15 0.3 ]
Setting new values (0.5, 0.2, 0.4, 0.2)
After modifications: [0.5 0.2 0.4 0.2]

翻译自: https://www.tutorialspoint.com/theano/theano_shared_variables.htm

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值