Week 4 Exercise 5 - L_model_forward shape challenges

This one I am feeling so stranded with it. I have no clue how to get shapes in order.

First of all, posting your code in here is not recommended. Please remove it.

Then, a couple of things that you need to consider.

  1. What a for-loop needs to do : This is for hidden layers to pass output to the next hidden layer. In this sense, you need to be very careful which parameters should be used for iterations. Apparently, that’s an output from linear_activation_foward(). You set A to A_prev initially. Which variables that you should set A_prev at the 2nd iteration ? In this sense, the name of a variable that you get from linear_activation_foward() should be a same variable to set A_prev.
  2. Activation function : In hidden layers, as an inline guide (comment) says, you need to use ‘relu’, not "sigmoid’.
  3. The scope of a loop : The last layer should not be part of for-loop. There is an indentation problem for those two lines of code. And, the activation function should be “sigmoid”, not “relu”.

Please read hints and inline guidances carefully, and revisit your implementations.


I have had to rewatch the lessons to understand how to implement the solution. I am not sure why this code cannot produce the shapes needed to solve the problem. Would you help once again on clues? Tried transposing either A or parametres[‘W’+str(l)] to pass np.dot

Given that you have used sigmoid in that call, I’m guessing this is for the output layer, after you fall out of the loop. My guess is that your problem has to do with how the loop index variable works when you fall out of the loop. Try this experiment and watch what happens:

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

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

Have you seen the “dimensional analysis” thread for this particular test cell? It’s worth a look to get a clear picture of what you expect to happen at each layer. Reading that, notice that A2 is 3 x 4 and that is the correct input for layer 3, but in that case the W3 shape should be 1 x 3. But your W value is the shape of W2. Hmmmmm …

Yes, I used relu inside the for loop while sigmoid for the output layer.
I went through the “dimensional analysis” thread. I have printed out the loop interations as suggested. See the result below.

That is the only exercise standing in my way to finish the course.

Ok, you now have the evidence that shows l is not the right index value to be using to select the W matrix for the output layer, right? That was the point of suggesting that experiment. It was the right value inside the loop, but not once you fall out of the loop. That’s a different case, right?

I get you now. Once augmented l by 1 in the output layer, everything became alright.