Python TypeError:切片索引必须是整数

Python TypeError:切片索引必须是整数,python,theano,Python,Theano,调用test_model时,它给出以下错误: TypeError: slice indices must be integers or None or have an __index__ method 但是我用一个整数(一个特定的批处理)调用test_模型。Inputtest是浮点列表,Labelts是整数向量。我不确定是什么问题 def optimize(learning_rate = 0.1,n_epochs = 1000, batch_size = 600): n_train_b

调用test_model时,它给出以下错误:

TypeError: slice indices must be integers or None or have an __index__ method
但是我用一个整数(一个特定的批处理)调用test_模型。Inputtest是浮点列表,Labelts是整数向量。我不确定是什么问题

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]})

有时您会得到一个看起来像整数的值。我做了两个实验来帮助你理解

一,

输出:TypeError:列表索引必须是整数或片,而不是str

二,

产出:3

因此,您必须确保输入列表或其他数据结构的索引应该是整数。但它并不总是整数。若你们使用字典,你们应该输入字符串。这取决于您使用的数据结构

希望有帮助。:)

提到这样做

index = T.lscalar('index')  
将索引返回到[小]批

我认为替换:

x --> inputstest[index * batch_size:(index + 1) * batch_size]
y --> labeltest[index * batch_size:(index + 1) * batch_size]

在您的图形中,由于您可能需要使用“批启动”和“批停止”,并且您没有将学习速率传递给您的函数,因此无法正常工作

你能发布完整的堆栈跟踪吗?你能清楚地解释你的问题吗?什么是
T.lscalar('index')
?可能性是,它不是
int
-like,将它乘以
int
不会产生
int
-like。它是labeltes还是labeltest?确保这不是打字错误。如果没有更多的细节,可能无法看到发生了什么。我的猜测是,
inputstest
和/或
labeltes
不是共享变量,但它们需要共享。如果它们是普通的numpy数组,那么就不能使用Theano符号索引(即符号表达式
index*batch\u size:(index+1)*batch\u size
)对numpy数组进行切片。我不认为这是问题所在-可以用示例中的list(map(int,list1))解决这个问题。我相信这里的问题与参数给定有关。抱歉误解。我认为他们的情况相似。
index = T.lscalar('index')  
x --> inputstest[index * batch_size:(index + 1) * batch_size]
y --> labeltest[index * batch_size:(index + 1) * batch_size]