C2 W3 - Ex. 6 Compute Total loss - Failing Tests

I am stuck in compute_total_loss function, I have searched and can not find resolution. All previous Tests have passed, Test code that is failing below.

tf.Tensor([0.25361034 0.5566767 ], shape=(2,), dtype=float32)
ValueError                                Traceback (most recent call last)
<ipython-input-39-0a8e136fceea> in <module>
     17     print("\033[92mAll test passed")
---> 19 compute_total_loss_test(compute_total_loss, new_y_train )

<ipython-input-39-0a8e136fceea> in compute_total_loss_test(target, Y)
     13     print(result)
     14     assert(type(result) == EagerTensor), "Use the TensorFlow API"
---> 15     assert (np.abs(result - (0.50722074 + 1.1133534) / 2.0) < 1e-7), "Test does not match. Did you get the reduce sum of your loss functions?"
     17     print("\033[92mAll test passed")

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Expected output

tf.Tensor(0.810287, shape=(), dtype=float32)

I have read through forums and can not deterrmine why this test is failing. I have restarted kernel, looked at one_hot_matrix function, and done transpose, but no joy. Hope this is not an issue unique to me, and it has been seen before.

Are you using tf.keras.losses.categorical_crossentropy and setting from_logits=True?

Yes, I am.

Note that your return value is an array, but it’s supposed to be a scalar, right? Did you do the “sum” as the instructions say?

Here’s the “checklist” for common errors on this function.


Thank you, tf.reduce_sum seemed to do the trick along with some other clean up.


Phil M.

Thank you, for your response, I was using both of the items below. Paul gave hing that I was missing tf.reduce_sum, and adding that funciton, and some other minor tweaks I was able to successful pass test. THanks again.