# Linear Algebra for ML wk3 assignment exercise 4

**URL:** <https://community.deeplearning.ai/t/linear-algebra-for-ml-wk3-assignment-exercise-4/305633>\
**Category:** Linear Algebra for Machine Learning and Data Sc...\
**Tags:** week-module-3\
**Created:** [April 9, 2023, 12:38am UTC](https://community.deeplearning.ai/t/linear-algebra-for-ml-wk3-assignment-exercise-4/305633 "2023-04-09T00:38:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![plaudev](https://avatars.discourse-cdn.com/v4/letter/p/9de0a6/32.png) [@plaudev](https://community.deeplearning.ai/u/plaudev)\
**Post date:** [April 9, 2023, 12:38am UTC](https://community.deeplearning.ai/t/linear-algebra-for-ml-wk3-assignment-exercise-4/305633/1 "2023-04-09T00:38:39Z")

</div>

can someone please give a hint on what I’m supposed to do here?

```
# Retrieve each parameter from the dictionary "parameters".
### START CODE HERE ### (~ 2 lines of code)
W = None
b = None
### END CODE HERE ###

```

the instruction is: 1. Retrieve each parameter from the dictionary “parameters” (which is the output of `initialize_parameters()`) by using `parameters[".."]`.

Thanks

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [April 9, 2023, 3:41am UTC](https://community.deeplearning.ai/t/linear-algebra-for-ml-wk3-assignment-exercise-4/305633/2 "2023-04-09T03:41:56Z")

</div>

I’m not a mentor for that course, but I suspect you’re supposed to use some construct like this:  
`<variable_name> = parameters["key"].`

Since “parameters” is a dictionary, you can access each value using its key.

---

<div class="post-metadata">

**Author:** ![ultron\_code](https://avatars.discourse-cdn.com/v4/letter/u/ec9cab/32.png) [@ultron\_code](https://community.deeplearning.ai/u/ultron_code)\
**Post date:** [April 9, 2023, 9:47am UTC](https://community.deeplearning.ai/t/linear-algebra-for-ml-wk3-assignment-exercise-4/305633/3 "2023-04-09T09:47:03Z")

</div>

```
Here's what you need to do ...

```

{moderator edit: code removed}

---

<div class="post-metadata">

**Author:** ![plaudev](https://avatars.discourse-cdn.com/v4/letter/p/9de0a6/32.png) [@plaudev](https://community.deeplearning.ai/u/plaudev)\
**Post date:** [April 9, 2023, 4:18pm UTC](https://community.deeplearning.ai/t/linear-algebra-for-ml-wk3-assignment-exercise-4/305633/4 "2023-04-09T16:18:59Z")

</div>

thanks for trying TMosh

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [April 9, 2023, 4:27pm UTC](https://community.deeplearning.ai/t/linear-algebra-for-ml-wk3-assignment-exercise-4/305633/5 "2023-04-09T16:27:25Z")

</div>

Generally posting code for the assignments is not allowed. That’s why I gave guidance instead of just providing the answer.

---

<div class="post-metadata">

**Author:** ![plaudev](https://avatars.discourse-cdn.com/v4/letter/p/9de0a6/32.png) [@plaudev](https://community.deeplearning.ai/u/plaudev)\
**Post date:** [April 9, 2023, 4:28pm UTC](https://community.deeplearning.ai/t/linear-algebra-for-ml-wk3-assignment-exercise-4/305633/6 "2023-04-09T16:28:56Z")

</div>

thanks, ultron\_code. That works and in the following step `print(Y_hat)` is giving the expected output.

However the `w3_unittest.test_forward_propagation(forward_propagation)` is failing as follows:

> ValueError Traceback (most recent call last)  
> in   
> ----\> 1 w3\_unittest.test\_forward\_propagation(forward\_propagation)
> 
> ~/work/w3\_unittest.py in test\_forward\_propagation(target\_forward\_propagation)  
> 286  
> 287 for test\_case in test\_cases:  
> → 288 result = target\_forward\_propagation(test\_case[“input”][“X”], test\_case[“input”][“parameters”])  
> 289  
> 290 try:
> 
> in forward\_propagation(X, parameters)  
> 26 print(f"m = {m}")  
> 27  
> —\> 28 Z = W \* X + b  
> 29 Y\_hat = Z  
> 30 ### END CODE HERE ###
> 
> ValueError: operands could not be broadcast together with shapes (1,2) (2,5)

my code is:

```
# Retrieve each parameter from the dictionary "parameters".
### START CODE HERE ### (~ 2 lines of code)
# moderator edit: code removed
### END CODE HERE ###

# Implement Forward Propagation to calculate Z.
### START CODE HERE ### (~ 2 lines of code)   
print ('The shape of X: ' + str(X.shape))
print (f'The shape of W: {W.shape}')
print (f'The shape of b: {b.shape}\n')    

#moderator edit: code removed

### END CODE HERE ###

```

and the print output is:

The shape of X: (1, 30)  
The shape of W: (1, 1)  
The shape of b: (1, 1)

The shape of X: (1, 30)  
The shape of W: (1, 1)  
The shape of b: (1, 1)

The shape of X: (2, 5)  
The shape of W: (1, 2)  
The shape of b: (1, 1)

why do the shapes of `X` and `W` change? thanks

---

<div class="post-metadata">

**Author:** ![plaudev](https://avatars.discourse-cdn.com/v4/letter/p/9de0a6/32.png) [@plaudev](https://community.deeplearning.ai/u/plaudev)\
**Post date:** [April 9, 2023, 5:49pm UTC](https://community.deeplearning.ai/t/linear-algebra-for-ml-wk3-assignment-exercise-4/305633/7 "2023-04-09T17:49:49Z")

</div>

so I figured out my error was doing {moderator edit: code removed}. With this change the test cases in exercises 4 & 5 both passed.

however my print statements as shown in my previous reply still are showing that the shapes of `X` and `W` change from `(1,30)` and `(1,1)` to `(2,5)` and `(1,2)` somewhere along the line during the test cases despite all the expected output are generated. Why is that? thanks

---

<div class="post-metadata">

**Author:** ![Ginny\_Khue\_Dang](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/ginny_khue_dang/32/71659_2.png) [@Ginny\_Khue\_Dang](https://community.deeplearning.ai/u/Ginny_Khue_Dang)\
**Post date:** [August 11, 2023, 2:29pm UTC](https://community.deeplearning.ai/t/linear-algebra-for-ml-wk3-assignment-exercise-4/305633/8 "2023-08-11T14:29:52Z")

</div>

@plaudev Thanks for posting this. I got the same error, and your guidance was absolutely helpful!

---

<div class="post-metadata">

**Author:** ![TMosh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/tmosh/32/9321_2.png) [@TMosh](https://community.deeplearning.ai/u/TMosh)\
**Post date:** [December 5, 2023, 8:12pm UTC](https://community.deeplearning.ai/t/linear-algebra-for-ml-wk3-assignment-exercise-4/305633/9 "2023-12-05T20:12:59Z")

</div>

Closed on December 5, 2023, 8:12pm UTC


