Week 4 Step by Step L_model_forward

" To add a new value c to a list, you can use list.append(c)"

When I try to do this with cache, I get the message “TypeError: descriptor ‘append’ requires a ‘list’ object but received a ‘tuple’”. This makes sense because fn “linear_activation_forward” explicitly states that “cache” is returned as a tuple.

I’ve tried using list() to convert the tuple to list, but still have this error…

t_X, t_parameters = L_model_forward_test_case_2hidden()
t_AL, t_caches = L_model_forward(t_X, t_parameters)

print("AL = " + str(t_AL))

L_model_forward_test(L_model_forward)


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)
31 # using list() below to convert tuple to list for append - not working presently
32 cache = list(cache)
—> 33 caches = list.append(cache[l-1])
34
35 # YOUR CODE ENDS HERE

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

Hi @Brendon_Wolff-Piggot. Look toward the end of the traceback, line 33 to be precise. The object that you are trying to append is an element of the cache, not the cache itself. Not good.

More importantly, your syntax for appending to a list is incorrect. The (empty) list is defined in the very first line of the L_model_forward function:

caches = []

The name of the list is caches. The proper syntax for appending to a list is as follows:

list_name = list_name.append(list_item)

Helping to confuse matters, list is actually a built-in Python function, but it is misapplied here. The list() function can construct a list from a Python iterable (e.g. tuple, string), but it will choke on an ndarray.

If you’re interested, do some Goolge-Fu on the Python list function, but you do not want to be using it here.

Thanks - at some stage I had an error saying that cache was of type tuple and so could not be added to a list - so I tried to convert the tuple to a list and access a single element.

I’ve adopted your suggestion on the syntax for appending a list but something is still wrong. It looks like the “caches” variable is undefined when it is accessed. But I have verified that
caches =
is still up front in the function. I’ve added some type() and print() lines to try and debug things, but I’m not getting any output from them when the function runs.

t_X, t_parameters = L_model_forward_test_case_2hidden()
t_AL, t_caches = L_model_forward(t_X, t_parameters)

print("AL = " + str(t_AL))

L_model_forward_test(L_model_forward)

AttributeError 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)
48 # cache = list(cache)
49 type(cache)
—> 50 caches = caches.append(cache)
51
52 # YOUR CODE ENDS HERE

AttributeError: ‘NoneType’ object has no attribute ‘append’

Hi, Brendon.

A confusion always arises while writing between 1 & l. Did you mention the same?

Are you sure that works? My understanding is that there is no “assignment” involved. You are simply invoking the “append” method of that object, as in:

myList.append(newElement)

Indeed, @paulinpalto. My apologies.

Hi, Brendon. Please note @paulinpaloalto’s correction. But I do not think that is the end of the line. It is common in this exercise for learner’s to copy and paste code from the loop (layers l = 1, ..., l-1 ) into the code for the final layer L, without doing all of the necessary cleanup. Please, have a look.

1 Like

Yes, Paul was right but you are also. I had indented the code for assigning LA into the for-loop for some unknown reason :face_with_open_eyes_and_hand_over_mouth:
But how does L+1 make sense if the network only has L layers?

The point is that you have to remember how indexing works in python: everything is zero based. Run this code and watch what happens:

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

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

There are two potential surprises there if you haven’t yet achieved the full Vulcan Mind Meld with how indexing works in python! :scream_cat: :vulcan_salute:

1 Like

Well, that IS a surprise :see_no_evil: :see_no_evil:

Update:
I added a list() statment into the caches.append and so avoided the “adding tuples to list” error.
Together with fixing the indent and the subsequent increment that sorted this function out. Somehow the “bad shapes” error that I picked up along the way also disappeared.
I’ll be looking into pylint and similar tools…

Thanks all!