theano - theano.function 中的“长度不知道”

标签 theano deep-learning

我想对深度学习教程中的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()

关于theano - theano.function 中的“长度不知道”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33151755/

相关文章:

python - 如何在终端中绘制图表

tensorflow - 在 tensorflow 中实现两个图像之间的交叉熵损失

image-processing - 医学图像分割需要像素值归一化吗?

python - 使用 python pydot 时出错

tensorflow - 计算图与(计算机代数)符号表达式

python - Theano (GPU) 内存占用管理/调试

deep-learning - 在 pytorch 代码中如何在 ResNet 中进行下采样?

python - Pytorch如何将除第一维之外的可变大小的张量相乘

machine-learning - 咖啡 |添加新层以使用训练模型后未知的底部 Blob (微调)

python - 如何处理 pymc3 确定性变量的形状