W4_A1_Ex-5_L_model_Forward


I do not know why I get a dimension error for this

You are processing the output layer, which is outside the “for” loop. So print what the value of l (ell) is there in that expression. Hint: it’s not what you think, which causes you to use the wrong W and b values.

Or try running this loop and watch what happens:

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

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

Here’s another thread which gives you the full “dimensional analysis” for this test case.



I see my mistake with the for loop, but now I have this error

One step at a time. Now that means that the shapes are correct, but the actual values are not. So we need to see more detail of the output to know where to look. Are you sure you used “relu” for the hidden layers and “sigmoid” only for the output layer?

BTW we are assuming that all your previous functions pass their respective tests, right?


So the output (AL) is not the issue, but the cache is what I believe is.

I did have the relu for the hidden layers and sigmoid for the output

Yes, my previous functions are alright

Yes, it looks like your cache values are wrong. Notice that you must have “None” for some of them. That can’t be right …

What can I do about that?

Debug the code. E.g. put in some print statements to show your cache values. Or just show the shapes. At each layer, the structure you get is a “tuple”:

((A, W, b), Z)

and then you end up with a list of those.

Pardon me, but I need some help understanding how to do that, and how it will help me fix the issue.

Looking at what we can see in the exception trace in your first post on this thread, everything looks correct in how you handled that caches. The only part we can’t see is how you handled it for the hidden layers (the body of the for loop). Do you have the same code there with the “append” that we can see for the output layer. (Update: I was wrong here and wasn’t looking closely enough. There should be no assignment in the append operation for the cache.)

Just start by putting print statements right before the return statement in L_model_forward like:

print(type(caches))
print(len(caches))

What does that show you? Or you could even add:

print(type(caches[0]))
print(len(caches[0]))

Thank you for the help
I just realized it was a syntax error with the way I used the append function.

Glad to hear you found the answer!

Oh, yikes, I missed the error! You were assigning to caches which is a mistake. Eeek!

both output and hidden layers actually :sweat_smile: