hi, I got this error message while trying to compute cost
I have transposed Y and 1-Y and other values are correct. what could be wrong?
hi, I got this error message while trying to compute cost
I think you have a matrix product problem. Instead of a scalar for the cost, you’re getting a matrix.
There are several ways to implement the cost calculation. Ultimately you need to get a scalar value.
As Tom points out, there are a number of ways to write the code for the cost. You have two 1 x m vectors and you need the sum of the products of their corresponding elements.
Using a dot product is a fine way to do it, but you’ll need to transpose one of the vectors to get that to work. But then you need to be careful exactly how you do the transpose.
v^T dot w
gives a very different answer than
v dot w^T
Here’s a thread which discusses that point in detail and shows examples.
Thanks Paul! I transposed the other vector in the dot calculations and got the right answer. My question is how do I know which vector I need to transpose? As far as I’m concerned they all seem to be row vectors; or should I carry out shape.() to look at the vectors like Y and A before the dot calculation?
hi Tom, that was indeed the problem, thanks for the reply!
Sorry, but there is no general rule. Note that in a lot of cases Professor Ng has standalone vectors that are column vectors. Every time you are using a dot product, you need to check the shapes of the operands and you need to understand what the math formula is telling you to do in that case in order to know the order of the operands and whether any transposes are required or not.
Here’s a thread which talks about this in more detail and discusses the important notational conventions that Professor Ng uses.