具有完整向量的 Numpy 数组索引

Numpy array indexing with complete vector

以下代码的作用是什么? (grad[range(m),y] -= 1)

def delta_cross_entropy(X,y):
    """
    X is the output from fully connected layer (num_examples x num_classes)
    y is labels (num_examples x 1)
        Note that y is not one-hot encoded vector. 
        It can be computed as y.argmax(axis=1) from one-hot encoded vectors of labels if required.
    """
    m = y.shape[0]
    grad = softmax(X)
# What does this do? Does it subtract y from grad? (As that is what is supposed to happen)
    grad[range(m),y] -= 1
    grad = grad/m
    return grad

编辑: 这与切片如何从原地更新数组无关,因为 y 不是 grad 的切片,这个问题关于NumPy的语法。

grad[range(m),y] -= 1 # It is the same as subtracting X[i,j] when j==y[i].