Course 2, Week 3, one_hot_matrix error

Hello everyone,
after I implemented the one_hot function, I got this error when i ran this part of code

new_y_test = y_test.map(one_hot_matrix)
new_y_train = y_train.map(one_hot_matrix)

I got this error

 ValueError: Shape must be rank 1 but is rank 0 for '{{node Reshape}} = Reshape[T=DT_FLOAT, Tshape=DT_INT32](one_hot, Reshape/shape)' with input shapes: [6], [].

and it impacts the whole code

That says that the objects you are attempting to reshape are scalars instead of rank 1 tensors. So how did that happen?

here my function : one_hot = tf.reshape(tf.one_hot(label, depth, axis=0), depth)

Ok, sorry, I think I misinterpreted what the error message is saying. It is saying that it doesn’t like the “shape” argument you gave to reshape. It is just a scalar, but it needs to be at least a rank 1 array. Try [depth,] or [-1,].

In other words, the shape needs to really be what array.shape would give you for the resulting reshaped output object. That would be an array, not a scalar. A scalar doesn’t make any sense as a shape.

3 Likes