SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Loss.h
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 Siddharth Kherada
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #ifndef __LOSS_H_
12 #define __LOSS_H_
13 
14 #include <shogun/lib/config.h>
15 
16 #include <shogun/lib/common.h>
17 #include <shogun/io/SGIO.h>
18 //#include <shogun/mathematics/lapack.h>
19 //#include <shogun/base/SGObject.h>
20 //#include <shogun/base/Parallel.h>
21 
22 
23 // Available losses
24 #define HINGELOSS 1
25 #define SMOOTHHINGELOSS 2
26 #define SQUAREDHINGELOSS 3
27 #define LOGLOSS 10
28 #define LOGLOSSMARGIN 11
29 
30 // Select loss
31 #define LOSS HINGELOSS
32 
33 
34 namespace shogun
35 {
38 class CLoss
39 {
40  public:
44 
45  CLoss();
46 
48  virtual ~CLoss();
50 
53  static inline float64_t loss(float64_t z)
54  {
55 #if LOSS == LOGLOSS
56  if (z >= 0)
57  return log(1+exp(-z));
58  else
59  return -z + log(1+exp(z));
60 #elif LOSS == LOGLOSSMARGIN
61  if (z >= 1)
62  return log(1+exp(1-z));
63  else
64  return 1-z + log(1+exp(z-1));
65 #elif LOSS == SMOOTHHINGELOSS
66  if (z < 0)
67  return 0.5 - z;
68  if (z < 1)
69  return 0.5 * (1-z) * (1-z);
70  return 0;
71 #elif LOSS == SQUAREDHINGELOSS
72  if (z < 1)
73  return 0.5 * (1 - z) * (1 - z);
74  return 0;
75 #elif LOSS == HINGELOSS
76  if (z < 1)
77  return 1 - z;
78  return 0;
79 #else
80 # error "Undefined loss"
81 #endif
82  }
83 
87  static inline float64_t dloss(float64_t z)
88  {
89 #if LOSS == LOGLOSS
90  if (z < 0)
91  return 1 / (exp(z) + 1);
92  float64_t ez = exp(-z);
93  return ez / (ez + 1);
94 #elif LOSS == LOGLOSSMARGIN
95  if (z < 1)
96  return 1 / (exp(z-1) + 1);
97  float64_t ez = exp(1-z);
98  return ez / (ez + 1);
99 #elif LOSS == SMOOTHHINGELOSS
100  if (z < 0)
101  return 1;
102  if (z < 1)
103  return 1-z;
104  return 0;
105 #elif LOSS == SQUAREDHINGELOSS
106  if (z < 1)
107  return (1 - z);
108  return 0;
109 #else
110  if (z < 1)
111  return 1;
112  return 0;
113 #endif
114  }
115 };
116 }
117 #endif

SHOGUN Machine Learning Toolbox - Documentation