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

gnn_mse.c

Go to the documentation of this file.
00001 /***************************************************************************
00002  *  @file gnn_mse.h
00003  *  @brief Mean Square Error Criterion.
00004  *
00005  *  @date   : 25-08-03 20:17
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 Mean Square Error function.
00027  * @defgroup gnn_mse_doc gnn_mse : Mean-Square-Error Criterion.
00028  * @ingroup gnn_criterion_doc
00029  *
00030  * This datatype implements the MSE criterion, given by
00031  * \f[ E(y,t) = \frac{1}{m} \sum_i^m (y_i - t_i)^2 \f]
00032  * Although widely used, this cost-function is not always the best suited
00033  * criterion. Since the errors are squared up and summed togheter, those
00034  * components with large errors can greatly influence the learning process
00035  * during training. It has been shown that for a class of problems, the MSE
00036  * criterion can lead to trappings in local minima, failing to find a solution
00037  * although there is at least one.
00038  */
00039 
00040 
00041 
00042 /******************************************/
00043 /* Include Files                          */
00044 /******************************************/
00045 
00046 #include "gnn_mse.h"
00047 
00048 
00049 
00050 /******************************************/
00051 /* Static Declaration                     */
00052 /******************************************/
00053 
00054 typedef gnn_criterion gnn_mse;
00055 
00056 double
00057 gnn_mse_e (gnn_criterion *crit,
00058            const gsl_vector *y,
00059            const gsl_vector *t);
00060 
00061 int
00062 gnn_mse_dy (gnn_criterion *crit,
00063             const gsl_vector *y,
00064             const gsl_vector *t,
00065             gsl_vector * dy);
00066 
00067 
00068 
00069 /******************************************/
00070 /* Static Implementation                  */
00071 /******************************************/
00072 
00073 /**
00074  * @brief The evaluation function.
00075  * @ingroup gnn_mse_doc
00076  *
00077  * This function corresponds to the evaluation of the MSE criterion.
00078  *
00079  * @param  crit A pointer to a \ref gnn_mse criterion.
00080  * @param  y    A pointer to an estimation vector \f$y\f$.
00081  * @param  t    A pointer to the desired target vector \f$t\f$.
00082  * @return A real number corresponding to the value of the criterion.
00083  */
00084 double
00085 gnn_mse_e (gnn_criterion *crit,
00086            const gsl_vector *y,
00087            const gsl_vector *t)
00088 {
00089     int i;
00090     double E;
00091     
00092     assert (crit != NULL);
00093     assert (y != NULL);
00094     assert (t != NULL);
00095     
00096     /* check sizes */
00097     if (y->size != t->size)
00098         GSL_ERROR_VAL ("vector sizes should be the same", GSL_EINVAL, 0.0);
00099 
00100     /* compute error */
00101     E = 0.0;
00102     for (i=0; i<y->size; ++i)
00103     {
00104         double yi;
00105         double ti;
00106 
00107         yi = gsl_vector_get (y, i);
00108         ti = gsl_vector_get (t, i);
00109 
00110         E += (yi - ti) * (yi - ti);
00111     }
00112     E /= y->size;
00113 
00114     return E;
00115 }
00116 
00117 /**
00118  * @brief The gradient evaluation function.
00119  * @ingroup gnn_mse_doc
00120  *
00121  * This function implements the \ref gnn_mse criterion's gradient evaluation
00122  * function given by
00123  *
00124  * \f[ \frac{\partial E}{\partial y_i} = \frac{2}{m} (y_i - t_i) \f]
00125  *
00126  * @param  crit A pointer to a \ref gnn_mse criterion.
00127  * @param  y    A pointer to an estimation vector \f$y\f$.
00128  * @param  t    A pointer to the desired target vector \f$t\f$.
00129  * @param  dy   A pointer to a buffer vector where the result should be placed.
00130  * @return Returns 0 if succeeded.
00131  */
00132 int
00133 gnn_mse_dy (gnn_criterion *crit,
00134             const gsl_vector *y,
00135             const gsl_vector *t,
00136             gsl_vector * dy)
00137 {
00138     int i;
00139     
00140     assert (crit != NULL);
00141     assert (y != NULL);
00142     assert (t != NULL);
00143     assert (dy != NULL);
00144     
00145     /* check sizes */
00146     if (y->size != t->size || y->size != dy->size)
00147         GSL_ERROR ("vector sizes should be the same", GSL_EINVAL);
00148 
00149     /* compute dy */
00150     for (i=0; i<y->size; ++i)
00151     {
00152         double yi;
00153         double ti;
00154         double dyi;
00155 
00156         yi  = gsl_vector_get (y, i);
00157         ti  = gsl_vector_get (t, i);
00158         dyi = gsl_vector_get (dy, i);
00159 
00160         dyi += 2 * (yi - ti) / y->size;
00161 
00162         gsl_vector_set (dy, i, dyi);
00163     }
00164 
00165     return 0;
00166 }
00167 
00168 
00169 /******************************************/
00170 /* Public Interface                       */
00171 /******************************************/
00172 
00173 /**
00174  * @brief Creates a new \ref gnn_mse criterion.
00175  * @ingroup gnn_mse_doc
00176  *
00177  * This function creates a new \ref gnn_mse of the given size. *
00178  *
00179  * @param  size    The size of the estimation and the target vector \f$y\f$
00180  *                 and \f$t\f$.
00181  * @return Returns a pointer to a new \ref gnn_mse or NULL if failed.
00182  */
00183 gnn_criterion *
00184 gnn_mse_new (size_t size)
00185 {
00186     int status;
00187     gnn_criterion *crit;
00188     
00189     /* check size */
00190     if (size < 1)
00191         GSL_ERROR_VAL ("size should be strictly positive", GSL_EINVAL, NULL);
00192 
00193     /* alloc memory */
00194     crit = (gnn_criterion *) malloc (sizeof (*crit));
00195     if (crit == NULL)
00196         GSL_ERROR_VAL ("couldn't alloc memory for gnn_mse", GSL_ENOMEM, NULL);
00197 
00198     /* initialize */
00199     status = gnn_criterion_init  (crit, "gnn_mse", size,
00200                                   gnn_mse_e, gnn_mse_dy, NULL);
00201     if (status)
00202     {
00203         gnn_criterion_destroy (crit);
00204         GSL_ERROR_VAL ("couldn't initialize gnn_mse", GSL_EFAILED, NULL);
00205     }
00206     
00207     return crit;
00208 }
00209 
00210 
00211 

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