W4_A 2_Ex-1_two_layer_model

I’m strugling trying to understand the cost function used in this exercise. We can use the the compute_cost function given in the assignment and this of course works very well. However I tried to use my own calculated function based on the previous assignment weeks. I’m not sure if I can put here the codes for both versions of the cost function, but I checked the one dnn_app_utils_v3.py code, and it looks very different to the one we could intuitively derive from the equation:

Could someone please explain why the compute_cost function given in the assignment ( i.e in dnn_app_utils_v3.py) doesn’t have a summation and the log of A2 is transposed? Thank you!


if I understand your question correctly …when he use A2.T and doesn’t have a summation. he use Matrix multiplication as when use matrix multiply (Vectorization expression) it multiply every element as element wise and it calculate the sum of the result

I hope I answer you question,
please feel free to ask any question,

There are (at least) two ways to compute each of the two terms of the cost:

  1. You can use elementwise multiply, followed by sum.
  2. You can do both the multiply and the sum in a single operation by using a dot product (np.dot). But to get the dimensions to work for a dot product, you need to first transpose the second argument.

If you don’t understand how matrix multiply works (dot product style), you should go take a basic linear algebra class first. Understanding the basic operations of linear algebra is a prerequisite for this course.

Here’s another thread that talks more about what dot product means.

Thank you both for your answers. I understand both cases but I posted the question because there must be something I’m missing since I decided to compute the cost with the summation and elementwise multiplication instead but it never passes the fourth test. Let me know if I should delete the code, but I have tried this:

{moderator edit - solution code removed}

What is it that I’m doing wrong here?

Thank you

That code looks correct to me. Are you sure you did “Shift-Enter” on the actual code cell after you made your most recent change? Just calling the function again without doing that runs the old code.

The other possibility is that the AL or m values are not correct for some reason.

If it is still failing, please show us the output of the tests that you are seeing.

I’m sure I have run the correct cells in this case, but I keep getting the same error (see below). When I run the code with the “default” function (the one in dnn_app_utils_v3.py), then it passes all the tests:

Ah, ok, the problem is that in this exercise, you should not be computing the cost directly yourself: you should simply be calling the function compute_cost. It’s also a mistake to “hand copy” over your version of that function from the Step by Step exercise: they provide you with a version of that function, so you should just call it. It turns out that in Step by Step, they would let you return the cost from compute_cost either as a scalar float or as a 1 x 1 array, but here they force it to be a 1 x 1 array for some reason.

Thank you. I now know then that the cost I was calculating was correct