C2W3: Tensorflow intro_Ex. 3 - one_hot_matrix : Error message while output is correct

Hello!
I am getting a huge error message while my output matches the expected output.
Here is my output:
tf.Tensor(
[[0.]
[1.]
[0.]
[0.]], shape=(4, 1), dtype=float32)

Here is the error message:

`InvalidArgumentError Traceback (most recent call last)
in
12 print("\033[92mAll test passed")
13
—> 14 one_hot_matrix_test(one_hot_matrix)

in one_hot_matrix_test(target)
7 assert result.shape[1] == 1, f"Reshape to have only 1 column"
8 assert np.allclose(result, [[0.], [1.], [0.], [0.]] ), “Wrong output. Use tf.one_hot”
----> 9 result = target(3, depth)
10 assert np.allclose(result, [[0.], [0.], [0.], [1.]] ), “Wrong output. Use tf.one_hot”
11

in one_hot_matrix(label, depth)
15 # YOUR CODE STARTS HERE
(deleted)
19 # YOUR CODE ENDS HERE`

/opt/conda/lib/python3.7/site-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
58 ctx.ensure_initialized()
59 tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
—> 60 inputs, attrs, num_outputs)
61 except core._NotOkStatusException as e:
62 if name is not None:

InvalidArgumentError: Input to reshape is a tensor with 4 values, but the requested shape has 12 [Op:Reshape]

Any ideas? I have difficulties with this package.

Thanks in advance

Hi Viktoriia,

Check out the dimensions of your call to tf.reshape, they look wrong to me.
Think about what dimension the resulting one_hot matrix should be? The exercise says:
" return the one hot encoding in a column wise[sic] matrix"

The case that is failing for you is when the one_hot function is being called with label=3, depth=4 for which the result should be (according to test_case):
[[0.],
[0.],
[0.],
[1.]]

Hope that helps!

3 Likes

Hi @Viktoriia,

@neurogeek is right, check the second argument to tf.reshape.

If still in doubt, print the shape of the one_hot tensor right after calling tf.one_hot and you’ll immediately see why you need to call tf.reshape :slight_smile:

If you don’t mind, delete the code between # YOUR CODE STARTS HERE and # YOUR CODE ENDS HERE once you get it to work.

Good luck!

3 Likes

Dear @neurogeek and @nramon
Many thanks for your replies. It took me time to figure out…
So in this particular exercise we need to compute a one_hot_matrix for a single label therefore the shape must be number of classes by 1. :woman_facepalming:
That was really helpful ! I would probably not get it today without your hints. :pray:
Thanks!

2 Likes

Yep you are right!
Glad that worked out!

1 Like

Hello, I am stuck on the same one_hot_matrix exe. but my problem is I can not manage shape.
Here Is my code.

##YOUR CODE STARTS HERE

one_hot = tf.one_hot(label, depth)

##YOUR CODE ENDS HERE

and here is my error :---------------------------

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

IndexError Traceback (most recent call last)
in
12 print("\033[92mAll test passed")
13
—> 14 one_hot_matrix_test(one_hot_matrix)

in one_hot_matrix_test(target)
5 print(result)
6 assert result.shape[0] == depth, “Use the parameter depth”
----> 7 assert result.shape[1] == 1, f"Reshape to have only 1 column"
8 assert np.allclose(result, [[0.], [1.], [0.], [0.]] ), “Wrong output. Use tf.one_hot”
9 result = target(3, depth)

/opt/conda/lib/python3.7/site-packages/tensorflow/python/framework/tensor_shape.py in getitem(self, key)
885 else:
886 if self._v2_behavior:
→ 887 return self._dims[key].value
888 else:
889 return self._dims[key]

IndexError: list index out of range