Week 2 vectorization

Hi, in week 2 vectorization was used to compute dw, dz etc. as shown below. But what about J? How is it calculated without using a loop?

Hi J is being calculated inside the loop, there is a line where J is updated adding up the cost of the i iteration. Then finally the value is divided by m outside the loop.

1 Like

Hi, the vectorized (right-hand side) version as shown doesn’t calculate J directly, but leaps to the dZ value with the magical A - Y formula which combines the derivative calculation for the loss and the preceding sigmoid layer.

A direct vectorized calculation of J can be done (the operations in their numpy form will run on vectors quite happily.

The A-Y expression has the advantage of being both more accurate and really fast to compute, so it is very widely used.

1 Like