2016-03-02 72 views
0

它給了我下面的錯誤時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

你能發佈完整的堆棧跟蹤嗎 – Selcuk

+0

你能清楚地解釋你的問題嗎? –

+0

什麼是'T.lscalar('index')'?有可能是,它不是'int',並且將其乘以'int'並不會產生類似'int'的東西。 – ShadowRanger

回答

0

有時你會得到一個看起來像整數的值。我做了兩個實驗來幫助你理解。

1.

list1 = [2,3] 

index = '1' 

print(list1[index]) 

輸出:類型錯誤:列表索引必須是整數或片,而不是str的

2.

list1 = [2,3] 

index = '1' 

print(list1[int(index)]) 

輸出:3

所以,你必須以確保您輸入到列表或其他數據結構的索引應該是整數。但它並不總是整數。如果你使用字典,你應該輸入字符串。這取決於您使用的數據結構。

希望它有幫助。 :)

+0

我不認爲這是這個問題 - 這個問題可以用你的例子中的list(map(int,list1))來修復。我相信這裏的問題有參數。 – jmugz3

+0

對不起,誤會。我雖然他們是類似的情況。 –

0

Theano documentation提到,這樣做

index = T.lscalar('index') 

將索引返回到[小]批量

我想更換:

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

在圖形中不因爲您可能需要使用批量開始和批量停止,並且您沒有將自己的學習速度傳遞給您的功能。

相關問題