W2_A2_Ex-8_Value Error for w

Hey there! I am afraid i have some kind of similar problem but just cant figure out the solution. Would be glad for help!

i printed x.shape (4,7) and x_train.shape(2,3) and w = w.reshaped(X_shape[0], 1) to w(2,1) and i dont know how/ and where to change it to (4,1).

Thank you for your help in advance!
@paulinpaloalto @Ramtin_Azami @kenb

The third argument to predict should be an X value, not a Y value, right? As in X_train or X_test

It also sounds like you may be referencing global variables in your model code. E.g. maybe in the call to the initialization routine. You should only reference local variables, which includes the parameters to the function.

Thank you very much!

May i ask another question:
So in this example: image|690x362

Lets say w and b are initialized with zeros and lets assume that the very first Pixel of an 64x64x3 image is 250 and the second is 100. So the first pixel (x1/x2) out of 12288 is 250/100. Lets assume alpha is 0,05.

Lets say after the forward propagation “a” or “y-hat” = 0,80 and (the underlying image) y=1.
So by performing backpropagation I want the model to change the parameters in order to get y-hat closer to 1 than what it is right now with 0,80.

So dz = y-hat - y = 0,80 - 1 = -0,20
dw1 = x1*(a-y) = 250*-0,20= - 50
dw2 = x2*(a-y) = 100*-0,20 = -20
w1 was zero and is now: w1 = 0 - 0,1*-0,20250 = 5
w2 was zero and is now: w2 = 0 - 0,1
-0,20*100 = 2

Next forward propagation is z = w1x+b
z2 for x1 = 250
5+b = 1000+b
z2 for x2 = 100*2+b = 200+b
→ which leads to a greater z than before which leads to a greater sigmoid of z, right? This makes sense because we want w to amplify x1 and x2 because they already perform ok?

but what if the modell performes poorly. lets assume y-hat is 0,2 so
dz is 0,2-1 = -0,8
dw1 = x1*(a-y) = 250*-0,8= - 200
dw2 = x2*(a-y) = 100*-0,81 = -80
w1 was zero and is now: w1 = 0 - 0,1*-200 = 20
w2 was zero and is now: w2 = 0 - 0,1*-80 = 8

z2 for x1= 25020+b = 5000+b
z2 for x2 =100
8+b= 800+b
which amplifies the Modell even more. What am i missing?

Thank you very much! I hope what i wrote here does make sense :).

The point is that the model is the w and b values, right? So you can’t just “make up” an artificial \hat{y} value of 0.8 or 0.2 and then calculate from that and expect everything to make sense. The point is that \hat{y} is determined by the w, b and x values. Also there is no guarantee that the very first step of gradient descent (or any step for that matter) will always be in the most desirable direction. Sometimes you have to wander around the solution surface for a while before you find the right path and start to converge. Even once you start to converge, if the learning rate you have chosen is too large, you can then later get divergence.

The other point it make here is that we never use the raw pixel values as 8 bit integers: we “normalize” or “standardize” them by dividing them by 255 to give floating point numbers between 0 and 1. The performance of gradient descent is much better with properly scaled values.

If you’re also asking the more general question of why it suffices to initialize the w and b values to zeros in the Logistic Regression case, but that no longer works when we get to real Neural Networks in Week 3, here is a thread about the math behind Symmetry Breaking which is worth a look. As with everything, it always goes back to the math. :nerd_face:

@paulinpaloalto @kenb

I have got this error while running the model:

Kindly help

That probably means you are “hard-coding” some parameters when you call optimize from your model function. There should be no equal signs in the parameters that you pass to optimize, right? There are some good discussions about this type of error earlier on this thread, e.g. here and here.

Dear Paul,

I haven’t hardcoded anything my code looks like the below:

{moderator edit - solution code removed}

Everything you show there looks fine. Are you sure that params is the variable name that you use later in the function when you call update_parameters?

Thank you for the reply!

I got what you were implying thank you

But that’s not really the question I am asking. That return statement references local variables within the scope of the optimize function. The question is whether you are consistent within the local scope of the model() function about which variable names you use for the params dictionary.

Thank you I got what you were saying now. Thanks for the insight

I am getting the same error even after using the updated params like:

{moderator edit - solution code removed}

Sorry, I wasn’t paying enough attention to detail in your earlier posts: you can see the bug in the code that you showed earlier. The problem is the variable name you use for the third return value from optimize. It should be costs, but you used cost. So the result is that when costs is referenced later in model, it picks up a global variable by that name that has the wrong values.

Hi Paul,
joining the bandwagon of missing something in my code and hence getting below error.
AssertionError: Wrong values for d[‘w’]. [[0.]
[0.]
[0.]
[0.]] != [[ 0.08639757]
[-0.08231268]
[-0.11798927]
[ 0.12866053]]

breaking my head for more than 2 days … All other assignments are working fine. only at the last step I am missing something.

below is code snipped where I am getting an error: see if you can help.

{moderator edit - solution code removed}

Dear @hmraval79,

Please check the code you are writing for params, grads and costs. You are missing something in it.

The model was defined as follows

def model(X_train, Y_train, X_test, Y_test, num_iterations=2000, learning_rate=0.5, print_cost=False):

but you are defining it in other ways. Make the correction and you would be able to pass the assignment fully!

Best,

Hi Rashmi,

Thanks for your prompt response,

I updated the argument for params, grads and costs function but getting same error. :frowning:

{moderator edit - solution code removed}

(please note for time being I kept print_cost=True)

Cost after iteration 0: 0.693147

AssertionError Traceback (most recent call last)
in
1 from public_tests import *
2
----> 3 model_test(model)

~/work/release/W2A2/public_tests.py in model_test(target)
121 assert type(d[‘w’]) == np.ndarray, f"Wrong type for d[‘w’]. {type(d[‘w’])} != np.ndarray"
122 assert d[‘w’].shape == (X.shape[0], 1), f"Wrong shape for d[‘w’]. {d[‘w’].shape} != {(X.shape[0], 1)}"
→ 123 assert np.allclose(d[‘w’], expected_output[‘w’]), f"Wrong values for d[‘w’]. {d[‘w’]} != {expected_output[‘w’]}"
124
125 assert np.allclose(d[‘b’], expected_output[‘b’]), f"Wrong values for d[‘b’]. {d[‘b’]} != {expected_output[‘b’]}"

AssertionError: Wrong values for d[‘w’]. [[0.]
[0.]
[0.]
[0.]] != [[ 0.08639757]
[-0.08231268]
[-0.11798927]
[ 0.12866053]]

Also, if I change Number of iteration = 2000 i.e. code snippet as under. I am getting a different error..

{moderator edit - solution code removed}

Cost after iteration 0: 0.693147
Cost after iteration 100: 0.088213
Cost after iteration 200: 0.053746
Cost after iteration 300: 0.038907
Cost after iteration 400: 0.030482
Cost after iteration 500: 0.025034
Cost after iteration 600: 0.021223
Cost after iteration 700: 0.018408
Cost after iteration 800: 0.016245
Cost after iteration 900: 0.014533
Cost after iteration 1000: 0.013144
Cost after iteration 1100: 0.011995
Cost after iteration 1200: 0.011029
Cost after iteration 1300: 0.010205
Cost after iteration 1400: 0.009496
Cost after iteration 1500: 0.008878
Cost after iteration 1600: 0.008334
Cost after iteration 1700: 0.007854
Cost after iteration 1800: 0.007425
Cost after iteration 1900: 0.007040

AssertionError Traceback (most recent call last)
in
1 from public_tests import *
2
----> 3 model_test(model)

~/work/release/W2A2/public_tests.py in model_test(target)
116
117 assert type(d[‘costs’]) == list, f"Wrong type for d[‘costs’]. {type(d[‘costs’])} != list"
→ 118 assert len(d[‘costs’]) == 1, f"Wrong length for d[‘costs’]. {len(d[‘costs’])} != 1"
119 assert np.allclose(d[‘costs’], expected_output[‘costs’]), f"Wrong values for d[‘costs’]. {d[‘costs’]} != {expected_output[‘costs’]}"
120

AssertionError: Wrong length for d[‘costs’]. 20 != 1

Thanks.

Regards,
Hemshanker Raval

Dear @hmraval79,

Okay. I understand the problem now. Firstly, don’t change the number of iterations on your own, as it’s already set for 2000. Changing any number, will throw an error.

Secondly, check your codes for y_prediction for test and train sets. They aren’t looking good.

Thirdly, erase your codes for this cell. Now, go to kernel above, click on ‘restart and clear all output’ then run all the codes till this cell by jumping on every cell through (shift+enter) key. Rewrite all the codes again for this cell, run this cell and check whether it’s giving the right output or not. Hope, you pass this assignment this time.

(Note- And, you are using print_cost in a right way now. This was the throwing an error before. Also, note that, when you type anything wrong while writing the codes, it changes the pattern and thus, you will never get the right output. So, whenever, you type the codes wrong most of the times, always remember to restart the notebook after clearing all it’s output, as explained above. This will save your real data but will remove the generated cache at the back).

Rise & shine!