XCSF 1.4.8
XCSF learning classifier system
Loading...
Searching...
No Matches
loss.c
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 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 */
15
24#include "loss.h"
25#include "utils.h"
26
34double
35loss_mae(const struct XCSF *xcsf, const double *pred, const double *y)
36{
37 double error = 0;
38 for (int i = 0; i < xcsf->y_dim; ++i) {
39 error += fabs(y[i] - pred[i]);
40 }
41 error /= xcsf->y_dim;
42 return error;
43}
44
52double
53loss_mse(const struct XCSF *xcsf, const double *pred, const double *y)
54{
55 double error = 0;
56 for (int i = 0; i < xcsf->y_dim; ++i) {
57 error += (y[i] - pred[i]) * (y[i] - pred[i]);
58 }
59 error /= xcsf->y_dim;
60 return error;
61}
62
70double
71loss_rmse(const struct XCSF *xcsf, const double *pred, const double *y)
72{
73 return sqrt(loss_mse(xcsf, pred, y));
74}
75
84double
85loss_log(const struct XCSF *xcsf, const double *pred, const double *y)
86{
87 double error = 0;
88 for (int i = 0; i < xcsf->y_dim; ++i) {
89 error += y[i] * log(fmax(pred[i], DBL_EPSILON));
90 }
91 return -error;
92}
93
101double
102loss_binary_log(const struct XCSF *xcsf, const double *pred, const double *y)
103{
104 double error = 0;
105 for (int i = 0; i < xcsf->y_dim; ++i) {
106 error += y[i] * log(fmax(pred[i], DBL_EPSILON)) +
107 (1 - y[i]) * log(fmax((1 - pred[i]), DBL_EPSILON));
108 }
109 return -error;
110}
111
119double
120loss_onehot(const struct XCSF *xcsf, const double *pred, const double *y)
121{
122 const int max_i = argmax(pred, xcsf->y_dim);
123 if (y[max_i] != 1) {
124 return 1;
125 }
126 return 0;
127}
128
136double
137loss_huber(const struct XCSF *xcsf, const double *pred, const double *y)
138{
139 double error = 0;
140 const double delta = xcsf->HUBER_DELTA;
141 for (int i = 0; i < xcsf->y_dim; ++i) {
142 const double a = y[i] - pred[i];
143 if (fabs(a) > delta) {
144 error += 0.5 * delta * delta + delta * (fabs(a) - delta);
145 } else {
146 error += 0.5 * a * a;
147 }
148 }
149 error /= xcsf->y_dim;
150 return error;
151}
152
158int
160{
161 switch (xcsf->LOSS_FUNC) {
162 case LOSS_MAE:
163 xcsf->loss_ptr = &loss_mae;
164 return LOSS_MAE;
165 case LOSS_MSE:
166 xcsf->loss_ptr = &loss_mse;
167 return LOSS_MSE;
168 case LOSS_RMSE:
169 xcsf->loss_ptr = &loss_rmse;
170 return LOSS_RMSE;
171 case LOSS_LOG:
172 xcsf->loss_ptr = &loss_log;
173 return LOSS_LOG;
174 case LOSS_BINARY_LOG:
175 xcsf->loss_ptr = &loss_binary_log;
176 return LOSS_BINARY_LOG;
177 case LOSS_ONEHOT:
178 xcsf->loss_ptr = &loss_onehot;
179 return LOSS_ONEHOT;
180 case LOSS_HUBER:
181 xcsf->loss_ptr = &loss_huber;
182 return LOSS_HUBER;
183 default:
184 return LOSS_INVALID;
185 }
186}
187
193const char *
194loss_type_as_string(const int type)
195{
196 switch (type) {
197 case LOSS_MAE:
198 return LOSS_STRING_MAE;
199 case LOSS_MSE:
200 return LOSS_STRING_MSE;
201 case LOSS_RMSE:
202 return LOSS_STRING_RMSE;
203 case LOSS_LOG:
204 return LOSS_STRING_LOG;
205 case LOSS_BINARY_LOG:
207 case LOSS_ONEHOT:
208 return LOSS_STRING_ONEHOT;
209 case LOSS_HUBER:
210 return LOSS_STRING_HUBER;
211 default:
212 printf("loss_type_as_string(): invalid type: %d\n", type);
213 exit(EXIT_FAILURE);
214 }
215}
216
222int
223loss_type_as_int(const char *type)
224{
225 if (strncmp(type, LOSS_STRING_MAE, 4) == 0) {
226 return LOSS_MAE;
227 }
228 if (strncmp(type, LOSS_STRING_MSE, 4) == 0) {
229 return LOSS_MSE;
230 }
231 if (strncmp(type, LOSS_STRING_RMSE, 5) == 0) {
232 return LOSS_RMSE;
233 }
234 if (strncmp(type, LOSS_STRING_LOG, 4) == 0) {
235 return LOSS_LOG;
236 }
237 if (strncmp(type, LOSS_STRING_BINARY_LOG, 11) == 0) {
238 return LOSS_BINARY_LOG;
239 }
240 if (strncmp(type, LOSS_STRING_ONEHOT, 7) == 0) {
241 return LOSS_ONEHOT;
242 }
243 if (strncmp(type, LOSS_STRING_HUBER, 6) == 0) {
244 return LOSS_HUBER;
245 }
246 return LOSS_INVALID;
247}
double loss_binary_log(const struct XCSF *xcsf, const double *pred, const double *y)
Binary logistic log loss for binary-class classification.
Definition loss.c:102
const char * loss_type_as_string(const int type)
Returns a string representation of a loss type from the integer.
Definition loss.c:194
double loss_rmse(const struct XCSF *xcsf, const double *pred, const double *y)
Root mean squared error loss function.
Definition loss.c:71
double loss_onehot(const struct XCSF *xcsf, const double *pred, const double *y)
One-hot classification error.
Definition loss.c:120
double loss_log(const struct XCSF *xcsf, const double *pred, const double *y)
Logistic log loss for multi-class classification.
Definition loss.c:85
int loss_type_as_int(const char *type)
Returns the integer representation of a loss type given a name.
Definition loss.c:223
double loss_mae(const struct XCSF *xcsf, const double *pred, const double *y)
Mean absolute error loss function.
Definition loss.c:35
double loss_mse(const struct XCSF *xcsf, const double *pred, const double *y)
Mean squared error loss function.
Definition loss.c:53
int loss_set_func(struct XCSF *xcsf)
Sets the XCSF error function to the implemented function.
Definition loss.c:159
double loss_huber(const struct XCSF *xcsf, const double *pred, const double *y)
Huber loss function.
Definition loss.c:137
Loss functions for calculating prediction error.
#define LOSS_RMSE
Root mean squared error.
Definition loss.h:31
#define LOSS_MSE
Mean squared error.
Definition loss.h:30
#define LOSS_STRING_MSE
Mean squared error.
Definition loss.h:39
#define LOSS_STRING_LOG
Log loss.
Definition loss.h:41
#define LOSS_STRING_MAE
Mean absolute error.
Definition loss.h:38
#define LOSS_STRING_HUBER
Huber loss.
Definition loss.h:44
#define LOSS_BINARY_LOG
Binary log loss.
Definition loss.h:33
#define LOSS_MAE
Mean absolute error.
Definition loss.h:29
#define LOSS_STRING_ONEHOT
One-hot classification error.
Definition loss.h:43
#define LOSS_STRING_BINARY_LOG
Binary log loss.
Definition loss.h:42
#define LOSS_LOG
Log loss.
Definition loss.h:32
#define LOSS_ONEHOT
One-hot encoding classification error.
Definition loss.h:34
#define LOSS_STRING_RMSE
Root mean squared error.
Definition loss.h:40
#define LOSS_HUBER
Huber loss.
Definition loss.h:35
#define LOSS_INVALID
Error code for invalid loss type.
Definition loss.h:28
XCSF data structure.
Definition xcsf.h:85
Utility functions for random number handling, etc.
static int argmax(const double *X, const int N)
Returns the index of the largest element in vector X.
Definition utils.h:86