SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LogLoss.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2011 Shashwat Lal Das
8  * Copyright (c) 2011 Berlin Institute of Technology and Max-Planck-Society.
9  */
10 
11 #include <shogun/loss/LogLoss.h>
12 
13 using namespace shogun;
14 
16 {
17  float64_t z = prediction * label;
18  if (z >= 0)
19  return log(1+exp(-z));
20  return -z + log(1+exp(z));
21 }
22 
24 {
25  float64_t z = prediction * label;
26  if (z < 0)
27  return -1 / (exp(z) + 1);
28  float64_t ez = exp(-z);
29  return -ez / (ez + 1);
30 }
31 
33 {
34  float64_t z = prediction * label;
35  float64_t ez = exp(z);
36 
37  return ez / (ez*(ez + 2) + 1);
38 }
39 
41 {
42  float64_t w,x;
43  float64_t d = exp(label * prediction);
44  if(eta_t < 1e-6){
45  /* As with squared loss, for small eta_t we replace the update
46  * with its first order Taylor expansion to avoid numerical problems
47  */
48  return label*eta_t/((1+d)*norm);
49  }
50  x = eta_t + label*prediction + d;
51 
52  /* This piece of code is approximating W(exp(x))-x.
53  * W is the Lambert W function: W(z)*exp(W(z))=z.
54  * The absolute error of this approximation is less than 9e-5.
55  * Faster/better approximations can be substituted here.
56  */
57  float64_t W = x>=1. ? 0.86*x+0.01 : exp(0.8*x-0.65); //initial guess
58  float64_t r = x>=1. ? x-log(W)-W : 0.2*x+0.65-W; //residual
59  float64_t t = 1.+W;
60  float64_t u = 2.*t*(t+2.*r/3.); //magic
61  w = W*(1.+r/t*(u-r)/(u-2.*r))-x; //more magic
62 
63  return -(label*w+prediction)/norm;
64 }
65 
67 {
68  float64_t d = first_derivative(prediction, label);
69  return d*d;
70 }
71 

SHOGUN Machine Learning Toolbox - Documentation