Predict grader cell issue

Remember what the model’s last layer does:
image
it concatenates the outputs.

So when you call model.predict(*) you get the concatenated output (1, 256) which you now need to split back into v1 and v2. (the v1v2[0] and v1v2[-1] would not work, since it’s the same thing in this case).

You probably have successfully implemented that in Exercise 4 (the classify function). What you need again here is to get:

  • v1.shape - (1, 128)
  • v2.shape - (1, 128)

Here is the entire thing


For (“When will I see you?” and “When can I see you again?”), against which you can check your values. First:

    # Call the predict method of your model and save the output into v1v2
    v1v2 = ?

Output:

v1v2.shape
(1, 256)

v1v2[0, :3]
array([0.03971604, 0.06642354, 0.00687801], dtype=float32)

v1v2[0, 128:131]
array([ 0.16333432, -0.01577964,  0.00418817], dtype=float32)

Second, now you need to split them:

    # Extract v1 and v2 from the model output
    v1 = ?
    v2 = ?

Output:

### v1
v1.shape
(1, 128)

v1[0, :3]
array([0.03971604, 0.06642354, 0.00687801], dtype=float32)

### v2
v2.shape
(1, 128)

v2[0, :3]
array([ 0.16333432, -0.01577964,  0.00418817], dtype=float32)

Then you need to get sum of the element wise products:

    # Take the dot product to compute cos similarity of each pair of entries, v1, v2
    # Since v1 and v2 are both vectors, use the function tf.math.reduce_sum instead of tf.linalg.matmul
    d = ?

Output:

print(d)
<tf.Tensor: shape=(), dtype=float32, numpy=0.8422112>

P.S. even they ask to use tf.math.reduce_sum you could get away with tf.linalg.matmul but you would have to be cognizant of the resulting shape. So, I guess, you better use the reduce_sum.


Lastly, you need to compare against the threshold:

    # Is d greater than the threshold?
    res = ?

Output:

res
<tf.Tensor: shape=(), dtype=bool, numpy=True>

# since 0.8422111 > 0.7

Cheers

1 Like