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)
grad[range(m),y] -= 1
grad = grad/m
return grad
此外,我也尝试通过使用整数替换y(matrix):import numpy as np
arr = np.array([[1,2,3], [4,5,6], [7,8,9]])
print(arr)
arr[range(2), 1] = 0
print("after range: ", arr)
结果我得到了:[[1 2 3]
[4 5 6]
[7 8 9]]
after range: [[1 0 3]
[4 0 6]
[7 8 9]]
有人可以解释一下range()函数在这里做什么?谢谢!