00001 /*************************************************************************** 00002 * @file gnn_simple.c 00003 * @brief Simple data set implementation. 00004 * 00005 * @date : 22/09/03 12:48 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 00027 /** 00028 * @defgroup gnn_simple_set_doc gnn_simple_set : A simple implementation of datasets. 00029 * @ingroup gnn_dataset_doc 00030 * @brief Simple dataset implementation. 00031 * 00032 * The \ref gnn_simple_set dataset is a simple implementation of a dataset 00033 * structure. It is built given three input samplers 00034 * (\ref gnn_input structures): one for inputs, targets and weights. 00035 * 00036 * There are two ways for building a simple set: 00037 * 00038 * - It is given an input and an target pattern sampler of the same size. 00039 * In this case, each weight is assumed to be 1. 00040 * - It is given an input, target and weight sampler of the same size. 00041 * 00042 * The simple set, in order to get the ith-pattern \f$(x^i, t^i, p^i)\f$, 00043 * retrieves the ith-sample from the input, target and weight source (if any) 00044 * calling the \ref gnn_input_get function on each of them. 00045 * 00046 * 00047 * 00048 * 00049 */ 00050 00051 00052 00053 /******************************************/ 00054 /* Include Files */ 00055 /******************************************/ 00056 00057 #include "gnn_utilities.h" 00058 #include "gnn_simple_set.h" 00059 #include "gnn_memory_input.h" 00060 00061 00062 /******************************************/ 00063 /* Static Declaration */ 00064 /******************************************/ 00065 00066 static int 00067 gnn_simple_set_reset (gnn_dataset *set); 00068 00069 static int 00070 gnn_simple_set_get (gnn_dataset *set, 00071 size_t k, 00072 gsl_vector **x, 00073 gsl_vector **t, 00074 double *p); 00075 00076 static void 00077 gnn_simple_set_destroy (gnn_dataset *set); 00078 00079 00080 00081 /******************************************/ 00082 /* Static Implementation */ 00083 /******************************************/ 00084 00085 /** 00086 * @brief The "reset" function for a simple set. 00087 * @ingroup gnn_simple_set_doc 00088 * 00089 * This function resets the \ref gnn_simple_set. 00090 * 00091 * @param set A pointer to a \ref gnn_simple_set. 00092 * @return 0 if succeeded. 00093 */ 00094 static int 00095 gnn_simple_set_reset (gnn_dataset *set) 00096 { 00097 gnn_simple_set *sset; 00098 00099 assert (set != NULL); 00100 00101 sset = (gnn_simple_set *) set; 00102 00103 /* reset samplers */ 00104 gnn_input_reset (sset->inputs); 00105 gnn_input_reset (sset->outputs); 00106 if (sset->weights != NULL) 00107 gnn_input_reset (sset->weights); 00108 00109 return 0; 00110 } 00111 00112 /** 00113 * @brief The "get" function for a simple set. 00114 * @ingroup gnn_simple_set_doc 00115 * 00116 * This function is returns the k-th pattern in the simple set, where the 00117 * k-th input pattern, output pattern and pattern weight are sampled from 00118 * the internally installed device samplers. 00119 * 00120 * @param set A pointer to a \ref gnn_simple_set. 00121 * @param k The index of the pattern to be retrieved. 00122 * @param x A reference for the input pattern. 00123 * @param t A reference for the output pattern. 00124 * @param p A pointer to a double for the pattern weight. 00125 * @return 0 if succeeded. 00126 */ 00127 static int 00128 gnn_simple_set_get (gnn_dataset *set, 00129 size_t k, 00130 gsl_vector **x, 00131 gsl_vector **t, 00132 double *p) 00133 { 00134 size_t P; 00135 gsl_vector *weight; 00136 gnn_simple_set *sset; 00137 00138 assert (set != NULL); 00139 00140 /* get size */ 00141 P = gnn_dataset_get_size (set); 00142 00143 /* get view as a simple set */ 00144 sset = (gnn_simple_set *) set; 00145 00146 /* check index */ 00147 if (k < 0 || P <= k) 00148 { 00149 GSL_ERROR ("pattern index out of bounds", GSL_EINVAL); 00150 } 00151 00152 /* get pattern views */ 00153 *x = (gsl_vector *) gnn_input_get (sset->inputs, k); 00154 *t = (gsl_vector *) gnn_input_get (sset->outputs, k); 00155 if (sset->weights != NULL) 00156 { 00157 weight = (gsl_vector *) gnn_input_get (sset->weights, k); 00158 *p = gsl_vector_get (weight, 0); 00159 } 00160 else 00161 *p = 1.0; 00162 00163 return 0; 00164 } 00165 00166 /** 00167 * @brief Destroy function. 00168 * @ingroup gnn_simple_set_doc 00169 * 00170 * This is the \ref gnn_simple_set destroy function. 00171 * @param set A pointer to a \ref gnn_simple_set dataset. 00172 */ 00173 static void 00174 gnn_simple_set_destroy (gnn_dataset *set) 00175 { 00176 gnn_simple_set *sset; 00177 00178 assert (set != NULL); 00179 00180 /* get simple set view */ 00181 sset = (gnn_simple_set *) set; 00182 00183 /* destroy inputs, outputs and weights if any */ 00184 if (sset->inputs != NULL) 00185 gnn_input_destroy (sset->inputs); 00186 if (sset->outputs != NULL) 00187 gnn_input_destroy (sset->outputs); 00188 if (sset->weights != NULL) 00189 gnn_input_destroy (sset->weights); 00190 } 00191 00192 00193 00194 /******************************************/ 00195 /* Public Interface */ 00196 /******************************************/ 00197 00198 /** 00199 * @brief Builds a new simple set. 00200 * @ingroup gnn_simple_set_doc 00201 * 00202 * This functions creates a new simple set from two or three input samplers. 00203 * The first two correspond to the input pattern and output pattern samplers, 00204 * wich should contain the same amount of patterns. The third is the pattern 00205 * weight sampler, which, if given, should also be of the same size. If omitted 00206 * (that means it is a \c NULL pointer), 00207 * then all weights are assumed to be 1. 00208 * 00209 * @param inputs The input sampler, a \ref gnn_input object. 00210 * @param outputs The target sampler, a \ref gnn_input object. 00211 * @param weights The weight sampler, a \ref gnn_input object. 00212 * @return Returns a pointer to a new \ref gnn_simple_set dataset. 00213 */ 00214 gnn_dataset * 00215 gnn_simple_set_new (gnn_input *inputs, gnn_input *outputs, gnn_input *weights) 00216 { 00217 size_t size; 00218 size_t n; 00219 size_t m; 00220 size_t P; 00221 int status; 00222 gnn_dataset *set; 00223 gnn_simple_set *sset; 00224 00225 assert (inputs != NULL); 00226 assert (outputs != NULL); 00227 00228 /* get sizes */ 00229 P = gnn_input_get_size (inputs); 00230 n = gnn_input_sample_get_size (inputs); 00231 m = gnn_input_sample_get_size (outputs); 00232 00233 /* check sizes */ 00234 if (P != gnn_input_get_size (outputs)) 00235 { 00236 GSL_ERROR_VAL ("input and output samplers should have the same " 00237 "number of patterns", 00238 GSL_EINVAL, NULL); 00239 } 00240 if ( (weights != NULL) && (P != gnn_input_get_size (weights)) ) 00241 { 00242 GSL_ERROR_VAL ("the input pattern, output pattern and pattern weight " 00243 "samplers should have the same number of patterns", 00244 GSL_EINVAL, NULL); 00245 } 00246 00247 /* alloc memory for dataset */ 00248 sset = (gnn_simple_set *) malloc (sizeof (gnn_simple_set)); 00249 00250 /* get memory dataset view */ 00251 set = (gnn_dataset *) sset; 00252 00253 /* initialize */ 00254 status = gnn_dataset_init (set, 00255 P, 00256 n, 00257 m, 00258 gnn_simple_set_reset, 00259 gnn_simple_set_get, 00260 gnn_simple_set_destroy); 00261 if (status) 00262 { 00263 GSL_ERROR_VAL ("could not initialize gnn_simple_set", 00264 GSL_EINVAL, NULL); 00265 } 00266 00267 /* set fields */ 00268 sset->inputs = inputs; 00269 sset->outputs = outputs; 00270 sset->weights = weights; 00271 00272 return set; 00273 } 00274 00275 00276 00277 /** 00278 * @brief Builds a new simple set from text files. 00279 * @ingroup gnn_simple_set_doc 00280 * 00281 * This functions creates a new simple set from two or three text files 00282 * containing the patterns. 00283 * 00284 * The text files should be ASCII-formatted, and should contain matrices 00285 * where the number of columns corresponds to the size of the vectors, and 00286 * the row number to the number of patterns. Obviously, it is illegal to 00287 * provide matrices of different row sizes. Also, the weights file should 00288 * have <b>only one<b> column. 00289 * 00290 * If the weights file is omitted (i.e. it is a pointer to \c NULL), the 00291 * the weights are assumed to be 1. 00292 * 00293 * It is basically a shorthand for building the dataset from \ref gnn_input 00294 * structures which themselves should be built from text files. 00295 * 00296 * @param inputs The name of the file containing the input examples. 00297 * @param outputs The name of the file containing the output examples 00298 * (targets). 00299 * @param weights The name of the file containing the vector with the 00300 * weights for each pattern, or NULL if they should all be 1. 00301 * @return Returns a pointer to a new \ref gnn_simple_set dataset. 00302 */ 00303 gnn_dataset * 00304 gnn_simple_set_from_files_new (const char *inputsFile, 00305 const char *outputsFile, 00306 const char *weightsFile) 00307 { 00308 assert (inputsFile != NULL); 00309 assert (outputsFile != NULL); 00310 00311 if (weightsFile != NULL) 00312 { 00313 return gnn_simple_set_new ( 00314 gnn_memory_input_new_from_file (inputsFile), 00315 gnn_memory_input_new_from_file (outputsFile), 00316 gnn_memory_input_new_from_file (weightsFile) 00317 ); 00318 } 00319 else 00320 { 00321 return gnn_simple_set_new ( 00322 gnn_memory_input_new_from_file (inputsFile), 00323 gnn_memory_input_new_from_file (outputsFile), 00324 NULL 00325 ); 00326 } 00327 } 00328 00329 00330 00331 /** 00332 * @brief Returns a pointer to the inputs. 00333 * @ingroup gnn_simple_set_doc 00334 * 00335 * This function returns a pointer to the inputs, which are of the 00336 * \ref gnn_input type. 00337 * @param set A pointer to a \ref gnn_simple_set dataset. 00338 * @return Returns a pointer to the internal input device. 00339 */ 00340 gnn_input * 00341 gnn_simple_set_get_inputs (gnn_dataset *set) 00342 { 00343 gnn_simple_set *sset; 00344 00345 assert (set != NULL); 00346 00347 sset = (gnn_simple_set *) set; 00348 return sset->inputs; 00349 } 00350 00351 /** 00352 * @brief Returns a pointer to the target vector input device. 00353 * @ingroup gnn_simple_set_doc 00354 * 00355 * This function returns a pointer to the outputs, which are of the 00356 * \ref gnn_input type. 00357 * @param set A pointer to a \ref gnn_simple_set dataset. 00358 * @return Returns a pointer to the internal target vector input device. 00359 */ 00360 gnn_input * 00361 gnn_simple_set_get_outputs (gnn_dataset *set) 00362 { 00363 gnn_simple_set *sset; 00364 00365 assert (set != NULL); 00366 00367 sset = (gnn_simple_set *) set; 00368 return sset->outputs; 00369 } 00370 00371 /** 00372 * @brief Returns a pointer to the inputs. 00373 * @ingroup gnn_simple_set_doc 00374 * 00375 * This function returns a pointer to the weights input device, 00376 * which are of the \ref gnn_input type or NULL if not available. 00377 * 00378 * @param set A pointer to a \ref gnn_simple_set dataset. 00379 * @return Returns a pointer to the internal weight input device or NULL. 00380 */ 00381 gnn_input * 00382 gnn_simple_set_get_weights (gnn_dataset *set) 00383 { 00384 gnn_simple_set *sset; 00385 00386 assert (set != NULL); 00387 00388 sset = (gnn_simple_set *) set; 00389 return sset->weights; 00390 } 00391 00392 00393
1.2.18