# Week 2 Programming Assignment Exercise 5 - cost function

**URL:** <https://dlai.staged-by-discourse.com/t/week-2-programming-assignment-exercise-5-cost-function/85743>\
**Category:** Neural Networks and Deep Learning\
**Tags:** coursera-platform\
**Created:** [January 12, 2022, 3:58am UTC](https://dlai.staged-by-discourse.com/t/week-2-programming-assignment-exercise-5-cost-function/85743 "2022-01-12T03:58:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Czajkowski](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/eric_czajkowski/32/13149_2.png) [@Eric\_Czajkowski](https://dlai.staged-by-discourse.com/u/Eric_Czajkowski)\
**Post date:** [January 12, 2022, 3:58am UTC](https://dlai.staged-by-discourse.com/t/week-2-programming-assignment-exercise-5-cost-function/85743/1 "2022-01-12T03:58:21Z")

</div>

I am having a difficult time figuring out how to program the cost function for Ex. 5 on the programming assignment for Week 2.

Does one of the videos explain this explicitly?

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://dlai.staged-by-discourse.com/u/paulinpaloalto)\
**Post date:** [January 12, 2022, 4:40am UTC](https://dlai.staged-by-discourse.com/t/week-2-programming-assignment-exercise-5-cost-function/85743/2 "2022-01-12T04:40:43Z")

</div>

No, Prof Ng really doesn’t talk much about how to actually code things in the lectures. He’s talking about what the computations actually do. It’s only in the assignments that they discuss the coding. As in all cases, you start with the mathematical formulas and then the question is how to express that in code. So the first step is to understand what the math actually means.

In this case, you’ve got two vectors Y and A which are both 1 x m, where m is the number of samples. Then what you need to compute is this formula, which is the average of the loss values across all the input samples:

J = -\displaystyle \frac {1}{m} \sum\_{i = 1}^{m} [y\_i \* log(a\_i) + (1 - y\_i) \* log(1 - a\_i)]

Or you could write that as:

J = -\displaystyle \frac {1}{m} \left ( \sum\_{i = 1}^{m} y\_i \* log(a\_i) + \sum\_{i = 1}^{m}(1 - y\_i) \* log(1 - a\_i)\right )

As usual, there are lots of ways to write that in python + numpy code. But the fundamental operation in both terms is multiplying together the individual elements of two 1 x m vectors and then adding up those products to compute the sum. I can think of two obvious ways to do that which are “vectorized” (which we want for performance reasons) with numpy:

1. Use _np.multiply_ or “\*” which are two ways to express “elementwise” multiply and then use _np.sum_ to add up the products.

2. Use _np.dot_ to do both operations in one shot (multiply followed by add). But the trick there is that the rules for dot product are that the “inner” dimensions need to agree. I can’t dot 1 x m with 1 x m, right? But I could dot 1 x m times m x 1 and then I’d get a 1 x 1 or scalar result, which is what I want. So that means I would need to “transpose” the second operand in order to get that to work.

So you pick one of those methods and apply it twice: once for the Y = 1 term and once for the Y = 0 term. For more information about the various numpy functions, just google “numpy sum”, “numpy multiply” and “numpy dot”.

---

<div class="post-metadata">

**Author:** ![ai\_curious](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/ai_curious/32/128003_2.png) [@ai\_curious](https://dlai.staged-by-discourse.com/u/ai_curious)\
**Post date:** [January 12, 2022, 1:53pm UTC](https://dlai.staged-by-discourse.com/t/week-2-programming-assignment-exercise-5-cost-function/85743/3 "2022-01-12T13:53:22Z")

</div>

That’s some spiffy LaTeX there @paulinpaloalto. Nicely done

---

<div class="post-metadata">

**Author:** ![paulinpaloalto](https://sea1.discourse-cdn.com/dlaistaging/user_avatar/dlai.staged-by-discourse.com/paulinpaloalto/32/187_2.png) [@paulinpaloalto](https://dlai.staged-by-discourse.com/u/paulinpaloalto)\
**Post date:** [May 24, 2023, 1:58am UTC](https://dlai.staged-by-discourse.com/t/week-2-programming-assignment-exercise-5-cost-function/85743/4 "2023-05-24T01:58:07Z")

</div>

Closed on May 24, 2023, 1:58am UTC


