[英]Theano shared variable update causes `ValueError: length not known`
最少的示例代碼:
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
此錯誤的原因是什么?
Updates
必須包含a list of pairs
。 請參閱有關使用共享變量的官方教程。
正確的代碼:
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.