Problems with neural network structure

Also use the browser search function (Command-F on Chrome on Mac) to search the whole notebook for occurrences of the string layer_sizes. Are you sure you never assign a value to that name? E.g.

layer_sizes = whatever

anyplace in the notebook.

1 Like

I got this in the grading box when I submitted my assignment for grading, which I do after every exercise. So there was no “root” for me to see.

The test cell gave the proper output, which was:
The size of the input layer is: n_x = 1
The size of the output layer is: n_y = 1

1 Like

Ok, then it must be my other theory: there is something happening later in the notebook that is trashing the value of layer_sizes.

Well, either that or you’ve accidentally done something that trashes the definition of layer_sizes in a way that works in the notebook, but doesn’t work in the grader. The notebook environment is a bit more foregiving in some ways: e.g. I think you can mix spaces and tabs in the indentation, but the grader barfs if you do that. I’m not saying that’s the problem here, but just giving an example.

If you can’t find anything later in the notebook that overwrites layer_sizes, then I think the “get a clean copy” and start over strategy is probably your best bet. You can “copy/paste” over the parts that you have completed. You’ll have to “rename” your existing notebook out of the way as part of the “get a fresh copy” procedure, so you’ll have it available in parallel.

Actually here’s one more question: have you completed all the graded function yet? Maybe there some code layer in the notebook that needs to be completed before you can submit to the grader? But that should turn up something on the search I suggested above …

1 Like

I did your search but found nothing useful.
Going to use your thread and start fresh.
I know enough to recreate what I’ve already done without too much hassle.
Thanks again.

I have not completed the graded function, to answer your question, but starting fresh makes the most sense. It will help me to clarify what I’ve already done, anyway.

1 Like

Sounds good. Keep us posted and I hope that method will help!

1 Like

I ran a new copy of the notebook and got full credit.
20 points down, 80 to go! ; )

1 Like

Hi again, not sure if I will ever sleep again :wink:
I passed Exercise 4 with the help of a model I found online. The model is very helpful, and I can start to make connections between the mathematics and the code.
But I am getting an Assertion error on Exercise 3 unit test and I thought I understood my code.
My expected output worked fine, in the cell above the unit test, shown here below:

parameters = initialize_parameters(n_x, n_y)
print("W = " + str(parameters[“W”]))
print("b = " + str(parameters[“b”]))

But the following cell, the unit test, produced the error and was unable to be graded. Here is the Traceback. Thanks a lot.

Note:

Actual values are not checked here in the unit tests (due to random initialization).

w3_unittest.test_initialize_parameters(initialize_parameters)


AssertionError Traceback (most recent call last)
in
1 # Note:
2 # Actual values are not checked here in the unit tests (due to random initialization).
----> 3 w3_unittest.test_initialize_parameters(initialize_parameters)

~/work/w3_unittest.py in test_initialize_parameters(target_initialize_parameters)
185
186 for test_case in test_cases:
→ 187 result = target_initialize_parameters(test_case[“input”][“n_x”], test_case[“input”][“n_y”])
188
189 try:

in initialize_parameters(n_x, n_y)
15
16 assert (W.shape == (n_y, n_x))
—> 17 assert (b.shape == (n_y, 1))
18
19 parameters = {“W”: W,

AssertionError:

1 Like

What do you mean “the expected output worked fine”? What that error message is telling you is that the b value produced by your code is not the correct shape. Have you looked up what the “assert” statement is in python? It checks the truth of a condition (boolean expression) and then “throws” if the condition does not evaluate to “True”.

Anytime you have a problem with shapes, the first question is “Ok, what shape is it?” Then the second question is “How did it get that way?”

You can answer the first question by adding this statement to the body of your function before you return:

print(f"b.shape = {b.shape}")

Note that in python (3,) is not the same thing as (3,1). The first means that your numpy array has only 1 dimension. Have you read the documentation for numpy zeros?

1 Like

When I said the expected output worked fine, I meant that the cell below gave the correct answer.

parameters = initialize_parameters(n_x, n_y)
print("W = " + str(parameters[“W”]))
print("b = " + str(parameters[“b”]))

And, no I have not read the documentation for numpy zeros, only what was in the course material. I will do that tomorrow. It is late in my time zone.

But if I understand your response, my parameters for W are okay, but I am missing something with b related with numpy zeros. I will pick up there tomorrow.

Again, thanks for your response.

1 Like

Well, we can say that the shape of your W value is correct from the fact that the previous assertion did not “throw”.

What I believe is the case is that when you say that answer is correct, you are not looking closely enough at the output and understanding the implications. In python,

[0. 0. 0.]

is not the same thing as

[[0. 0. 0.]]

which is not the same thing as

[[0.] [0.] [0.]]

See the difference there? Those brackets matter. The first example is a 1D vector with shape (3,), the second is a 2D vector with shape (1,3) and the third is a 2D vector with shape (3,1).

So even though all of them have 3 elements, all of which are floating point 0, they are not the same.

Note that I am just guessing here about what your mistake is based on past experience. I cannot see your code and I may be wrong. There may be other ways to get the shape of b wrong.

1 Like

I’ve figured it out and actually passed the lab with 90%, with help of a model I found online and modified to fit this assignment. Reverse engineering the code has been very helpful for me so far. I am tempted to just close the lab and move on to Week 4, but the perfectionist in me (even when I have no idea what I am doing) wants to finish in style.
I am in Exercise 6:

Exercise 6

Run the constructed above neural network model nn_model() for 100 iterations, passing the training dataset saved in the arrays X_multi_norm and Y_multi_norm.

I can’t use only ‘X_multi-norm’ and ‘Y_multi_norm’ in my parameters, that doesn’t quite work, and I tried various methods to “pass the training dataset saved in the arrays X_multi_norm and Y_multi_norm,” but nothing has worked.

Any hints to put me on the right track?
Am I allowed to post the incorrect code I used?

Thanks a lot.

1 Like

I’m glad to hear that you were able to learn some things by comparing a solution found on the Internet with the exercise and copying/modifying it. That may be a valid educational approach, but you might want to soft pedal the discussion of it. It’s a violation of the Course Honor Code at least according to some interpretations. Note that when you “submit” to the grader, it asks you to testify that the work you are submitting is your own. I guess if you modified the code, then part of it is your work, but that seems at least worth worrying about.

On the specific point about running nn_model, notice that it has more parameters than just the input dataset, right? What happens if you don’t pass those other additional arguments?

1 Like

Thank you for the hint, and moreso for the comment about the course honor code. That never crossed my mind. From the course description I was led to believe that as a beginner I could get through it, but I’ve had to use a LOT of online resources to get my Python up to speed (which it still isn’t).
I am learning a lot using this method, but I am definitely modifying the code to suit the assignment.
That’s the last I’ll say about that.

1 Like

Well, it’s a different thing to read a tutorial website about Python, than it is to find a pre-existing solution to the Planar Data Assignment from DLS Course 1 Week 3 and then adapting the code to solve this assignment, right? But as you say, perhaps that’s “enough said”.

1 Like

I called the shape_X and shape_Y with the local scope but still do not understand why should be expected a result of 5 (instead of 2 ) for the input layer and 2 (instead of 1) for the output layer
Thks for help

1 Like

These are just test cases for the layer_sizes function: the shapes don’t have to match the real shapes we’ll be using. We’re always trying to write general code that will work in all cases.

I also think you are not correctly understanding how “scope” works in python. shape_X and shape_Y are global variables. They are not declared within the scope of the layer_sizes function and it is a mistake to reference them inside the body of the function. You should reference only X and Y, which are the formal parameters to the function.

If you are new to python, you may want to spend some time on learning more about this. Try googling “python scope” and spending some time reading a few explanations on this. But python’s scoping model is pretty standard and not surprising. If you are familiar with Java or JavaScript or C++, then you should already understand this. If this is literally your first exposure to any kind of programming, then you should consider taking an “intro to python” course first before continuing here. This course is not a programming course: they assume you are already a reasonably competent python programmer before you start here.

1 Like

I already explained this point about the scope of shape_X earlier on this same thread.

So in this particular case and In a real world we are going to pass to the function the matrix with X (2,) input examples and for the output the vector Y(1,) examples right?

1 Like

I came from that explanation… but my question is:
in a real world what I should pass to the function are the matrix/vector values of the global variables? And if so what am I doing this ?
def layer_sizes(X, Y):
X= X.shape
Y=Y.shape
The matrix are X(5.3) and Y(2,3) - where do these values come from?

1 Like

Yes, the real data we will be using in this assignment is shaped as you describe, but there is no need for the layer_sizes function to “hard-code” those values.

1 Like