gpt4 book ai didi

python - Theano:在编译函数内建立索引(GPU)

转载 作者:太空宇宙 更新时间:2023-11-03 18:20:32 27 4
gpt4 key购买 nike

由于 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)

有人可以帮我吗?

哇:这个问题是在我向 Google 结果排名前 4 的“Theano indexing gpu”询问后 9 分钟提出的。 O_o

最佳答案

看看: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

关于python - Theano:在编译函数内建立索引(GPU),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24229361/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com