Main Page   Modules   Data Structures   File List   Data Fields   Globals   Related Pages  

gnn_weight_decay.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *  @file gnn_weight_decay.c
00003  *  @brief gnn_weight_decay Implementation.
00004  *
00005  *  @date   : 03-10-03 21:21
00006  *  @author : Pedro Ortega C. <peortega@dcc.uchile.cl>
00007  *  Copyright  2003  Pedro Ortega C.
00008  ****************************************************************************/
00009 /*
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU General Public License
00021  *  along with this program; if not, write to the Free Software
00022  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00023  */
00024 
00025 /**
00026  * @brief Weight Decay Regularization.
00027  * @defgroup gnn_weight_decay_doc gnn_weight_decay : Weight Decay Regularization.
00028  * @ingroup gnn_criterion_doc
00029  *
00030  * This datatype implements the \em Weight \em Decay Regularization, given
00031  * by
00032  * \f[ \Omega = \frac{1}{2} \sum_i w_i^2 \f]
00033  * Where the \f$w_i, i=0,\ldots,L\f$ are choosen parameters. They are usually
00034  * those of a \ref gnn_weight linear transform node.
00035  *
00036  * The gradient is:
00037  * \f[ \frac{\partial \widetilde{E}}{\partial w_l} =
00038  *         \frac{\partial E}{\partial w_l} + \nu w_l
00039  * \f]
00040  */
00041 
00042 
00043 
00044 /******************************************/
00045 /* Include Files                          */
00046 /******************************************/
00047 
00048 #include <math.h>
00049 #include "gnn_weight_decay.h"
00050 
00051 
00052 
00053 /******************************************/
00054 /* Static Declaration                     */
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 /* Static Implementation                  */
00088 /******************************************/
00089 
00090 /**
00091  * @brief The evaluation function.
00092  * @ingroup gnn_weight_decay_doc
00093  *
00094  * This function corresponds to the evaluation function.
00095  *
00096  * @param  crit A pointer to a \ref gnn_weight_decay criterion.
00097  * @param  y    A pointer to an estimation vector \f$y\f$.
00098  * @param  t    A pointer to the desired target vector \f$t\f$.
00099  * @return A real number corresponding to the value of the criterion.
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     /* get view as a gnn_weight_decay */
00116     wc = (gnn_weight_decay *) crit;
00117 
00118     /* check sizes */
00119     if (y->size != t->size)
00120         GSL_ERROR_VAL ("vector sizes should be the same", GSL_EINVAL, 0.0);
00121 
00122     /* compute subnode error */
00123     E = gnn_criterion_evaluate_e (wc->subcrit, y, t);
00124 
00125     /* get parameter vector */
00126     gnn_pbundle_get_w (wc->pb, wc->w);
00127 
00128     /* compute penalization term */
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  * @brief The gradient evaluation function.
00144  * @ingroup gnn_weight_decay_doc
00145  *
00146  * This function implements criterion's gradient evaluation function.
00147  *
00148  * @param  crit A pointer to a \ref gnn_weight_decay criterion.
00149  * @param  y    A pointer to an estimation vector \f$y\f$.
00150  * @param  t    A pointer to the desired target vector \f$t\f$.
00151  * @param  dy   A pointer to a buffer vector where the result should be placed.
00152  * @return Returns 0 if succeeded.
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     /* get view as a gnn_weight_decay */
00169     wc = (gnn_weight_decay *) crit;
00170 
00171     /* compute subnode dy */
00172     gnn_criterion_evaluate_dy (wc->subcrit, dy);
00173 
00174     /* get parameter gradient vector */
00175     gnn_pbundle_get_dw (wc->pb, wc->dw);
00176 
00177     /* compute gradient penalization terms */
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     /* set parameter gradient vector penalization */
00192     gnn_pbundle_set_dw (wc->pb, wc->dw);
00193 
00194     return 0;
00195 }
00196 
00197 /**
00198  * @brief The destroy function.
00199  * @ingroup gnn_weight_decay_doc
00200  *
00201  * This function implements destroy function.
00202  *
00203  * @param  crit A pointer to a \ref gnn_weight_decay criterion.
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     /* get view as a gnn_weight_decay */
00213     wc = (gnn_weight_decay *) crit;
00214 
00215     /* free allocate memory */
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 /* Public Interface                       */
00230 /******************************************/
00231 
00232 /**
00233  * @brief Creates a Weight Decay regularization for \ref gnn_weight.
00234  * @ingroup gnn_weight_decay_doc
00235  *
00236  * This function builds a new \ref gnn_weight_decay regularizator for
00237  * all \ref gnn_weight nodes contained in the given node \a node.
00238  *
00239  * @param  crit A pointer to a \ref gnn_weight_decay criterion.
00240  * @param  nu   The regularization coefficient \f$nu\f$.
00241  * @param  node A pointer to a \ref gnn_node.
00242  * @return Returns a pointer to a \ref gnn_weight_decay if succeeded or NULL.
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     /* get parameter bundle consisting of gnn_weight nodes' params */
00254     pb = gnn_node_sub_search_params (node, "gnn_weight");
00255 
00256     /* create weight decay penalizer */
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  * @brief Creates a Weight Decay regularization for a given type.
00270  * @ingroup gnn_weight_decay_doc
00271  *
00272  * This function builds a new \ref gnn_weight_decay regularizator for
00273  * all nodes of type \a type contained in the given node \a node.
00274  *
00275  * @param  crit A pointer to a \ref gnn_weight_decay criterion.
00276  * @param  nu   The regularization coefficient \f$nu\f$.
00277  * @param  node A pointer to a \ref gnn_node.
00278  * @param  type A string containing the type.
00279  * @return Returns a pointer to a \ref gnn_weight_decay if succeeded or NULL.
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     /* get parameter bundle consisting of  */
00295     /* the nodes' params of the given type */
00296     pb = gnn_node_sub_search_params (node, type);
00297 
00298     /* create weight decay penalizer */
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  * @brief Creates a Weight Decay regularization for a given parameter bundle.
00312  * @ingroup gnn_weight_decay_doc
00313  *
00314  * This function builds a new \ref gnn_weight_decay regularizator for
00315  * all parameters contained in the given parameter bundle \a pb.
00316  *
00317  * @param  crit A pointer to a \ref gnn_weight_decay criterion.
00318  * @param  nu   The regularization coefficient \f$nu\f$.
00319  * @param  pb   A \ref gnn_pbundle.
00320  * @return Returns a pointer to a \ref gnn_weight_decay if succeeded or NULL.
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     /* check for positive penalty term coefficient */
00337     if (nu < 0.0)
00338     {
00339         GSL_ERROR_VAL ("penalty term coefficient should be positive",
00340                        GSL_EINVAL, NULL);
00341     }
00342 
00343     /* alloc memory */
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     /* get view as gnn_criterion */
00352     c = (gnn_criterion *) wc;
00353     
00354     /* get size */
00355     size = gnn_criterion_get_size (crit);
00356     
00357     /* initialize */
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     /* get number of penalized parameters */
00372     l = gnn_pbundle_get_size (pb);
00373 
00374     /* set fields */
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  * @brief Sets the regularization coefficient.
00393  * @ingroup gnn_weight_decay_doc
00394  *
00395  * This function sets a new regularization coefficient for the
00396  * \ref gnn_weight_decay regularizer. It should be positive.
00397  *
00398  * @param  crit A pointer to a \ref gnn_weight_decay criterion.
00399  * @param  nu   The new regularization coefficient \f$nu\f$.
00400  * @return Returns 0 if succeeded.
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  * @brief Gets the regularization coefficient.
00422  * @ingroup gnn_weight_decay_doc
00423  *
00424  * This function gets the currently used regularization coefficient of the
00425  * \ref gnn_weight_decay regularizer.
00426  *
00427  * @param  crit A pointer to a \ref gnn_weight_decay criterion.
00428  * @return Returns the regularization coefficient \f$nu\f$.
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 

Generated on Sun Jun 13 20:50:13 2004 for libgnn Gradient Retropropagation Machine Library by doxygen1.2.18