Course 4 Week 4 A1: issue at triplet_loss

something wrong in my code, I’m stucking at final testcase:

y_pred_perfect = ([[1., 1.], [2., 0.]], [[0., 3.], [1., 1.]], [[1., 0.], [0., 1.,]])
loss = triplet_loss(y_true, y_pred_perfect, 1)
if (loss == 4.):
    raise Exception('Perhaps you are not using axis=-1 in reduce_sum?')
assert loss == 5, "Wrong value. Check your implementation"

Here are my step results:

#Step 1: Compute the (encoding) distance between the anchor and the positive
pos_dist: [5. 2.]
#Step 2: Compute the (encoding) distance between the anchor and the negative
neg_dist: [1. 5.]
# Step 3: subtract the two previous distances and add alpha.
basic_loss: [ 5. -2.]
# Step 4: Take the maximum of basic_loss and 0.0. Sum over the training examples.
loss: tf.Tensor(3.0, shape=(), dtype=float32) # this result much be 5

What step did I do wrong?

turns out the issue at step 4 which i did:
loss = tf.maximum(tf.reduce_sum(basic_loss), 0.0)
that’s wrong formula!

1 Like

But what is the correct formula to use?

Edit: It turns out that the maximum function is applied first to the basic_loss, all of which is then included in the reduce_sum function.

Hello, I am stuck on the last case as well and I am doing what @Tom_Pham says:
image
Any help would be appreciated.

I need more context to try to help you here. Did you use tf.reduce_sum, and if yes then what did you include in the brackets?

The typical mistake here is to take the maximum of the sum, but what they are asking for is the sum of the maxima. You have to study the formula closely to understand what it means. The notation is a bit unusual. I added some instrumentation to my code similar to yours and here’s what I get:

pos_dist = [3122.1926 3377.6548 3365.9343]
neg_dist [3905.9553 2850.595  3434.0498]
shape of basic loss (3,)
basic loss [-783.5627   527.2598   -67.91548]
loss 527.2598266601562
loss = tf.Tensor(527.2598, shape=(), dtype=float32)
pos_dist = 0.0
neg_dist 0.0
shape of basic loss ()
basic loss 5.0
loss 5.0
pos_dist = 0.0
neg_dist 2.0
shape of basic loss ()
basic loss 1.0
loss 1.0
pos_dist = 2.0
neg_dist 0.0
shape of basic loss ()
basic loss 2.0
loss 2.0
pos_dist = 0.0
neg_dist 0.0
shape of basic loss ()
basic loss -2.0
loss 0.0
pos_dist = [0. 0.]
neg_dist [2. 2.]
shape of basic loss (2,)
basic loss [1. 1.]
loss 2.0
pos_dist = [5. 2.]
neg_dist [1. 5.]
shape of basic loss (2,)
basic loss [ 5. -2.]
loss 5.0

Hmmm, it looks like the problem is more basic: your positive and negative distances are already different than what I show.

1 Like