简体   繁体   中英

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?

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM