我有一個奇怪的錯誤,在編譯Theano中的掃描運算符時我無法理解。 當outputs_info與最後的尺寸等於一個初始化的,我得到這個錯誤:當輸出的最後尺寸等於1時,Theano中掃描的奇怪行爲
TypeError: ('The following error happened while compiling the node', forall_inplace,cpu,
scan_fn}(TensorConstant{4}, IncSubtensor{InplaceSet;:int64:}.0, <TensorType(float32, vector)>),
'\n', "Inconsistency in the inner graph of scan 'scan_fn' : an input and an output are
associated with the same recurrent state and should have the same type but have type
'TensorType(float32, (True,))' and 'TensorType(float32, vector)' respectively.")
而我如果尺寸設置爲任何大於一沒有得到任何錯誤。
這個錯誤發生在gpu和cpu目標上,其中theano 0.7,0.8.0和0.8.2。
這裏是一段代碼重現該錯誤:
import theano
import theano.tensor as T
import numpy as np
def rec_fun(prev_output, bias):
return prev_output + bias
n_steps = 4
# with state_size>1, compilation runs smoothly
state_size = 2
bias = theano.shared(np.ones((state_size),dtype=theano.config.floatX))
(outputs, updates) = theano.scan(fn=rec_fun,
sequences=[],
outputs_info=T.zeros([state_size,]),
non_sequences=[bias],
n_steps=n_steps
)
print outputs.eval()
# with state_size==1, compilation fails
state_size = 1
bias = theano.shared(np.ones((state_size),dtype=theano.config.floatX))
(outputs, updates) = theano.scan(fn=rec_fun,
sequences=[],
outputs_info=T.zeros([state_size,]),
non_sequences=[bias],
n_steps=n_steps
)
# compilation fails here
print outputs.eval()
彙編具有依賴於「state_size」,從而不同的行爲。 是否有解決方案來處理case_size == 1和state_size> 1?
謝謝!這似乎是一個很好的解決方法,即使在我的特殊情況下,ouputs_info事實上依賴於幾個形狀參數,它使得代碼不易讀。我想解釋爲什麼直接輸出信息output_info = T.zeros([1,])解決方案失敗... –
我編輯了答案,以便它包含解釋和另一個解決方案,以幫助您瞭解問題 –
謝謝非常感謝您的幫助!現在我也更清楚了。顯式設置廣播模式使代碼更具可讀性。謝謝 ! –