Stuck on C2W3 Assignment: Cost Function

Please do not post any part of codes here on public post. It is against community guidelines. Kindly edit your comments and remove the codes.

You can share a screenshot of your output with the expected output or you can share your error log.

Your output is requiring a shape of (6,2) which is not matching with your shape as the grader clearly mentions
logits – output of forward propagation (output of the last LINEAR unit), of shape (6, num_examples), same for logits.

So recalling what python function will get you the desired shape for logits and labels?

Using which loss is clearly mentioned in the instructions above

GRADED FUNCTION: compute_total_loss

def compute_total_loss(logits, labels):

So this should not create any confusion.

It looks like there are (at least) two problems:

  1. You missed the fact that the labels and logits need to be transposed. Here is a thread with a checklist for this function. Here’s a thread which explains why the transpose is required.
  2. There is an extra dimension on one of your tensors. I assume it’s the labels tensor, since it’s the first operand. Note that the new_y_train value is generated by calling your earlier one_hot_matrix function. So this may indicate a problem with that function that somehow was not caught by the test cases.

I added print statements to my compute_total_loss code to show the shapes of the inputs before any processing of them (e.g. transpose) and here’s what I see:

labels [[0. 1.]
 [0. 0.]
 [0. 0.]
 [0. 0.]
 [0. 0.]
 [1. 0.]]
before logits.shape (6, 2)
before labels.shape (6, 2)

Just to make sure I was clear about things, the (6,2) is the input shape. It needs to be (2,6) by the time you pass it to the TF loss function. But the other point is that you need to figure out how to remedy that extra dimension as well.

As tf.keras.metrics.categorical_crossentropy() accept true_y then pred_y.
make sure you pass first true_label (only have 0 or 1 value) then predicted values, other wise it will do wrong calculations.


both true_y and pred_y should have dimension (# of examples, # of classes )
in next cell both arguments of target ‘target(pred, tf.transpose(minibatch))’ have shape (# of classes, # of example) ,
hear # of classes is 6 and # of example is 2
and ‘target’ is ‘compute_total_loss’