'length not know' 在 theano.function

'length not know' in theano.function

我想对深度学习教程中的 logistic_sgd.py 进行一些更改。详情如下。

原代码:

index = T.lscalar()    
x = T.matrix('x')  
y = T.ivector('y')
train_model = theano.function(
        inputs=[index],
        outputs=classifier.errors(y),
        givens={
            x: test_set_x[index * train_batch_size: (index + 1) * train_batch_size],
            y: test_set_y[index * train_batch_size: (index + 1) * train_batch_size]
        }
    )

我的代码:

index = T.lscalar()  
idx_list = T.lvector()  

x = T.matrix('x')  
y = T.ivector('y') 

train_model = theano.function(
        inputs=[idx_list],
        outputs=cost,
        updates=updates,
        givens={
            x: train_set_x[[i for i in idx_list]],
            y: train_set_y[[i for i in idx_list]]
        }
    )

我想使用向量 idx_list 中 train_set_x 和 train_set_y 的索引,而不是原始代码中的索引 index,但我得到以下错误:

Traceback (most recent call last):
  File "Y:/ARBM/code/logistic_sgd_rand.py", line 169, in <module>
    train_batch_size=5, select_batch_size=10)
  File "Y:/ARBM/code/logistic_sgd_rand.py", line 92, in sgd_optimization_mnist
    x: train_set_x[[i for i in idx_list]],
  File "C:\Anaconda\lib\site-packages\theano\tensor\var.py", line 433, in __iter__
    for i in xrange(theano.tensor.basic.get_vector_length(self)):
  File "C:\Anaconda\lib\site-packages\theano\tensor\basic.py", line 3773, in get_vector_length
    raise ValueError("length not known")
ValueError: length not known

问题是您以不受支持的方式将 Python 与符号 Theano 代码混合。

而不是

x: train_set_x[[i for i in idx_list]],
y: train_set_y[[i for i in idx_list]]

你需要

x: train_set_x[idx_list],
y: train_set_y[idx_list]

下面是一个更详细地演示更改的完整示例:

import numpy
import theano
import theano.tensor as T


def v1(all_x):
    batch_size = 3
    index = T.lscalar()
    x_part = T.vector()
    f = theano.function(
        inputs=[index],
        outputs=x_part,
        givens={
            x_part: all_x[index * batch_size: (index + 1) * batch_size]
        }
    )
    print f(1)


def v2_broken(all_x):
    idx_list = T.lvector()
    x_part = T.vector()
    f = theano.function(
        inputs=[idx_list],
        outputs=x_part,
        givens={
            x_part: all_x[[i for i in idx_list]]
        }
    )
    print f([2, 4, 6, 8])


def v2_fixed(all_x):
    idx_list = T.lvector()
    x_part = T.vector()
    f = theano.function(
        inputs=[idx_list],
        outputs=x_part,
        givens={
            x_part: all_x[idx_list]
        }
    )
    print f([2, 4, 6, 8])


def main():
    all_x = theano.shared(-numpy.arange(10, dtype=theano.config.floatX))
    v1(all_x)
    # v2_broken(all_x)  # raises ValueError: length not known
    v2_fixed(all_x)


main()