简体   繁体   English

Theano函数使用'givens'属性引发ValueError

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

I use theano function and want to use givens to iterate all the input samples. 我用theano功能,想用givens遍历所有的输入样本。 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] 问题是这样的: train_set[index]

Here train_set is a numpy ndarray and index a Theano variable. 这里train_set是一个numpy ndarray,并为Theano变量建立索引。 NumPy don't know how to work with Theano variables. NumPy不知道如何使用Theano变量。 You must convert train_set to a Theano variable like a shared variable: 您必须将train_set转换为Theano变量,例如共享变量:

train_set = theano.shared(train_set)

You also need to change your declaration of index as Theano don't support real value for index: 您还需要更改索引声明,因为Theano不支持索引的实际值:

index = T.iscalar()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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