# Course 1- week 2- Exercise 8- model

**URL:** <https://community.deeplearning.ai/t/course-1-week-2-exercise-8-model/40077>\
**Category:** Neural Networks and Deep Learning\
**Tags:** coursera-platform\
**Created:** [September 21, 2021, 12:42am UTC](https://community.deeplearning.ai/t/course-1-week-2-exercise-8-model/40077 "2021-09-21T00:42:42Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hamid\_ILBEYGI](https://avatars.discourse-cdn.com/v4/letter/h/7c8e57/32.png) [@Hamid\_ILBEYGI](https://community.deeplearning.ai/u/Hamid_ILBEYGI)\
**Post date:** [September 21, 2021, 12:42am UTC](https://community.deeplearning.ai/t/course-1-week-2-exercise-8-model/40077/1 "2021-09-21T00:42:42Z")

</div>

Hi  
I am getting the following error with my code.  
can you please give some hint?

_{moderator edit - solution code removed}_

ValueError 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)  
113 y\_test = np.array([0, 1, 0])  
114  
 → 115 d = target(X, Y, x\_test, y\_test, num\_iterations=50, learning\_rate=0.01)  
116  
117 assert type(d[‘costs’]) == list, f"Wrong type for d[‘costs’]. {type(d[‘costs’])} != list"

in model(X\_train, Y\_train, X\_test, Y\_test, num\_iterations, learning\_rate, print\_cost)  
50 b = params[“b”]  
51  
—\> 52 Y\_prediction\_test = predict(w, b, X\_test)  
53 Y\_prediction\_train = predict(w, b, X\_train)  
54

in predict(w, b, X)  
16 m = X.shape[1]  
17 Y\_prediction = np.zeros((1, m))  
—\> 18 w = w.reshape(X.shape[0], 1)  
19  
20 # Compute vector “A” predicting the probabilities of a cat being present in the picture

ValueError: cannot reshape array of size 2 into shape (4,1)

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [September 21, 2021, 12:47am UTC](https://community.deeplearning.ai/t/course-1-week-2-exercise-8-model/40077/2 "2021-09-21T00:47:08Z")

</div>

You have misspelled the first return value for the _optimize_ function as _prams_ instead of _params_. That will not end well. 🤓

---

<div class="post-metadata">

**Author:** ![Manan\_Jain](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@Manan\_Jain](https://community.deeplearning.ai/u/Manan_Jain)\
**Post date:** [November 28, 2021, 11:34pm UTC](https://community.deeplearning.ai/t/course-1-week-2-exercise-8-model/40077/3 "2021-11-28T23:34:32Z")

</div>

Hello,  
I am having the sa=omewhat similar issue and I have correctly defined the value of params.

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

~/work/release/W2A2/public_tests.py in model_test(target)
    120 
    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 

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

```

I understand that there is some issue with the shape of w. I defined the value of w as:

```auto
w = np.zeros((dim, 1))

```

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [November 29, 2021, 12:06am UTC](https://community.deeplearning.ai/t/course-1-week-2-exercise-8-model/40077/4 "2021-11-29T00:06:14Z")

</div>

That error probably means you are referencing global variables instead of the parameters passed to the _model_ function. For starters, you don’t need to call _np.zeros_ in _model_, because you already wrote the function initialize\_with\_zeros and you can just call that. But passing _dim_ is a mistake, unless you have set _dim_ to the number of rows of the matrix _X\_train_, which is the relevant local variable in the _model_ function.

---

<div class="post-metadata">

**Author:** ![Manan\_Jain](https://avatars.discourse-cdn.com/v4/letter/m/77aa72/32.png) [@Manan\_Jain](https://community.deeplearning.ai/u/Manan_Jain)\
**Post date:** [November 29, 2021, 12:55am UTC](https://community.deeplearning.ai/t/course-1-week-2-exercise-8-model/40077/5 "2021-11-29T00:55:16Z")

</div>

> [@paulinpaloalto](#):
>
> set _dim_ to the number of rows of the matrix _X\_train_

So should I pass var _dim_ as shape/ size of X\_train? But this would mean that I would still have a mismatch in the dimensions when I calculate A.

Also when I pass that value as array like X\_train[0]. I still get an error.

```auto
TypeError: only integer scalar arrays can be converted to a scalar index

```

which I can’t do as the X\_train is not scalar.

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://community.deeplearning.ai/u/paulinpaloalto)\
**Post date:** [November 29, 2021, 12:58am UTC](https://community.deeplearning.ai/t/course-1-week-2-exercise-8-model/40077/6 "2021-11-29T00:58:17Z")

</div>

The point is that you need to pass the number of rows of _X\_train_ to _initialize\_with\_zeros_. So how do you do that? Hint: use the “shape” attribute of _X\_train_, not _X\_train_ itself.
