gpt4 book ai didi

python - Theano共享变量更新导致 `ValueError: length not known`

转载 作者:行者123 更新时间:2023-12-01 05:42:57 24 4
gpt4 key购买 nike

最小示例代码:

import theano as th
import theano.tensor as T
import numpy as np

x = T.dscalars('x')
z = th.shared(np.zeros(2))
updates = [z, z+x]

f1 = th.function(inputs=[x], updates=updates)
f1(3)
print z.get_value()

错误消息:

Traceback (most recent call last):
File "/home/temp2/theano.test.py", line 9, in <module>
f1 = th.function(inputs=[x], updates=updates)
File "/usr/local/lib/python2.7/dist-packages/theano/compile/function.py", line 205, in function
profile=profile)
File "/usr/local/lib/python2.7/dist-packages/theano/compile/pfunc.py", line 460, in pfunc
no_default_updates=no_default_updates)
File "/usr/local/lib/python2.7/dist-packages/theano/compile/pfunc.py", line 191, in rebuild_collect_shared
for (store_into, update_val) in iter_over_pairs(updates):
File "/usr/local/lib/python2.7/dist-packages/theano/tensor/basic.py", line 1610, in __iter__
for i in xrange(get_vector_length(self)):
File "/usr/local/lib/python2.7/dist-packages/theano/tensor/basic.py", line 5210, in get_vector_length
raise ValueError("length not known")
ValueError: length not known

这个错误的原因是什么?

最佳答案

更新必须包含对列表。参见官方教程using shared variables .

正确代码:

import theano as th
import theano.tensor as T
import numpy as np

x = T.dscalars('x')
z = th.shared(np.zeros(2))
updates = [(z, z+x)]

f1 = th.function(inputs=[x], updates=updates)
f1(3)
print z.get_value()

关于python - Theano共享变量更新导致 `ValueError: length not known`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17026496/

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