DLS Course 1 Week 4

I am now running L_model_forward

I got this error message

NameError Traceback (most recent call last)
1 t_X, t_parameters = L_model_forward_test_case_2hidden()
----> 2 t_AL, t_caches = L_model_forward(t_X, t_parameters)
4 print("AL = " + str(t_AL))

in L_model_forward(X, parameters)
—> 29 A, cache = linear_activation_forward (A_prev,W, b, activation = “relu”)
30 caches.append(cache)

NameError: name ‘W’ is not defined

Well, where in your L_model_forward function is the variable W defined? The error message seems pretty clear and it gives you something very definite to investigate.

Notice that the W^{[l]} and b^{[l]} values for all the layers are contained in a python dictionary called parameters, right? So part of the logic you need to write here is to extract the correct values from that dictionary for the layer that you are currently processing. You should already be familiar with how to deal with the dictionary from the earlier routines that you have written, e.g. the initialize routines.

This is where W has been defined

for l in range(1, L):
parameters[‘W’ + str(l)] = np.random.randn(layer_dims[l], layer_dims[l-1]) * 0.01

But that is in a completely different function, right? Are you familiar with the way variable “scope” works in python? But that is an example that should illuminate what you need to be writing in L_model_forward: note that ‘W’ is a string in that line of code, right? It’s being used to construct a “key” for the python dictionary.

so should I recall W again in this function?

What do you mean “recall”? You need to write code that uses the “key” values for the parameters dictionary to extract the particular values you need.

This is not a beginning python course: you are already expected to have solid familiarity with how python works as a prerequisite before you start here. If this is the type of issue you are having difficulty with, it might be worth considering taking a python course first.