簡體   English   中英

Theano函數使用'givens'屬性引發ValueError

[英]Theano function raises ValueError with 'givens' attribute

我用theano功能,想用givens遍歷所有的輸入樣本。 代碼如下:

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.

你能告訴我為什么,以及如何解決這個問題嗎?

問題是這樣的: train_set[index]

這里train_set是一個numpy ndarray,並為Theano變量建立索引。 NumPy不知道如何使用Theano變量。 您必須將train_set轉換為Theano變量,例如共享變量:

train_set = theano.shared(train_set)

您還需要更改索引聲明,因為Theano不支持索引的實際值:

index = T.iscalar()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM