Error in compute_total_loss_test(target, Y):

def compute_total_loss_test(target, Y):
pred = tf.constant([[ 2.4048107, 5.0334096 ],
[-0.7921977, -4.1523376 ],
[ 0.9447198, -0.46802214],
[ 1.158121, 3.9810789 ],
[ 4.768706, 2.3220146 ],
[ 6.1481323, 3.909829 ]])
minibatches = Y.batch(2)
for minibatch in minibatches:
result = target(pred, tf.transpose(minibatch))
break

print(result)
assert(type(result) == EagerTensor), "Use the TensorFlow API"
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?"

print("\033[92mAll test passed")

compute_total_loss_test(compute_total_loss, new_y_train )

Error:

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

AssertionError Traceback (most recent call last)
in
17 print(“\033[92mAll test passed”)
18
—> 19 compute_total_loss_test(compute_total_loss, new_y_train )

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?”
16
17 print(“\033[92mAll test passed”)

AssertionError: Test does not match. Did you get the reduce sum of your loss functions?

I assume you’re working on the Week 3 assignment “Tensorflow_introduction”?

Here is a summary of the hints for the compute_total_loss() function:

  • use tf.reduce_sum()
  • use tf.keras.losses.categorical_crossentropy()
  • use the labels and logits, both of them transposed using tf.transpose().
  • use from_logits = True

Ya

Is the following code correct??

{moderator edit: code removed}

Please don’t post your code on the forum. That’s not allowed by the Code of Conduct.

If you do post your code, please use the “preformatted text” tag, so the comments don’t look like like bold-font Markdown.

No, that code isn’t correct. You need to only write one line of code. It should include the hints I mentioned.

  • The hints did not include using tf.reduce_mean().
  • And you haven’t use tf.transpose() on the logits or labels.