00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #include <math.h>
00049 #include "gnn_weight_decay.h"
00050
00051
00052
00053
00054
00055
00056
00057 typedef struct _gnn_weight_decay gnn_weight_decay;
00058
00059 struct _gnn_weight_decay
00060 {
00061 gnn_criterion crit;
00062 gnn_criterion *subcrit;
00063 gnn_pbundle *pb;
00064 gsl_vector *w;
00065 gsl_vector *dw;
00066 double nu;
00067 };
00068
00069
00070 double
00071 gnn_weight_decay_e (gnn_criterion *crit,
00072 const gsl_vector *y,
00073 const gsl_vector *t);
00074
00075 int
00076 gnn_weight_decay_dy (gnn_criterion *crit,
00077 const gsl_vector *y,
00078 const gsl_vector *t,
00079 gsl_vector * dy);
00080
00081 static void
00082 gnn_weight_decay_destroy (gnn_criterion *crit);
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101 double
00102 gnn_weight_decay_e (gnn_criterion *crit,
00103 const gsl_vector *y,
00104 const gsl_vector *t)
00105 {
00106 int i;
00107 double E;
00108 double Ep;
00109 gnn_weight_decay *wc;
00110
00111 assert (crit != NULL);
00112 assert (y != NULL);
00113 assert (t != NULL);
00114
00115
00116 wc = (gnn_weight_decay *) crit;
00117
00118
00119 if (y->size != t->size)
00120 GSL_ERROR_VAL ("vector sizes should be the same", GSL_EINVAL, 0.0);
00121
00122
00123 E = gnn_criterion_evaluate_e (wc->subcrit, y, t);
00124
00125
00126 gnn_pbundle_get_w (wc->pb, wc->w);
00127
00128
00129 Ep = 0.0;
00130 for (i=0; i<wc->w->size; ++i)
00131 {
00132 double wi;
00133
00134 wi = gsl_vector_get (wc->w, i);
00135
00136 Ep += wi * wi;
00137 }
00138
00139 return E + 0.5 * wc->nu * Ep;
00140 }
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154 int
00155 gnn_weight_decay_dy (gnn_criterion *crit,
00156 const gsl_vector *y,
00157 const gsl_vector *t,
00158 gsl_vector * dy)
00159 {
00160 int i;
00161 gnn_weight_decay *wc;
00162
00163 assert (crit != NULL);
00164 assert (y != NULL);
00165 assert (t != NULL);
00166 assert (dy != NULL);
00167
00168
00169 wc = (gnn_weight_decay *) crit;
00170
00171
00172 gnn_criterion_evaluate_dy (wc->subcrit, dy);
00173
00174
00175 gnn_pbundle_get_dw (wc->pb, wc->dw);
00176
00177
00178 for (i=0; i<wc->w->size; ++i)
00179 {
00180 double wi;
00181 double dwi;
00182
00183 wi = gsl_vector_get (wc->w, i);
00184 dwi = gsl_vector_get (wc->dw, i);
00185
00186 dwi += wc->nu * wi;
00187
00188 gsl_vector_set (wc->dw, i, dwi);
00189 }
00190
00191
00192 gnn_pbundle_set_dw (wc->pb, wc->dw);
00193
00194 return 0;
00195 }
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205 static void
00206 gnn_weight_decay_destroy (gnn_criterion *crit)
00207 {
00208 gnn_weight_decay *wc;
00209
00210 assert (crit != NULL);
00211
00212
00213 wc = (gnn_weight_decay *) crit;
00214
00215
00216 if (wc->subcrit != NULL)
00217 gnn_criterion_destroy (wc->subcrit);
00218 if (wc->pb != NULL)
00219 gnn_pbundle_destroy (wc->pb);
00220 if (wc->w != NULL)
00221 gsl_vector_free (wc->w);
00222 if (wc->dw != NULL)
00223 gsl_vector_free (wc->dw);
00224 }
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244 gnn_criterion *
00245 gnn_weight_decay_new (gnn_criterion *crit, double nu, gnn_node *node)
00246 {
00247 gnn_pbundle *pb;
00248 gnn_weight_decay *wc;
00249
00250 assert (crit != NULL);
00251 assert (node != NULL);
00252
00253
00254 pb = gnn_node_sub_search_params (node, "gnn_weight");
00255
00256
00257 wc = (gnn_weight_decay *) gnn_weight_decay_new_with_pbundle (crit, nu, pb);
00258 if (wc == NULL)
00259 {
00260 gnn_pbundle_destroy (pb);
00261 GSL_ERROR_VAL ("couldn't create gnn_weight_decay regularizer",
00262 GSL_EFAILED, NULL);
00263 }
00264
00265 return (gnn_criterion *) wc;
00266 }
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281 gnn_criterion *
00282 gnn_weight_decay_new_with_type (gnn_criterion *crit,
00283 double nu,
00284 gnn_node *node,
00285 const char *type)
00286 {
00287 gnn_pbundle *pb;
00288 gnn_weight_decay *wc;
00289
00290 assert (crit != NULL);
00291 assert (node != NULL);
00292 assert (type != NULL);
00293
00294
00295
00296 pb = gnn_node_sub_search_params (node, type);
00297
00298
00299 wc = (gnn_weight_decay *) gnn_weight_decay_new_with_pbundle (crit, nu, pb);
00300 if (wc == NULL)
00301 {
00302 gnn_pbundle_destroy (pb);
00303 GSL_ERROR_VAL ("couldn't create gnn_weight_decay regularizer",
00304 GSL_EFAILED, NULL);
00305 }
00306
00307 return (gnn_criterion *) wc;
00308 }
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322 gnn_criterion *
00323 gnn_weight_decay_new_with_pbundle (gnn_criterion *crit,
00324 double nu,
00325 gnn_pbundle *pb)
00326 {
00327 int status;
00328 size_t l;
00329 size_t size;
00330 gnn_criterion *c;
00331 gnn_weight_decay *wc;
00332
00333 assert (crit != NULL);
00334 assert (pb != NULL);
00335
00336
00337 if (nu < 0.0)
00338 {
00339 GSL_ERROR_VAL ("penalty term coefficient should be positive",
00340 GSL_EINVAL, NULL);
00341 }
00342
00343
00344 wc = (gnn_weight_decay *) malloc (sizeof (gnn_weight_decay));
00345 if (wc == NULL)
00346 {
00347 GSL_ERROR_VAL ("couldn't alloc memory for gnn_weight_decay",
00348 GSL_ENOMEM, NULL);
00349 }
00350
00351
00352 c = (gnn_criterion *) wc;
00353
00354
00355 size = gnn_criterion_get_size (crit);
00356
00357
00358 status = gnn_criterion_init (c,
00359 "gnn_weight_decay",
00360 size,
00361 gnn_weight_decay_e,
00362 gnn_weight_decay_dy,
00363 gnn_weight_decay_destroy);
00364 if (status)
00365 {
00366 gnn_criterion_destroy (c);
00367 GSL_ERROR_VAL ("couldn't initialize gnn_weight_decay",
00368 GSL_EFAILED, NULL);
00369 }
00370
00371
00372 l = gnn_pbundle_get_size (pb);
00373
00374
00375 wc->subcrit = crit;
00376 wc->pb = pb;
00377 wc->w = gsl_vector_alloc (l);
00378 wc->dw = gsl_vector_alloc (l);
00379 wc->nu = nu;
00380
00381 if (wc->dw == NULL || wc->w == NULL)
00382 {
00383 gnn_criterion_destroy (c);
00384 GSL_ERROR_VAL ("couldn't allocate memory for internal "
00385 "gnn_weight_decay buffer", GSL_EFAILED, NULL);
00386 }
00387
00388 return c;
00389 }
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402 int
00403 gnn_weight_decay_set_nu (gnn_criterion *crit, double nu)
00404 {
00405 gnn_weight_decay *wc;
00406
00407 assert (crit != NULL);
00408
00409 if (nu < 0.0)
00410 {
00411 GSL_ERROR ("penalty term coefficient should be positive", GSL_EINVAL);
00412 }
00413
00414 wc = (gnn_weight_decay *) crit;
00415 wc->nu = nu;
00416
00417 return 0;
00418 }
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430 double
00431 gnn_weight_decay_get_nu (gnn_criterion *crit)
00432 {
00433 gnn_weight_decay *wc;
00434
00435 assert (crit != NULL);
00436
00437 wc = (gnn_weight_decay *) crit;
00438 return wc->nu;
00439 }
00440
00441
00442
00443