Incorrect for loop setting for Exercise 5 - L_model_forward

There seems to be an issue with how the for loop was set up for ReLU forward prop for hidden layers. This is what was i the notebook:

for l in range(L):

This however will traverse through parameters for ALL layers whereas it should have been only for the hidden layers and the final one should be implemented in sigmoid call. The only way to make this work is to change the loop to:

for l in range(L-1):

And use L in parameters like ths parameters[“W”+str(L)] . Now, i know that the code should be entered only within the designated space but there is no way this could work in it’s original state.

Any ideas?

You filed this under NLP Course 1, but this is DLS. I moved it for you by using the little “edit pencil” on the title.

That is not the code they gave you in the template. It is this:

for l in range(1, L):

Also remember how loops and indexing work in python: everything is “0 based”. Try running this code and watch what happens:

for ii in range(1,5):
    print(f"ii = {ii}")

print(f"After loop ii = {ii}")

If you want to get a clean copy of the notebook to see that you must have changed the loop logic, there is a topic about that on the DLS FAQ Thread.

Thanks for your feedback.

Thanks for moving my question to the correct area. Regarding the issue i am having, i have just one note/comment:

That is not the code they gave you in the template. It is this:
for l in range(1, L):

Highly unlikely. I am very careful not to change the code outside the dedicated are. The link you provided explains how to load the latest version of the notebook. Unfortunately there is a big difference between, clean copy and latest version. With the latest version i am afraid there will be more changes in cells which i have completed already and that means more time wasted on the troubleshooting. So i think i will skip the reload option and change the loop to what you suggest.

We’ll see how it goes, hopefully no problems.
Anyway, thank you for your feedback.


The procedure for getting a clean copy gives you the clean copy of the latest notebook. So I don’t understand what you mean about a big difference between the clean copy and the latest version.

I’ve checked all the versions I can find of that notebook back to a couple of years ago and they all have the logic as I showed it.

So if you didn’t modify that line of code, then the only theory I can come up with is that you copied your solution from someone’s GitHub account and they had modified it.

I hope my theory there is wrong, but we still don’t have an explanation in that case.

The procedure for getting the latest copy requires that you rename your current version out of the way first: the procedure will not overwrite an existing file. So you don’t lose any work by doing that. Once you’ve compared, you can just rename your notebook back to the standard name after just copying over that one line of code.

It’s all good Paul, thanks for the help.


1 Like

Hi @Nenad_Nikolic

Yes, you are correct that the original code is only traversing through all the layers in the range L, instead of only the hidden layers. This would result in the final layer not being implemented in the sigmoid call, and could cause errors in the forward propagation.

To fix this issue, you can modify the for loop as you suggested and only iterate through the range of L-1 for the hidden layers. This will ensure that the final layer is not included in the for loop and can be implemented separately in the sigmoid call.

Also, you should use L in the parameters for the final layer, such as parameters[“W”+str(L)]. This will ensure that the final layer’s parameters are used correctly in the forward propagation.

Another alternative is to use a different looping mechanism like while loop and use a variable to keep track of the current layer and stop the loop when the current layer is the final one.

In summary, modifying the for loop to only iterate through the range of L-1 for the hidden layers, and using L in the parameters for the final layer, will fix the issue with the forward propagation for the ReLU hidden layers.

Muhammad John Abbas

Or you could just use the template code as it was originally given to you. If you look at the code I posted in my first reply here, it is correct. The other advantage of the code as actually written is that you can just use l (ell) as the index value instead of l + 1. A minor point, but it makes the code a bit clearer IMO.

Thanks both for your feedback. My concern was more about changing the code outside the dedicated space and how will that affect the grader. Obviously, if the grader checks the code itself, and if i change the for loop settings then the grader will fail my solution even if it gives correct results.
So just for the future reference, would the grader fail the code that relies on modified template? In other words if we make any change outside the ‘Your Code Starts here’ and ‘Your code ends here’ section would the grader fail or pass the code that gives proper results?

And once again, i don’t believe i have changed the setting of the for loop in this assignment. I am very careful with this and I noticed it as soon as i got to the cell in question. However, there is no point in dwelling over it as i have finished the assignment and the course. So it would be great if you could answer the above question.

thanks in advance

The grader does not care how you write the code: it does not analyze the source. It only runs tests and compares the output values.

I’m sorry, but I don’t believe you about not modifying that code. As I already mentioned earlier, I have checked that assignment as far back as 2 years ago and it’s always been the way I showed it. And I gave you the means to check that for yourself as well.

So where did you get that code if you didn’t change it? Did you copy the solution from somewhere other than the course website?


Ok, thanks for that. So it is fair to say that if i change the code outside of expected section and my results are correct than the grader will accept the solution.

To answer your question, I did not copy the code from anywhere. As i said i noticed this as soon as i got to that cell. But this is not important anymore. I don’t want to waste any ones time. It’s all good, i have finished the assignment.


Yes, you are free to change code outside of the “YOUR CODE HERE” sections, but just need to be careful that you know what you are doing when you do that. :smiley: As I said above, the grader only cares about the return values of your functions. Of course that means that you also can’t change things like the function definitions.

1 Like

Hello Nenad.

Latest version of the notebook is when something gets changed or added at the back end (by the staff). You go to Help—>Lab help—>download the new one version. You can rename it.

Clean copy is when your notebook is accidentally changed due to some technical issues in your system or you have changed the markup language by mistake. Then you need to go to the file—click the notebook that you are working on–shutdown the notebook—>delete the file—>go to lap help—>upload a new one.

Renaming is naming the notebook that you are working on for your future reference. This will be available until you are subscribed to the course. To be on a safe side, you can also download your renamed file in your local system.