Course 2, Week 3, one_hot_matrix clarification

If I reshape the one_hot matrix to (depth, ), the tests pass. However, if I reshape the one_hot matrix to (depth, 1), the tests fail. Why is this? I thought we would need to use (depth, 1) to make sure we get a single-column matrix with rows = depth. Basically, I don’t understand why we need to use (depth, ).



the shape of both y_pred and y_true are [batch_size, num_classes],

later on, when you need to calculate the cost using tf.keras.losses.CategoricalCrossentropy  |  TensorFlow v2.14.0

If you reshape to [depth, 1], the shape will be [batch_size, num_classes, 1] which throws an exception in the cost function.

However, you actually don’t need the reshape command here at all. I will report this upstream.


Sorry to ask, but, we should then, ignore this error?

1 Like

I don’t remember if it gives you 100% pass when you submit with that error, if it does, then you can ignore it. If it doesn’t, change it, but remember that it’s not needed here.


it seems that “[depth]” works for me, not “depth”, not “…, depth,1)”, not “…,(depth,1)”.

1 Like

Here’s another explanation:

  • shape=[C, 1]: This shape results in a column vector. Each element of the one-hot encoded tensor will be reshaped into its own column. This is often used when you want to represent each one-hot encoded label as a column vector.
  • shape=[C,]: This shape results in a one-dimensional tensor. The resulting tensor will be a flat array with C elements. This is often used when you want to represent the one-hot encoded labels as a flat array.

if you see the code comments, the function should return a one-dimensional tensor (array) with the one hot encoding.

1 Like