Theano:在编译函数内索引(GPU)

1 投票
1 回答
517 浏览
提问于 2025-04-18 09:50

因为Theano可以通过简单地定义哪些内存区域需要更新以及如何更新来更新显卡上的内存,所以我在想以下操作是否可能(我认为应该可以)。

我有一个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)

有没有人能帮我解决这个问题?

哇:这个问题在我提问后9分钟,就已经在谷歌搜索“Theano indexing gpu”的前四名结果中出现了。O_o

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

撰写回答