Question 3/13 fast.ai 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?

Answer

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].(https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.indexing.html#integer-array-indexing)