Question 3/13 v3 lecture 9

At times we want to take values from specific locations in an array and sum them up. For example, this scenario arises when calculating the cross entropy loss. On one side we have a matrix of probabilities, on the other an array of one hot encoded variables where 1s signify values of interest.

1) One way to go about this would be to perform element wise multiplication of probabilities and one hot encoded variables and sum the results.

2) Another option would be to index into the probabilities array using the position of 1s in the one_hot_encoded array and perform the summation.

The code above demonstrates both approaches. Which one is faster?


approach #2 is faster

Relevant part of lecture

supplementary material

Pytorch implements integer array indexing as in numpy. You can read more about how it works [here].(