2

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?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
BoscoTsang
  • 394
  • 1
  • 14
  • possible duplicate of [ValueError: setting an array element with a sequence](http://stackoverflow.com/questions/4674473/valueerror-setting-an-array-element-with-a-sequence) – aIKid May 17 '14 at 10:20
  • 1
    I don't think it's duplicate of the question that you provide – BoscoTsang May 17 '14 at 10:50
  • The error is a general one, and the cause is mentioned in the answer of the qeustion I linked. – aIKid May 17 '14 at 11:05
  • I can't get any idea to solve it by what you linked. My train_set is a numpy ndarray whose shape is (10,3) and all items is float. It seems that it doesn't accord with all the probably problem in the question what you linked. – BoscoTsang May 17 '14 at 11:48

1 Answers1

4

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()
nouiz
  • 5,071
  • 25
  • 21