Course 4, Assignment 1, Triple loss

Hi,
I have gotten the error below, after completing the first exercise of assignment 1.
Could you please give me a hint to resolve the error?
Thanks.

This is an interesting case. I added some print statements to my code to see the types of the values passed in and here’s what I get:

type(anchor) <class 'tensorflow.python.framework.ops.EagerTensor'>
type(positive) <class 'tensorflow.python.framework.ops.EagerTensor'>
shape of basic loss (3,)
basic loss [-783.5627   527.2598   -67.91548]
loss 527.2598266601562
loss = tf.Tensor(527.2598, shape=(), dtype=float32)
type(anchor) <class 'list'>
type(positive) <class 'list'>
shape of basic loss ()
basic loss 5.0
loss 5.0
type(anchor) <class 'list'>
type(positive) <class 'list'>
shape of basic loss ()
basic loss 1.0
loss 1.0
type(anchor) <class 'list'>
type(positive) <class 'list'>
shape of basic loss ()
basic loss 2.0
loss 2.0
type(anchor) <class 'list'>
type(positive) <class 'list'>
shape of basic loss ()
basic loss -2.0
loss 0.0
type(anchor) <class 'list'>
type(positive) <class 'list'>
shape of basic loss (2,)
basic loss [1. 1.]
loss 2.0
type(anchor) <class 'list'>
type(positive) <class 'list'>
shape of basic loss (2,)
basic loss [ 5. -2.]
loss 5.0

So what you see is that the test cases are constructed in different ways. Sometimes the inputs are Tensors and sometimes they are lists. It turns out that using the explicit “-” operator for subtraction works fine if the inputs are tensors, but it fails if the inputs are lists.

I happened to use tf.subtract instead of the overloaded “-” for the subtraction and apparently it is smart enough to handle either type of inputs successfully.

1 Like