Course 1 [Week 4] Assignment #1, Exercise 5 L_model_forward

Hello,
This is more a python programming question than a DL topic, but I am stuck in this and I’d appreciate some help.
In Course 1 [Week 4] Assignment #1, Exercise 5 L_model_forward
In the for loop (i.e., for l in range(1, L):), I do not know how to access the dictionary parameters through the sequence (i.e., W1, b1, W2, b2) to apply the relu function. Same for the last position of the dictionary parameters to apply the sigmoid function (i.e., W3, b3).
I’ve googled a lot but I do not find anything that can help.
Thank you

They give you an example of how to do this in the instructions for the initialize_parameters_deep routine in that same notebook. The keys to the dictionaries are python strings. In python, you can append two strings using the “+” operator:

firstName = "Fred"
lastName = "Flintstone"
fullName = firstName + " " + lastName
print(f"Full name is {fullName}")

Then the other technique is that if you have a number, e.g. the loop counter in your for loop, and you want to turn that into a string, use the str() function:

answer = 42
print(type(answer))
print(type(str(answer)))

To set values in a python dictionary, we use the format
my_dictionary["mykey"] = 1234
and then to retrieve that value from the dictionary later, you write
my_var = my_dictionary["mykey"]

Like @paulinpaloalto suggested, look at initialize_parameters_deep function. You’ll see that we saved the weight and bias values in the dictionary inside a loop. Now in L_model_forward function, you should retrieve those weight and bias values from the dictionary in a similar manner.

1 Like

I was calling it like in initialize_parameters_deep function, but I was getting an error. I just assumed it was because I was not fetching the values correctly from the dictionary. It turned out that I was just not calling well the function, and the dictionary fetching was right. I could solve it and learned a few terms on the way, so win win.
Thank you for your advice.

1 Like

I have tried what I used in ititialize_parameters_deep. However, I keep getting: UnboundLocalError: local variable ‘linear_cache’ referenced before assignment. Is it incorrect to retrive values from the dictionary inside the parameters of the linear_activation_forward?

I have tried what I used in ititialize_parameters_deep. However, I keep getting: UnboundLocalError: local variable ‘linear_cache’ referenced before assignment. Is it incorrect to retrive values from the dictionary inside the parameters of the linear_activation_forward?

That usually means you are passing an incorrect value for the activation parameter, which results in neither of the conditional paths being taken

Right now I have it set as relu for the first and sigmoid for the second. Am I using the wrong syntax?

You have to be careful there. Note that relu is not the same thing as “relu”. The former is an object reference to the function and the latter is the string name of the function. Which does the code in the called function expect?

2 Likes

It seems that it should just be “relu”. However, I have tried it both ways. If I try relu I get: UnboundLocalError: local variable ‘linear_cache’ referenced before assignment. If I try “relu” I get: TypeError: descriptor ‘append’ requires a ‘list’ object but received a ‘tuple’.

Using “relu” is correct, but this means you have yet another bug somewhere else. Look at the line that throws the error about “append”. You must be doing something wrong there. You can “copy/paste” the actual exception trace that you get and that may give us enough info to make some suggestions for what the issue may be.

1 Like

One other general thing to note is that the functions you are calling from the “Step by Step” assignment are provided for you and can be assumed to be correct. But a perfectly correct function can throw an error if you pass it incorrect arguments. So debugging the issue may require starting with the lower level function where the error is actually thrown. Then you’ll need to track backwards up the call stack to figure out where the actual error is.

Is this the actual exception trace?
TypeError Traceback (most recent call last)
in
1 t_X, t_parameters = L_model_forward_test_case_2hidden()
----> 2 t_AL, t_caches = L_model_forward(t_X, t_parameters)
3
4 print("AL = " + str(t_AL))
5

in L_model_forward(X, parameters)
29
30 A, cache = linear_activation_forward(A_prev, parameters[“W” + str(l)], parameters[“b” + str(l)], “relu”)
—> 31 caches = list.append(cache)
32
33 # YOUR CODE ENDS HERE

TypeError: descriptor ‘append’ requires a ‘list’ object but received a ‘tuple’

My understanding is that loops work from the first given value up to but not including the upper limit. So, I’m not sure how WL and bL were initialized in the (1, L) loop of exercise 4

Yes, your description of how loop indices work is correct. You can confirm that by running some test loops. Try this and watch what happens:

for ii in range(5):
    print("ii = " + str(ii))
for jj in range(1,4):
    print("jj = " + str(jj))

The “append” error is caused by a misunderstanding of how “append” works. This is “object oriented programming”. “append” is a “method” of a list object. So if I have a list called myList and I want to append a new element called newElement to it, the syntax is:

myList.append(newElement)

Notice that there is no assignment statement involved there. In Object Oriented Speak, I am “invoking the append() method of the list object myList”.

5 Likes

Thank you so much! I finally got it and I learned so much!