C3 - Week 4: Question about masking in the assignment (in TripletLossFn)

I have completed the assignment, but I am having trouble understanding how the masking portion works in TripletLossFn.

mask_exclude_positives has two masks; these masks are implemented with the “or” (|) operator. After, in negative_without_positive, we multiply mask_exclude_positives by a number and then subtract it from an array. How does the “or” operator work with arithmetic? When we multiply negative_without_positive by a number, does this mean both masks are multiplied? Does the “or” operator mean that the masks are applied sequentially? Would that even make sense, since they masks work on two different parts of the array?

In general, don’t understand how | would work in this part of the function. This seems like a pretty powerful concept, but I am not understanding it.

Hi @Harvey_Wang

The “or” operation is pretty simple in mask_exclude_positives it just checks both masks if there is TRUE value in any of them.

When you multiply TRUE value by a number you get that number, 0 everywhere else.

It might be more informative to visualize the operations of your question:

This is a part from TripletLossFn calculation when I wanted to check the " Expected Output" of that exercise. (Where I think the course creators chose a poor batch dimension of 2, so I wanted to check the inner workings when the batch_size is 4).

If you might be interested in full calculations:

So here is the original exercise test (with batch size 2, I remove the solution code lines):

And here is I think better example:



I am also trying to understand what to do here. The first mask is of negative_zero_on_duplicate itself?

1 Like

That’s correct - the negative_zero_on_duplicate results in zeroes on the diagonal - in other words, assign zero scores for “itself”.

1 Like