[英]TypeError: slice indices must be integers
调用test_model时,出现以下错误:
TypeError: slice indices must be integers or None or have an __index__ method
但是我用整数(特定批次)调用test_model。 Inputtest是浮点数列表的列表,labeltes是整数的向量。 我不确定是什么问题。
def optimize(learning_rate = 0.1,n_epochs = 1000, batch_size = 600):
n_train_batches = len(inputt)//batch_size
n_val_batches = len(inputsdev)//batch_size
n_test_batches = len(inputstest)//batch_size
rng = numpy.random.RandomState(1234)
index = T.lscalar('index')
x = T.ivector('x')
y = T.ivector('y')
classifier = Regression(x, n_in = 150, n_out = 24)
cost = classifier.negative_log_likelihood(labelt)
test_model = theano.function(inputs = [index], outputs = classifier.errors(y),givens = { x: inputstest[index * batch_size:(index + 1) * batch_size], y : labeltes[index * batch_size:(index + 1) * batch_size]})
有时您会得到一个看起来像整数的值。 我做两个实验来帮助您理解。
1。
list1 = [2,3]
index = '1'
print(list1[index])
输出:TypeError:列表索引必须是整数或切片,而不是str
2。
list1 = [2,3]
index = '1'
print(list1[int(index)])
输出:3
因此,您必须确保输入到列表或其他数据结构的索引应该是整数。 但是它并不总是整数。 如果使用字典,则应输入字符串。 这取决于您使用的数据结构。
希望能帮助到你。 :)
Theano文档提到这样做
index = T.lscalar('index')
将索引返回到[mini]批次
我认为更换:
x --> inputstest[index * batch_size:(index + 1) * batch_size]
y --> labeltest[index * batch_size:(index + 1) * batch_size]
在您的图表中无法正常工作,因为您可能需要使用批处理开始和批处理停止,并且您没有将学习率传递给函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.