2014-05-17 18 views
2

我使用theano函數,並且想要使用givens來迭代所有輸入樣本。代碼如下:Theano函數通過'givens'屬性引發ValueError

index = T.scalar('index') 
train_set = np.array([[0.2, 0.5, 0.01], [0.3, 0.91, 0.4], [0.1, 0.7, 0.22], 
         [0.7, 0.54, 0.2], [0.1, 0.12, 0.3], [0.2, 0.52, 0.1], 
         [0.12, 0.08, 0.4], [0.02, 0.7, 0.22], [0.71, 0.5, 0.2], 
         [0.1, 0.42, 0.63]]) 
train = function(inputs=[index], outputs=cost, updates=updates, 
       givens={x: train_set[index]}) 

它最終會引發錯誤:

ValueError: setting an array element with a sequence. 

你能告訴我爲什麼,以及如何解決這個問題?

+0

可能重複[ ValueError:使用序列設置數組元素](http://stackoverflow.com/questions/4674473/valueerror-setting-an-array-element-with-a-sequence) – aIKid

+1

我不認爲它是重複的問題你提供的 – BoscoTsang

+0

這個錯誤是一個普遍的錯誤,並且在我關聯的qeustion的答案中提到了原因。 – aIKid

回答

4

問題是這樣的:train_set[index]

這裏train_set是numpy的ndarray和索引Theano變量。 NumPy不知道如何使用Theano變量。你必須train_set轉換爲Theano變量如共享變量:

train_set = theano.shared(train_set) 

你還需要改變你的索引Theano的聲明不索引支持真正的價值:的

index = T.iscalar()