1

由于 Theano 允许通过简单地定义必须更新的内存区域以及应该如何完成来更新显卡 DRAM 上的内存,我想知道以下是否可行(恕我直言)。

我有一个 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 名中询问“ Theano indexing gpu ”之后的 9 分钟。o_o

4

1 回答 1

1

看看:如何在 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
于 2014-06-20T14:22:41.983 回答