Course 2, Tensorflow_introduction One_hot matrix function

so we start with y vector with shape (1,) converted into matrix (depth,)

resulting vector before reshape is of shape (4,) meaning it was of dimension (c,). If it is already a in vertical vector format, why do we need to reshape it?

After reshape, the tensor is the same of the same shape (4,) on initial test and and on new_y_test both shaped shape=(6,).

the shape has not changed. I experimented with switching axis and through trial and error completed the function, but I still do not understand what the purpose of the reshape function was here if no change?

on the picture before the exercise the transformation is clearly from (1, m) to (c, m), but the purpose of the function is not clear to me

before reshape tf.Tensor([0. 1. 0. 0.], shape=(4,), dtype=float32)
after reshape tf.Tensor([0. 1. 0. 0.], shape=(4,), dtype=float32)
Test 1: tf.Tensor([0. 1. 0. 0.], shape=(4,), dtype=float32)
before reshape tf.Tensor(
[[0.]
[0.]
[1.]
[0.]], shape=(4, 1), dtype=float32)
after reshape tf.Tensor([0. 0. 1. 0.], shape=(4,), dtype=float32)
Test 2: tf.Tensor([0. 0. 1. 0.], shape=(4,), dtype=float32)
All test passed
Expected output

Test 1: tf.Tensor([0. 1. 0. 0.], shape=(4,), dtype=float32)
Test 2: tf.Tensor([0. 0. 1. 0.], shape=(4,), dtype=float32)

before reshape Tensor(“one_hot:0”, shape=(6,), dtype=float32)
after reshape Tensor(“Reshape:0”, shape=(6,), dtype=float32)
before reshape Tensor(“one_hot:0”, shape=(6,), dtype=float32)
after reshape Tensor(“Reshape:0”, shape=(6,), dtype=float32)

Good catch. You don’t need the reshape. I have answered this previously over here:

2 Likes

I remember Andrew saying somewhere (maybe another course) that he likes to use explicit reshaping methods to ensure the dimensions are the expected at the end of some computation since some operations potentially lose or gain dimensions in the process.

I don’t know if this is the case, but that could be an explanation for why the reshape was there in the first place.

1 Like

Thank you for posting the link to another similar question. I think if the lab instruction are revised, it would help future students to expand on the instruction to the exercise. I used (-1,) and (depth,) dimensions to reshape but this was more trial error and looking what the output expected than truly understanding the reason why reshape was needed.

1 Like

yes, indeed, in the previous course, he did say he prefers to explicitly reshape to eliminate possible bugs caused by incorrect shapes later on. I have also noticed, in some exercises, he uses assert function to verify the shape is correct within functions as an extra check

1 Like