1

What does the following code do? (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

EDIT: This is not about how slices update the arrays they are from inplace, as y is not a slice of grad, this question is about the syntax of NumPy.

Clebo Sevic
  • 581
  • 1
  • 7
  • 17
  • 1
    Does this answer your question? [- vs -= operators with numpy](https://stackoverflow.com/questions/9047111/vs-operators-with-numpy) – alex Oct 06 '21 at 10:46
  • Feel free to edit the title, or tell me, what the correct search term would be, because i am unsure my title fits the bill perfectly – Clebo Sevic Oct 06 '21 at 10:46
  • @alex Kind of, but i still do not understand what it does ... sry – Clebo Sevic Oct 06 '21 at 10:48
  • it subtracts 1 from a subset of elements of `grad`. The indexing determines which. – hpaulj Oct 06 '21 at 15:00

1 Answers1

1
grad[range(m),y] -= 1 # It is the same as subtracting X[i,j] when j==y[i].
Mahi
  • 332
  • 3
  • 11
  • I just realised, that X is a matrix, in my case x was always just a vector. PLease correct me if a am wrong, but does this mean, that in `grad`, every column will get `y` subtracted from it? – Clebo Sevic Oct 06 '21 at 14:42
  • Yes exactly as you said. – Mahi Oct 07 '21 at 04:18