由于Theano允许通过简单地定义哪个内存区域必须更新以及如何更新来更新显卡DRAM上的内存,我想知道以下内容是否可行(imho)应该是)。
我有一个2x5随机初始化矩阵,第一列将用起始值初始化。我想编写一个依赖于前一列的函数,并根据任意计算更新下一列。
我认为这段代码解释得非常好:
注意:此代码不正在运行,它只是一个例子:
from theano import function, sandbox, shared
import theano.tensor as T
import numpy as np
reservoirSize = 2
samples = 5
# To initialize _mat first column
_vec = np.asarray([1 for i in range(reservoirSize)], np.float32)
# Random matrix, first column will be initialized correctly (_vec)
_mat = np.asarray(np.random.rand(reservoirSize, samples), np.float32)
_mat[:,0] = _vec
print "Init:\n",_mat
_mat = shared(_mat)
idx = T.iscalar()
test = function([idx], updates= {
# The indexing causes the problem here. Imho there should be
# a way to do something like this:
# update: _mat[:, idx] = _max[:, idx-1] * 2
_mat[:,idx]:sandbox.cuda.basic_ops.gpu_from_host(_mat[:,idx-1] * 2)
})
for i in range(1, samples):
test(i)
print "Done:\n",_mat
我想要的输出是:
Init:
[[ 1. 0.62166548 0.17463242 0.00858122 0.59709388]
[ 1. 0.52690667 0.20800008 0.86816955 0.43518791]]
Done:
[[ 1. 2. 4. 8. 16. ]
1. 2. 4. 8. 16. ]]
但我得到了
Init:
[[ 1. 0.62166548 0.17463242 0.00858122 0.59709388]
[ 1. 0.52690667 0.20800008 0.86816955 0.43518791]]
Traceback (most recent call last):
File "/home/snooc/workspace/eclipse-python/Bachelorarbeit/theano/test.py", line 20, in <module>
_mat[:,idx]:sandbox.cuda.basic_ops.gpu_from_host(_mat[:,idx-1] * 2) })
File "/usr/lib/python2.7/site-packages/theano/compile/function.py", line 223, in function
profile=profile)
File "/usr/lib/python2.7/site-packages/theano/compile/pfunc.py", line 490, in pfunc
no_default_updates=no_default_updates)
File "/usr/lib/python2.7/site-packages/theano/compile/pfunc.py", line 194, in rebuild_collect_shared
store_into)
TypeError: ('update target must be a SharedVariable', Subtensor{::, int32}.0)
有人可以帮助我吗?
哇:这个问题是在谷歌搜索结果排名前4位之后的9分钟,<#> Theano索引gpu &#34;为了我。 O_O
答案 0 :(得分:1)
查看:How can I assign/update subset of tensor shared variable in Theano?
对于您的代码,这转换为:
from theano import function, sandbox, shared
import theano.tensor as T
import numpy as np
reservoirSize = 2
samples = 5
# To initialize _mat first column
_vec = np.asarray([1 for i in range(reservoirSize)], np.float32)
# Random matrix, first column will be initialized correctly (_vec)
_mat = np.asarray(np.random.rand(reservoirSize, samples), np.float32)
_mat[:,0] = _vec
print "Init:\n",_mat
_mat = shared(_mat)
idx = T.iscalar()
test = function([idx], updates= {
# -> instead of
#_mat[:,idx]:sandbox.cuda.basic_ops.gpu_from_host(_mat[:,idx-1] * 2)
# -> do this:
_mat:T.set_subtensor(_mat[:,idx], _mat[:,idx-1]*2)
})
for i in range(1, samples):
test(i)
print "Done:\n",_mat.get_value() # use get_value() here to retrieve the data