Python,元组索引必须是整数,而不是元组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39839034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:47:50  来源:igfitidea点击:

Python, tuple indices must be integers, not tuple?

pythonindexingtypestuples

提问by Scorch

So, I'm not entirely sure what's going on here, but for whatever reason Python is throwing this at me. For reference, it's part of a small neural network I'm building for fun, but it uses a lot of np.array and such, so there's a lot of matrices being thrown around, so I think it's creating some sort of data type clash. Maybe somebody can help me figure this out, because I've been staring at this error for too long without being able to fix it.

所以,我不完全确定这里发生了什么,但无论出于何种原因,Python 都向我抛出了这个问题。作为参考,它是我为了好玩而构建的一个小型神经网络的一部分,但它使用了很多 np.array 等,所以有很多矩阵被抛出,所以我认为它造成了某种数据类型冲突. 也许有人可以帮我解决这个问题,因为我一直盯着这个错误太久而无法修复它。

#cross-entropy error
#y is a vector of size N and output is an Nx3 array
def CalculateError(self, output, y): 

    #calculate total error against the vector y for the neurons where output = 1 (the rest are 0)
    totalError = 0
    for i in range(0,len(y)):
       totalError += -np.log(output[i, int(y[i])]) #error is thrown here

    #now account for regularizer
    totalError+=(self.regLambda/self.inputDim) * (np.sum(np.square(self.W1))+np.sum(np.square(self.W2)))     

    error=totalError/len(y) #divide ny N
    return error

EDIT: Here's the function that returns the output so you know where that came from. y is a vector of length 150 that is taken directly from a text document. at each index of y it contains an index either 1,2, or 3:

编辑:这是返回输出的函数,所以你知道它来自哪里。y 是一个长度为 150 的向量,它直接取自文本文档。在 y 的每个索引处,它包含一个索引 1,2 或 3:

#forward propogation algorithm takes a matrix "X" of size 150 x 3
def ForProp(self, X):            
        #signal vector for hidden layer
        #tanh activation function
        S1 = X.dot(self.W1) + self.b1
        Z1 = np.tanh(S1)

        #vector for the final output layer
        S2 = Z1.dot(self.W2)+ self.b2
        #softmax for output layer activation
        expScores = np.exp(S2)
        output = expScores/(np.sum(expScores, axis=1, keepdims=True))
        return output,Z1

回答by lejlot

Your outputvariable is not a N x 4matrix, at least not in python typessense. It is a tuple, which can only be indexed by a single number, and you try to index by tuple (2 numbers with coma in between), which works only for numpy matrices. Print your output, figure out if the problem is just a type (then just convert to np.array) or if you are passing something completely different (then fix whatever is producing output).

您的output变量不是N x 4矩阵,至少在python 类型意义上不是。它是一个tuple,它只能由单个数字索引,并且您尝试通过元组(中间有昏迷的 2 个数字)进行索引,这仅适用于 numpy 矩阵。打印您的输出,确定问题是否只是一种类型(然后只需转换为 np.array),或者您是否正在传递完全不同的内容(然后修复正在产生的任何内容output)。

Example of what is happening:

正在发生的事情的例子:

import numpy as np
output = ((1,2,3,5), (1,2,1,1))

print output[1, 2] # your error
print output[(1, 2)] # your error as well - these are equivalent calls

print output[1][2] # ok
print np.array(output)[1, 2] # ok
print np.array(output)[(1, 2)] # ok
print np.array(output)[1][2] # ok