BoscoTsang
BoscoTsang

Reputation: 394

Theano function raises ValueError with 'givens' attribute

I use theano function and want to use givens to iterate all the input samples. The code is as below:

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]})

It eventually raises an error:

ValueError: setting an array element with a sequence.

Could you tell me why, and how to solve the problem?

Upvotes: 2

Views: 1089

Answers (1)

nouiz
nouiz

Reputation: 5071

The problem is this: train_set[index]

Here train_set is a numpy ndarray and index a Theano variable. NumPy don't know how to work with Theano variables. You must convert train_set to a Theano variable like a shared variable:

train_set = theano.shared(train_set)

You also need to change your declaration of index as Theano don't support real value for index:

index = T.iscalar()

Upvotes: 4

Related Questions