0

Can someone help me in coding the cross entropy loss function in Matlab. I want to code it in single line using @ i.e function handle. The error function is

E(w) = 1/N* summation(n=1..N) ln(1+ exp( -y(n)*w*x(n) ) )

N is the total number of training examples. 'w' are the parameters of the function. 'x' is a vector containing features of a training example and 'y' is the corresponding label.

Each evaluation of 'E' requires processing of all the training examples.

Thank you very much

samquest
  • 57
  • 7

1 Answers1

1

I'm not sure why this is so hard, but here's one version

E = @(w)mean(log1p(exp(-y.*w.*x)));

without knowing the dimensions of x, w, and y, I can't be sure that one or more of them don't need to be transposed (or if w is even a vector).

horchler
  • 18,384
  • 4
  • 37
  • 73
  • Thank you for the help as I am not experienced in matlab. Can you please tell me that what if it was not summation( here 'mean' can do the trick) but any other function that we want to do iteratively on the operands then what would be the code ? – samquest Aug 20 '13 at 20:09
  • I don't understand what you're asking. – horchler Aug 20 '13 at 21:40