1

I'm going through Andrew Ng's course on machine learning and am currently writing a forward propagation code in MATLAB/Octave that solves this cost function:

enter image description here

Currently, I have wrote it in a for loop form like this:

for i= 1:m
  for j= 1:num_labels
    J = J + log(ht(j,i))*y(j,i) + log(1-ht(j,i))*(1-y(j,i));
  end
end

J = -J/m;

And that gets the job done. However, I would like to simplify this code, as I always feel a little "dirty" using for loops in MATLAB when I feel I could be using a vectorized form. But the for loops seem natural since there are 2 summations.

gnovice
  • 125,304
  • 15
  • 256
  • 359
Davis Owen
  • 57
  • 1
  • 9

1 Answers1

2

This line should give you the same result as your loop code above:

J = -sum(log(ht(:)).*y(:) + log(1-ht(:)).*(1-y(:)))/m;

The colon operator (:) is used to reshape each matrix into a column vector so that after element-wise multiplication a single call to sum can be used to add up all the results.

gnovice
  • 125,304
  • 15
  • 256
  • 359
  • Nice! Just curious, is this kind of implementation faster than the nested for loop method? – Davis Owen May 09 '17 at 04:21
  • 1
    @DavisOwen: It probably depends somewhat on how large `m` and `num_labels` are. Loops in current versions of MATLAB aren't as terrible as they once were, so for a simple enough operation as this there might not be a huge difference in speed. My gut says you'd probably see a moderate improvement from vectorization. When in doubt, you can just [`timeit`](https://www.mathworks.com/help/matlab/ref/timeit.html)! – gnovice May 09 '17 at 04:26