In this exercise, you have to reshape the vector into a (depth, )
or (depth,1)
vector. But some of those vectors give errors in the assertion test of that exercise. However, if you have a reshaped vector that passes this assertion test (depth)
, later on in new_y_test = y_test.map(one_hot_matrix)
you get an error.
Reading Course 2, Week 3, one_hot_matrix clarification - #5 by philgo the solution is given to use [depth]
instead of (depth)
. For me that also works. It passes the assertion test in one_hot_matrix
and the y_test.map call
.
However, I have no clue why.
I did some testing, and this is what works and doesn’t work.
a) one_hot = tf.reshape(tf.one_hot(label, depth, axis=0), (depth))
passes one_hot_matrix assertion test and fails y_test.map call.
b) one_hot = tf.reshape(tf.one_hot(label, depth, axis=0), (depth, 1))
fails one_hot_matrix_assertion test and passes y_test.map.call
c) one_hot = tf.reshape(tf.one_hot(label, depth, axis=0), (depth, ))
passes one_hot_matrix_assertion test and passes y_test.map.call
d) one_hot = tf.reshape(tf.one_hot(label, depth, axis=0), [depth])
passes one_hot_matrix assertion test and passes y_test.map call.
I have no clue, why c & d work, but a & b give problems.
Furthermore if I look at the vector which you get from the reshaping:
a) ((depth)): tf.Tensor([0. 1. 0. 0.], shape=(4,), dtype=float32)
b) (depth,1): tf.Tensor(
[[0.]
[1.]
[0.]
[0.]], shape=(4, 1), dtype=float32)
c) (depth,): tf.Tensor([0. 1. 0. 0.], shape=(4,), dtype=float32)
d) [depth]: tf.Tensor([0. 1. 0. 0.], shape=(4,), dtype=float32)
vectors a, c & d look completely the same to me. Yet vector a gives trouble further down the road when y_test.map
is called.
Is there some logical explanation why this is the case, and why c & d work and a & b do not?
PS Worth noting is that if I look a bit ahead at, this line: print(next(iter(new_y_test)))
for vectors a & b, I get:
tf.Tensor(
[[1.]
[0.]
[0.]
[0.]
[0.]
[0.]], shape=(6, 1), dtype=float32)
and for vectors c & d I get:
tf.Tensor([1. 0. 0. 0. 0. 0.], shape=(6,), dtype=float32)
So somehow rank 1 vectors are needed, but again, why? So far, we have been instructed in these courses to explicitly reshape vectors into the right shape.