C2 W3 Exercise 5


I do not understand the type error
More:
I didn’t understand why I did the one_hot encoding, what was the purpose, even because
at the end of exercise 3 there was these 2 lines of code with the subsequent errors
new_y_test = y_test.map(one_hot_matrix)
new_y_train = y_train.map(one_hot_matrix)

Hi @Luca_De_Renzo ,

The typeError is referring to tf.math.add(), which requires 2 input arguments. However, what you have done here is that you have only passed in one argument, tf.linalg.matmul(W1,X)+b1. If you replace the + operator with , that should work.

The nameError means new_y_test which was defined in cell further up, and has not been included in the execution environment. Every time you start working on the assignment, just make sure you run all the code cells from start, so that the execution environment is up-to-date with all the global variables, library files, and utilities files linked in.

The typeError is referring to tf.math.add(), which requires 2 input arguments. However, what you have done here is that you have only passed in one argument, tf.linalg.matmul(W1,X)+b1. If you replace the + operator with , that should work.
Tks it works

It’s not like you said, it’s keeps me giving the error even if I run all over
There are 2 errors, if you pay attention at the screenshot; this one is the name error and is not depending on the prior running codes as was trying to say.
The second is the value error and is tied, I think, to the one-hot encoding of which I didn’t understood why am I calculating it, for which purpose since seem like I do not use afterwards
Many thks

Hi @Luca_De_Renzo ,

What is happening here in regard to the new_y_test error is this, your code for one_hot_matrix() is not working. Please check you have passed in the (label, depth, axis = 0) when calling tf.one_hot()

I put all of the 3 arguments

HI @Luca_De_Renzo ,

Here is the instruction on how to implement the one_hot() function:

  • tf.reshape(tensor, shape)

one_hot = None(None(None, None, None), shape=[-1, ])

Notice, tf.reshape() has two parameters. Your code passed in the input argument depth in place of the shape dimensions required to turn the matrix to a single column matrix. The template code has already showed you how to specify the shape as a single column matrix, shape=[-1,]. Here is a link that explains the meaning of -1.

Please see the instruction taken from the comment lines below:
Returns:
one_hot – tf.Tensor A single-column matrix with the one hot encoding.

It is important to read the implementation instructions as well as the comment lines within a code cell. Because the information provided would help to write your code correctly.

Many thks, you were perfectly right

But now I have another question if I may:
Why there is a returns from the one_hot_matrix function of 6 classes depth
and from the one_hot_matrix_test(target) function a 4 classes?
If it’not confusing it 's not good right? Who is there behind these notebooks?

Hi @Luca_De_Renzo ,

The definition of one_hot_matrix() function sets depth = 6 as the default value. This means if the one_hot_matrix() is called with no depth is passed in, the default value will be used.

This kind of behaviour is common in Python. There are lots of online resources on Python programming. It would be good if you can familiar yourself with the programming language. Here is a link to Python resources that you may like to start with.

For the unit test, depth is set to 4. The unit test is just to check if your code works. Whoever designed the lab assignment, the main objective is to help you understand the concepts presented in the video lectures.

The mentors here are happy to answer your questions and offer assistance if needed.