00001 /*************************************************************************** 00002 * @file gnn_evaluation.h 00003 * @brief Evaluation Header File. 00004 * 00005 * @date : 16-09-03 23:47 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 #ifndef _GNN_EVALUATION_H_ 00026 #define _GNN_EVALUATION_H_ 00027 00028 /******************************************/ 00029 /* Include Files */ 00030 /******************************************/ 00031 00032 #include <gsl/gsl_matrix.h> 00033 #include "gnn_node.h" 00034 #include "gnn_criterion.h" 00035 #include "gnn_dataset.h" 00036 #include "gnn_output.h" 00037 00038 00039 00040 /******************************************/ 00041 /* Macros */ 00042 /******************************************/ 00043 00044 /************************************/ 00045 /* a) Input Examples Evaluation */ 00046 /************************************/ 00047 00048 /** 00049 * @brief Returns a pointer to the associated \ref gnn_node. 00050 * @ingroup gnn_evaluation_doc 00051 * 00052 */ 00053 #define GNN_EVAL_GET_NODE(eval) ((eval)->node) 00054 00055 /** 00056 * @brief Returns a pointer to the associated \ref gnn_input. 00057 * @ingroup gnn_evaluation_doc 00058 * 00059 */ 00060 #define GNN_EVAL_GET_INPUT(eval) ((eval)->in) 00061 00062 /** 00063 * @brief Returns a pointer to the associated \ref gnn_output. 00064 * @ingroup gnn_evaluation_doc 00065 * 00066 */ 00067 #define GNN_EVAL_GET_OUTPUT(eval) ((eval)->out) 00068 00069 /** 00070 * @brief Returns the size of the input. 00071 * @ingroup gnn_evaluation_doc 00072 * 00073 */ 00074 #define GNN_EVAL_INPUT_SIZE(eval) ((eval)->n) 00075 00076 /** 00077 * @brief Returns the size of the output. 00078 * @ingroup gnn_evaluation_doc 00079 * 00080 */ 00081 #define GNN_EVAL_OUTPUT_SIZE(eval) ((eval)->m) 00082 00083 /** 00084 * @brief Returns the size of the parameter vector. 00085 * @ingroup gnn_evaluation_doc 00086 * 00087 */ 00088 #define GNN_EVAL_PARAMETER_SIZE(eval) ((eval)->l) 00089 00090 /** 00091 * @brief Returns the amount of examples to be evaluated by the \ref gnn_eval. 00092 * @ingroup gnn_evaluation_doc 00093 * 00094 * This macro returns the number of examples to be evaluated by the 00095 * \ref gnn_eval evaluation buffer. Basically, it is the number of 00096 * examples contained in the associated \ref gnn_input object. 00097 */ 00098 #define GNN_EVAL_AMOUNT(eval) ((eval)->P) 00099 00100 00101 00102 /************************************/ 00103 /* b) Error and Gradient Evaluation */ 00104 /************************************/ 00105 00106 /** 00107 * @brief Returns a pointer to the associated \ref gnn_node. 00108 * @ingroup gnn_evaluation_doc 00109 * 00110 */ 00111 #define GNN_GRAD_GET_NODE(grad) ((grad)->node) 00112 00113 /** 00114 * @brief Returns a pointer to the associated \ref gnn_input. 00115 * @ingroup gnn_evaluation_doc 00116 * 00117 */ 00118 #define GNN_GRAD_GET_INPUT(grad) ((grad)->in) 00119 00120 /** 00121 * @brief Returns a pointer to the associated \ref gnn_output. 00122 * @ingroup gnn_evaluation_doc 00123 * 00124 */ 00125 #define GNN_GRAD_GET_OUTPUT(grad) ((grad)->out) 00126 00127 /** 00128 * @brief Returns the size of the input. 00129 * @ingroup gnn_evaluation_doc 00130 * 00131 */ 00132 #define GNN_GRAD_INPUT_SIZE(grad) ((grad)->n) 00133 00134 /** 00135 * @brief Returns the size of the output. 00136 * @ingroup gnn_evaluation_doc 00137 * 00138 */ 00139 #define GNN_GRAD_OUTPUT_SIZE(grad) ((grad)->m) 00140 00141 /** 00142 * @brief Returns the size of the parameter vector. 00143 * @ingroup gnn_evaluation_doc 00144 * 00145 */ 00146 #define GNN_GRAD_PARAMETER_SIZE(grad) ((grad)->l) 00147 00148 /** 00149 * @brief Returns the amount of examples considered by the \ref gnn_grad. 00150 * @ingroup gnn_evaluation_doc 00151 * 00152 * This macro returns the number of examples considered in the computation 00153 * of the weighted values. Basically, it is the number of 00154 * examples contained in the associated \ref gnn_dataset object. 00155 */ 00156 #define GNN_GRAD_AMOUNT(grad) ((grad)->P) 00157 00158 /** 00159 * @brief Returns the last pattern weight sum. 00160 * @ingroup gnn_evaluation_doc 00161 * 00162 * This macro returns the sum 00163 * \f[ \sum_{k=s}^{s+n} p_k \f] 00164 * of the last processed minibatch in the given \ref gnn_grad buffer. 00165 */ 00166 #define GNN_GRAD_SUMP(grad) ((grad)->mp) 00167 00168 /** 00169 * @brief Returns the last pattern weight sum. 00170 * @ingroup gnn_evaluation_doc 00171 * 00172 * This macro returns the sum 00173 * \f[ \sum_{k=s}^{s+n} p_k \f] 00174 * of the last processed minibatch in the given \ref gnn_grad buffer. 00175 */ 00176 #define GNN_GRAD_SUMP(grad) ((grad)->mp) 00177 00178 /** 00179 * @brief Returns the sum of the last batch's error. 00180 * @ingroup gnn_evaluation_doc 00181 * 00182 * This macro returns the last sum of errors, given by 00183 * \f[ <E> = \sum_{k=s}^{s+n} p_k E_k \f] 00184 * for the last processed minibatch in the given \ref gnn_grad buffer. 00185 */ 00186 #define GNN_GRAD_SUME(grad) ((grad)->mp * (grad)->me) 00187 00188 /** 00189 * @brief Returns the last batch's mean error sum. 00190 * @ingroup gnn_evaluation_doc 00191 * 00192 * This macro returns the last mean error, given by 00193 * \f[ <E> = \frac{1}{\sum_{k=s}^{s+n} p_k} \sum_{k=s}^{s+n} p_k E_k \f] 00194 * for the last processed minibatch in the given \ref gnn_grad buffer. 00195 */ 00196 #define GNN_GRAD_E(grad) ((grad)->me) 00197 00198 /** 00199 * @brief Returns the last batch's mean error sum. 00200 * @ingroup gnn_evaluation_doc 00201 * 00202 * This macro returns the last gradient with respect its inputs, given by 00203 * \f[ <\frac{\partial E}{\partial x}> = 00204 * \frac{1}{\sum_{k=s}^{s+n} p_k} 00205 * \sum_{k=s}^{s+n} p_k \frac{\partial E}{\partial x}^k \f] 00206 * for the last processed minibatch in the given \ref gnn_grad buffer. 00207 */ 00208 #define GNN_GRAD_DX(grad) ((grad)->mdx) 00209 00210 /** 00211 * @brief Returns the last batch's mean error sum. 00212 * @ingroup gnn_evaluation_doc 00213 * 00214 * This macro returns the last gradient with respect its parameters, given by 00215 * \f[ <\frac{\partial E}{\partial w}> = 00216 * \frac{1}{\sum_{k=s}^{s+n} p_k} 00217 * \sum_{k=s}^{s+n} p_k \frac{\partial E}{\partial w}^k \f] 00218 * for the last processed minibatch in the given \ref gnn_grad buffer. 00219 */ 00220 #define GNN_GRAD_DW(grad) ((grad)->mdw) 00221 00222 00223 00224 /****************************/ 00225 /* c) Error Line Evaluation */ 00226 /****************************/ 00227 00228 /** 00229 * @brief Returns the origin of the line evaluations. 00230 * @ingroup gnn_evaluation_doc 00231 * 00232 * This macro returns a pointer to the origin \f$w_0\f$ where all directional 00233 * evaluations are evaluated at. 00234 */ 00235 #define GNN_LINE_W(line) ((line)->w) 00236 00237 /** 00238 * @brief Returns the direction of the line evaluations. 00239 * @ingroup gnn_evaluation_doc 00240 * 00241 * This macro returns a pointer to the directional vector \f$d\f$ where all 00242 * directional evaluations are evaluated at. 00243 */ 00244 #define GNN_LINE_DIR(line) ((line)->d) 00245 00246 /** 00247 * @brief Returns the last computed error along the line evaluation. 00248 * @ingroup gnn_evaluation_doc 00249 * 00250 * This macro returns the value of the last computed average error along the 00251 * last line evaluation. 00252 */ 00253 #define GNN_LINE_E(line) ((line)->error) 00254 00255 /** 00256 * @brief Returns the last computed derivative along the line evaluation. 00257 * @ingroup gnn_evaluation_doc 00258 * 00259 * This macro returns the value of the computed derivate along the 00260 * last line evaluation. 00261 */ 00262 #define GNN_LINE_DE(line) ((line)->derivative) 00263 00264 00265 00266 /******************************************/ 00267 /* Typedefs and Enums */ 00268 /******************************************/ 00269 00270 /*************************/ 00271 /* a) Pattern Evaluation */ 00272 /*************************/ 00273 00274 /** 00275 * @brief Evaluation buffer structure. 00276 * @ingroup gnn_evaluation_doc 00277 * 00278 * This structure contains the necessary elements for performing a full 00279 * dataset evaluation. 00280 */ 00281 typedef struct _gnn_eval gnn_eval; 00282 00283 struct _gnn_eval 00284 { 00285 gnn_node *node; /**< Pointer to the associated \ref gnn_node */ 00286 gnn_input *input; /**< Pointer to the associated \ref gnn_input */ 00287 gnn_output *output; /**< Pointer to the associated \ref gnn_output */ 00288 00289 size_t n; /**< The input size */ 00290 size_t m; /**< The output size */ 00291 size_t l; /**< The number of parameters */ 00292 size_t P; /**< The number of available patterns */ 00293 00294 gsl_vector *y; /**< Buffer for \f$y^k\f$ */ 00295 }; 00296 00297 00298 00299 /************************************/ 00300 /* b) Error and Gradient Evaluation */ 00301 /************************************/ 00302 00303 /** 00304 * @brief Error and Gradients Evaluation buffer structure. 00305 * @ingroup gnn_evaluation_doc 00306 * 00307 * This structure contains the necessary values and buffers for performing 00308 * the computation of averaged gradients and errors over datasets. 00309 */ 00310 typedef struct _gnn_grad gnn_grad; 00311 00312 struct _gnn_grad 00313 { 00314 gnn_node *node; /**< Pointer to the associated \ref gnn_node */ 00315 gnn_criterion *crit; /**< Pointer to the associated \ref gnn_criterion */ 00316 gnn_dataset *data; /**< Pointer to the associated \ref gnn_dataset */ 00317 00318 size_t n; /**< The input size */ 00319 size_t m; /**< The output size */ 00320 size_t l; /**< The number of parameters */ 00321 size_t P; /**< The number of available patterns */ 00322 00323 double p; /**< Buffer for \f$p\f$ */ 00324 double e; /**< Buffer for \f$E\f$ */ 00325 gsl_vector *y; /**< Buffer for \f$y\f$ */ 00326 gsl_vector *w; /**< Buffer for \f$w\f$ */ 00327 gsl_vector *dy; /**< Buffer for \f$\frac{\partial E}{\partial y}\f$ */ 00328 gsl_vector *dx; /**< Buffer for \f$\frac{\partial E}{\partial x}\f$ */ 00329 gsl_vector *dw; /**< Buffer for \f$\frac{\partial E}{\partial w}\f$ */ 00330 gsl_vector *h; /**< Auxiliary buffer for Hessian computing */ 00331 00332 /** Public Members **/ 00333 00334 double mp; /**< Sum of pattern weights \f$\sum p\f$ */ 00335 double me; /**< Buffer for \f$<E>\f$ */ 00336 gsl_vector *mdx; /**< Buffer for \f$<\frac{\partial E}{\partial y}>\f$ */ 00337 gsl_vector *mdw; /**< Buffer for \f$<\frac{\partial E}{\partial y}>\f$ */ 00338 }; 00339 00340 /** 00341 * @brief Gradient evaluation flags. 00342 * @ingroup gnn_evaluation_doc 00343 * 00344 * This special datatype is used by the gradient evaluation functions. It 00345 * tells them how far the batch should be processed, i.e. if it should 00346 * compute \f$<E>\f$, \f$<\frac{\partial E}{\partial x}>\f$ and 00347 * \f$<\frac{\partial E}{\partial w}>\f$, in this order of dependence. 00348 */ 00349 typedef enum _gnn_grad_eval gnn_grad_eval; 00350 00351 enum _gnn_grad_eval {gnnGradE, gnnGradDx, gnnGradDw}; 00352 00353 00354 00355 /****************************/ 00356 /* c) Error Line Evaluation */ 00357 /****************************/ 00358 00359 /** 00360 * @brief Directional Error and Gradients Evaluation buffer structure. 00361 * @ingroup gnn_evaluation_doc 00362 * 00363 * This structure contains the necessary values and buffers for performing 00364 * the computation of averaged gradients and errors over datasets, evaluated 00365 * along a given direction, treating the node's function as a one-dimensional 00366 * function. 00367 * 00368 * The structure contains the direction and a error and gradient evaluation 00369 * buffer (\ref gnn_grad). 00370 */ 00371 typedef struct _gnn_line gnn_line; 00372 00373 struct _gnn_line 00374 { 00375 gnn_grad *grad; /**< The error and gradient evaluation buffer. */ 00376 00377 gsl_vector *w; /**< The center of evaluations. */ 00378 gsl_vector *d; /**< The direction vector. */ 00379 gsl_vector *dbuf; /**< A buffer for direction evaluation. */ 00380 double error; /**< Last computed error along the given line. */ 00381 double derivative; /**< The derivative along the given line. */ 00382 }; 00383 00384 /** 00385 * @brief Line evaluation flags. 00386 * @ingroup gnn_evaluation_doc 00387 * 00388 * This special datatype is used by the line evaluation functions. It 00389 * tells them if they should computed only the error along the given 00390 * line or if they also should compute the error derivative. 00391 */ 00392 typedef enum _gnn_line_eval gnn_line_eval; 00393 00394 enum _gnn_line_eval {gnnLineE, gnnLineDE}; 00395 00396 00397 00398 /******************************************/ 00399 /* Public Interface */ 00400 /******************************************/ 00401 00402 /*************************/ 00403 /* a) Pattern Evaluation */ 00404 /*************************/ 00405 00406 gnn_eval * 00407 gnn_eval_new (gnn_node *node, gnn_input *in, gnn_output *out); 00408 00409 void 00410 gnn_eval_destroy (gnn_eval *eval); 00411 00412 int 00413 gnn_eval_pats (gnn_eval *eval, size_t s, size_t n); 00414 00415 int 00416 gnn_eval_all (gnn_eval *eval); 00417 00418 00419 /************************************/ 00420 /* b) Error and Gradient Evaluation */ 00421 /************************************/ 00422 00423 gnn_grad * 00424 gnn_grad_new (gnn_node *node, gnn_criterion *crit, gnn_dataset *data); 00425 00426 void 00427 gnn_grad_destroy (gnn_grad *grad); 00428 00429 int 00430 gnn_grad_pats (gnn_grad *grad, gnn_grad_eval flag, size_t s, size_t n); 00431 00432 int 00433 gnn_grad_all (gnn_grad *grad, gnn_grad_eval flag); 00434 00435 00436 /****************************/ 00437 /* c) Error Line Evaluation */ 00438 /****************************/ 00439 00440 gnn_line * 00441 gnn_line_new (gnn_grad *grad, gsl_vector *direction); 00442 00443 void 00444 gnn_line_destroy (gnn_line *line); 00445 00446 int 00447 gnn_line_set_direction (gnn_line *line, const gsl_vector *dir); 00448 00449 const gsl_vector * 00450 gnn_line_get_direction (gnn_line *line); 00451 00452 int 00453 gnn_line_set_origin (gnn_line *line, const gsl_vector *origin); 00454 00455 const gsl_vector * 00456 gnn_line_get_origin (gnn_line *line); 00457 00458 gnn_grad * 00459 gnn_line_get_grad (gnn_line *line); 00460 00461 int 00462 gnn_line_pats (gnn_line *line, 00463 double alpha, gnn_line_eval flag, size_t s, size_t n); 00464 00465 int 00466 gnn_line_all (gnn_line *line, double alpha, gnn_line_eval flag); 00467 00468 00469 00470 #endif /* _GNN_EVALUATION_H_ */ 00471 00472 00473
1.2.18