# Wk2 Assignment Ex 5. Implementing cost fn in code

According to my understanding of the cost function, we should be multiplying columns of the Y vector with columns of the log(A) vector, which technically means y(i) * log(a(i)) (and the same for the remaining parts of the formula) and then summing up these values row by row. How can I implement this operation on code?

Hi mate, unfortunately, we cannot give any hint or clue related to coding implemetation because it violates the honor code. However, the answers are hidden in the notebook itself. Also, we have learnt vectorization and used some useful numpy functions. I believe these operations are mentioned on notebook. Hence, please read the notebook carefully.

Hi Muhammed thanks for your reply. Is my understanding of the operation as stated correct tho? If thats ok to ask.

Yes, your understanding is correct. Note that both Y and A are row vectors of dimension 1 x m, where m is the number of samples.

@MuhammedHasanKayapin is correct that we aren’t supposed to share actual solution code with each other, since everyone is supposed to submit their own work. But it’s ok to talk in general about how to code things without actually writing out the code. E.g. if you want to multiply the corresponding elements of two vectors or matrices of the same size, you can use the * operator or the np.multiply function. That will give you another vector or matrix of the same size, of course. Then to add up the elements of that, you can use np.sum. If you want to know more about how any of these functions work, just google (for example) “numpy sum” or “numpy multiply”.

The other general thing to say is that there are frequently several correct ways to achieve the same result with code. The other way to approach this is to use np.dot, which implements full “dot product” matrix multiply. Note that if you have, say, two 1 x 3 vectors and you transpose the second one so that it becomes 3 x 1, then dotting 1 x 3 dot 3 x 1 will give you a 1 x 1 result that is the sum of the products of the corresponding elements. So that’s a way to do both operation in “one shot”, so it will be more efficient, but it requires you to do the transpose first.