我想在DeepLearning教程中对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]]
}
)
我想使用vector 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
答案 0 :(得分:2)
问题在于您以不受支持的方式将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()