# Week 2 programming assignment issue

HI,

I’m having TWO different problems with Week Two’s programming assignment.

FIRST, it keeps giving one error in my initialize_with_zeros() function, and apparently, the problem is with my b value? (Maybe I don’t understand what a scalar, “based on the bias” is… ???)

Since b is broadcast in this case (in the equation W.T*X + b), can’t I simply just set b = 0??

I’m receiving an “assert” error, when the invisible test code tries to assert b’s type to float (from within the test code, I obviously presume).

I’ve finished almost the whole rest of the assignment, but this function is upsetting me, because np.zeros() seems like such a basic numpy function that I need to be able to use correctly!!

SECONDLY,

When I run the model() code in the end, I’m getting an error to do with the dimensions of my arrays, and I can tell that I’m trying to np.dot, like, an (1, m) with a (4, m)… and I don’t know how to fix this error. I tried switching between np.multiply and np.dot… and various other combinations of np.sum, etc…

Are my two issues related??

I keep getting a 67% on this assignment.

I’ve passed ALL my other assignments. PLEASE, PLEASE, PLEAAAAASE somebody help me!

You’ve initialized `b` to an integer data type.
Integer examples: 1, 3, -1
Float of the same integers above: 1.0, 3.0, -1.0

Dot product can be performed on 2 matrices only when inner dimensions match. So, you can perform dot product on matrices of shape `(a,b)` and `(b,c)` to get a result of form `(a,c)`. See if transpose helps.

1 Like

Thank you so much for helping me understand the difference between an integer and the float of the same integer!

Dear Community,
I am also struggling a bit with this week’s assignment and i would like to ask for some help.
During model implementation i am recieving an error message regarding to different dimensions as in the previous commenter mentioned. However the solution is not so clear for me. Might i get some more detailed explanation?
I’m inserting my error message below (in addition with some extra printouts what represents my question)

Exercise 8 - Model:

``````prints: X, Xshape, wshape, Xshape[0],1 as the followings:
[[ 1.  -1.1 -3.2]
[ 1.2  2.   0.1]]
(2, 3)
(2, 1)
(2, 1)
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-31-9408a3dffbf6> in <module>
1 from public_tests import *
2
----> 3 model_test(model)

~/work/release/W2A2/public_tests.py in model_test(target)
130
131     assert type(d['w']) == np.ndarray, f"Wrong type for d['w']. {type(d['w'])} != np.ndarray"
--> 132     assert d['w'].shape == (X.shape[0], 1), f"Wrong shape for d['w']. {d['w'].shape} != {(X.shape[0], 1)}"
133     assert np.allclose(d['w'], expected_output['w']), f"Wrong values for d['w']. {d['w']} != {expected_output['w']}"
134

AssertionError: Wrong shape for d['w']. (2, 1) != (4, 1)
``````

Regards

Hello @Erno_Benko,

The public test that you have failed uses an X that has X.shape[0] = 4. However, instead of that X, it appears that your function is using another X which is this one:

And this X was defined for Exercise 7’s test. A very possible reason why your function is using an X that’s defined for previous Exercise instead of the X from the list of input arguments, is that you didn’t use the correct variable name for X in your function. Please check for the correct variable name for X to use in your function by reading this:

``````def model(X_train, Y_train, X_test, Y_test, num_iterations=2000, learning_rate=0.5, print_cost=False):
"""
Builds the logistic regression model by calling the function you've implemented previously

Arguments:
X_train -- training set represented by a numpy array of shape (num_px * num_px * 3, m_train)
Y_train -- training labels represented by a numpy array (vector) of shape (1, m_train)
X_test -- test set represented by a numpy array of shape (num_px * num_px * 3, m_test)
Y_test -- test labels represented by a numpy array (vector) of shape (1, m_test)
num_iterations -- hyperparameter representing the number of iterations to optimize the parameters
learning_rate -- hyperparameter representing the learning rate used in the update rule of optimize()
print_cost -- Set to True to print the cost every 100 iterations

Returns:
d -- dictionary containing information about the model.
"""
``````

Cheers,
Raymond

I tried to implement your suggestions, and i understand your reasoning and see the problem myself, still not sure about how to solve this.
Previously I really just called an input ‘X’ argument, now i referred to the imput arguments given, and still gives an erros message

``````---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-19-9408a3dffbf6> in <module>
1 from public_tests import *
2
----> 3 model_test(model)

~/work/release/W2A2/public_tests.py in model_test(target)
123     y_test = np.array([0, 1, 0])
124
--> 125     d = target(X, Y, x_test, y_test, num_iterations=50, learning_rate=0.01)
126
127     assert type(d['costs']) == list, f"Wrong type for d['costs']. {type(d['costs'])} != list"

<ipython-input-18-32ac9ce482f8> in model(X_train, Y_train, X_test, Y_test, num_iterations, learning_rate, print_cost)
35     # YOUR CODE STARTS HERE
36     w, b = initialize_with_zeros(dim)
---> 37     params, grads, costs = optimize(w, b, X_train, Y_train, num_iterations=100, learning_rate=0.009, print_cost=False)
38     w = params["w"]
39     b = params["b"]

<ipython-input-14-bd93eeef1d6a> in optimize(w, b, X, Y, num_iterations, learning_rate, print_cost)
35         # grads, cost = ...
36         # YOUR CODE STARTS HERE
---> 37         grads, cost = propagate(w, b, X, Y)
38
39         # YOUR CODE ENDS HERE

<ipython-input-12-ddedfcc10e46> in propagate(w, b, X, Y)
30     # cost = ...
31     # YOUR CODE STARTS HERE
---> 32     A = sigmoid(np.dot(w.T, X) + b)
33     cost = (-1/m)* np.sum((Y*np.log(A) + ((1-Y)*np.log(1-A))))
34

<__array_function__ internals> in dot(*args, **kwargs)

ValueError: shapes (1,2) and (4,7) not aligned: 2 (dim 1) != 4 (dim 0)
``````

{code removed by mentor}

Now it seems that it has problem with some previous function which was all good in the tests.
I have no idea what else parameter i could call for good

What is `dim`? It seems to me that you are using variables outside of the scope of the function. You really need to be careful when using variables, because variables outside won’t give you any “undefined” error message but it can be an error that will make your function not working as expected.

Raymond
PS: It’s against the guideline in this community to share assignment code, so I have removed it.

oh sorry for the assignment code…
I will avoid posting them.
dim is used in exercise 4 when initializing ‘w’-s.
it is just a real number showing how many w-s are in the system. (by defining the first dimension of vector w)
so basically here would be the same to achieve, i need there an argument with real number to define the first dimension of matrix(here just vector) ‘w’
but just nothing works for me
initialize with ‘dim’ does not work (obviously because it was for a previous exercise).
initialize with argument X_train does not work because it is an array itself not a real number
initialize with shape0 of argument X_train would be a good idea but still not works because i get wrong value for d[‘w’]
initialize with X_test or any version of xtest just doesnt makes any sense, or even init with y* arguments doesnt make any sense.

So i would appriciate some other idea.

Regards

You can use X.shape to check out the shape of the data matrix X. Then with your understanding of the numbers in the shape, you need to get the right number out of the shape to replace dim.

X_train.shape[0] = 4
but still error

``````---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-58-9408a3dffbf6> in <module>
1 from public_tests import *
2
----> 3 model_test(model)

~/work/release/W2A2/public_tests.py in model_test(target)
131     assert type(d['w']) == np.ndarray, f"Wrong type for d['w']. {type(d['w'])} != np.ndarray"
132     assert d['w'].shape == (X.shape[0], 1), f"Wrong shape for d['w']. {d['w'].shape} != {(X.shape[0], 1)}"
--> 133     assert np.allclose(d['w'], expected_output['w']), f"Wrong values for d['w']. {d['w']} != {expected_output['w']}"
134
135     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.14449502]
[-0.1429235 ]
[-0.19867517]
[ 0.21265053]] != [[ 0.08639757]
[-0.08231268]
[-0.11798927]
[ 0.12866053]]
``````

Hello @Erno_Benko,

`X_train.shape[0]` looks like a great choice!

I hope you see that you are making progress, and now you are facing a different error. It is important that you read the error message and try to figure out what’s wrong, because it’s part of your assignment to make your work correct!

For example, the error message says your `d['w']` value is not correct, then you need to look at how you calculate the `w` in your function. We know `w` is optimized by gradient descent, and assuming your `optimize` function is indeed OK, then the first thing to check is, again, whether you have passed into gradient descent the parameters provided by the list of input arguments. Did you hardcode the values, or did you use variable outside of the function’s scope? Neither of them is good.

Raymond

Thank you for your encouragement @rmwkwok !

Proof beyond assumption about optimize function:

``````optimize_test(optimize)
w = [[0.80956046]
[2.0508202 ]]
b = 1.5948713189708588
dw = [[ 0.17860505]
[-0.04840656]]
db = -0.08888460336847771
Costs = [array(0.15900538)]
All tests passed!
``````

The order of optimize function imput arguments are: weights, bias, X, Y values.
from initialization i got w and b…
So optimization shall look like optimize(w, b, X_train, Y_train).
After comes the retrievement of w and b after the last iteration.
dictionary is ‘params’ so e.g. params[“w”]

As i suppose it is not hardcoding and even not a variable from outside function, yet the result is not the expected.
Please correct me if i am wrong.

Regards

However, the `optimize` function also takes `num_iterations`, `learning_rate` and `print_cost` as its inputs and all 3 of them are provided in the list of input arguments of the `model` function. If you do not pass them through to `optimize`, then `optimize` will use their default values. Using the default means you are effectively hardcoding them, although you didn’t hardcode them explicitly.

Let the input arguments of `function` take control. The tests could be using those input arguments.

Raymond

Oh, that was a brand new information, thank you very much!

Although passing these values to the optimization function is the right way, i still have some concern…
The original optimize function (with hyperparameter interation number: 100) has a built-in code line to append the calculated cost value after evety 100th iteration to it’s list.
However is during the model function running it has a number of 2000 iteration (as listed in the model input arguments)
This means that in the cost list i have 2000/100 = 20 cost value appended.
And the test algorythm does not like this idea:

``````---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-27-9408a3dffbf6> in <module>
1 from public_tests import *
2
----> 3 model_test(model)

~/work/release/W2A2/public_tests.py in model_test(target)
126
127     assert type(d['costs']) == list, f"Wrong type for d['costs']. {type(d['costs'])} != list"
--> 128     assert len(d['costs']) == 1, f"Wrong length for d['costs']. {len(d['costs'])} != 1"
129     assert np.allclose(d['costs'], expected_output['costs']), f"Wrong values for d['costs']. {d['costs']} != {expected_output['costs']}"
130

AssertionError: Wrong length for d['costs']. 20 != 1
``````

Actually this is why i thought at first “i just need to let the default hyperparameters do the job”.
Despite all these, now the code works fine with the next steps:

``````train accuracy: 100.0 %
test accuracy: 72.0 %
``````

So it is working, but still a bit cloudy for me the general requirement of the test algorythm.

Regards

Hello @Erno_Benko,

If you want to clear your doubt around the length of the `d['costs']`, then we can look at the following one by one.

1. `optimize` function uses `num_iterations=100` as default.
2. `model` function uses `num_iterations=2000` as default. `model` uses `optimize` in it, and we give that `optimize` the variable `num_iterations` (NOT `num_iterations = 2000`! )
3. the public test that tests the `model` function has one case, and it uses `num_iterations=50` which overrides the default. With 50 iterations, it should have only one cost value appened.

Cheers,
Raymond