类型错误:切片索引必须是整数
它给了我下面的错误时test_model叫做:类型错误:切片索引必须是整数
TypeError: slice indices must be integers or None or have an __index__ method
但我打电话test_model一个整数(特定批次)。 Inputtest是浮动列表的列表,标签是一个整数的向量。我不确定问题是什么。
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])
输出:类型错误:列表索引必须是整数或片,而不是str的
2.
list1 = [2,3]
index = '1'
print(list1[int(index)])
输出:3
所以,你必须以确保您输入到列表或其他数据结构的索引应该是整数。但它并不总是整数。如果你使用字典,你应该输入字符串。这取决于您使用的数据结构。
希望它有帮助。 :)
我不认为这是这个问题 - 这个问题可以用你的例子中的list(map(int,list1))来修复。我相信这里的问题有参数。 – jmugz3
对不起,误会。我虽然他们是类似的情况。 –
的Theano documentation提到,这样做
index = T.lscalar('index')
将索引返回到[小]批量
我想更换:
x --> inputstest[index * batch_size:(index + 1) * batch_size]
y --> labeltest[index * batch_size:(index + 1) * batch_size]
在图形中不因为您可能需要使用批量开始和批量停止,并且您没有将自己的学习速度传递给您的功能。
你能发布完整的堆栈跟踪吗 – Selcuk
你能清楚地解释你的问题吗? –
什么是'T.lscalar('index')'?有可能是,它不是'int',并且将其乘以'int'并不会产生类似'int'的东西。 – ShadowRanger