00001 /*************************************************************************** 00002 * @file gnn_memory_output.c 00003 * @brief Input Set Residing in Memory Implementation. 00004 * 00005 * @date : 22-09-03 21:22 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_memory_output_doc gnn_memory_output : Memory Output Samples. 00029 * @ingroup gnn_output_doc 00030 * @brief Output sample sets that reside in memory. 00031 * 00032 * The \ref gnn_memory_output samples set stores its samples in memory. 00033 * Internally, it contains a matrix, where the output samples are stored 00034 * row-wise, so the matrix can be retrieved. 00035 * 00036 * A \ref gnn_memory_output can be build directly providing a matrix of the 00037 * required size using the \ref gnn_memory_output_new_from_matrix. 00038 * Alternatively, it can be built just by indicating the number of outputs 00039 * that it should be able to store, and their size. 00040 */ 00041 00042 00043 00044 /******************************************/ 00045 /* Include Files */ 00046 /******************************************/ 00047 00048 #include "gnn_utilities.h" 00049 #include "gnn_memory_output.h" 00050 00051 00052 00053 /******************************************/ 00054 /* Static Declaration */ 00055 /******************************************/ 00056 00057 /** 00058 * @brief The datatype for memory outputs. 00059 * @ingroup gnn_memory_output_doc 00060 * 00061 * This is the datatype for memory outputs. It extends the basic 00062 * \ref gnn_output structure to include the additional pointer 00063 * to the output samples matrix, and a vector view. 00064 */ 00065 typedef struct _gnn_memory_output gnn_memory_output; 00066 00067 struct _gnn_memory_output 00068 { 00069 gnn_output set; /**< The underlying \ref gnn_input structure. */ 00070 gsl_matrix *matrix; /**< A pointer to the outputs. */ 00071 gsl_vector_view v; /**< A view to the last stored sample. */ 00072 }; 00073 00074 static int 00075 gnn_memory_output_put (gnn_output *set, size_t k, const gsl_vector *v); 00076 00077 static void 00078 gnn_memory_output_destroy (gnn_output *set); 00079 00080 00081 00082 /******************************************/ 00083 /* Static Implementation */ 00084 /******************************************/ 00085 00086 /** 00087 * @brief The "put" function for a memory output set. 00088 * @ingroup gnn_memory_output_doc 00089 * 00090 * This function stores the k-th pattern in the set. 00091 * 00092 * @param set A pointer to a \ref gnn_memory_output. 00093 * @param k The index of the pattern to be stored. 00094 * @param v A pointer to the output vector to be stored. 00095 * @return Returns 0 if suceeded. 00096 */ 00097 static int 00098 gnn_memory_output_put (gnn_output *set, size_t k, const gsl_vector *v) 00099 { 00100 gnn_memory_output *mset; 00101 00102 assert (set != NULL); 00103 00104 /* get memory dataset view */ 00105 mset = (gnn_memory_output *) set; 00106 00107 /* check index */ 00108 if (k < 0 || mset->matrix->size1 <= k) 00109 { 00110 GSL_ERROR ("pattern index out of bounds", GSL_EINVAL); 00111 } 00112 00113 /* get pattern views */ 00114 mset->v = gsl_matrix_row (mset->matrix, k); 00115 00116 /* copy output */ 00117 gsl_vector_memcpy (&(mset->v.vector), v); 00118 00119 return 0; 00120 } 00121 00122 /** 00123 * @brief Destroy function. 00124 * @ingroup gnn_memory_output_doc 00125 * 00126 * This is the \ref gnn_memory_output destroy function. 00127 * @param set A pointer to a \ref gnn_memory_output dataset. 00128 */ 00129 static void 00130 gnn_memory_output_destroy (gnn_output *set) 00131 { 00132 gnn_memory_output *mset; 00133 00134 assert (set != NULL); 00135 00136 /* get memory dataset view */ 00137 mset = (gnn_memory_output *) set; 00138 00139 /* free matrices and the weight vector */ 00140 if (mset->matrix != NULL) 00141 gsl_matrix_free (mset->matrix); 00142 } 00143 00144 00145 00146 /******************************************/ 00147 /* Public Interface */ 00148 /******************************************/ 00149 00150 /** 00151 * @brief Builds an output set for required sizes. 00152 * @ingroup gnn_memory_output_doc 00153 * 00154 * This function creates a new \ref gnn_memory_output that should be able 00155 * to store the given number of patterns, where the sample's size is also 00156 * given. 00157 * 00158 * @param size The number of patterns to be stored by the output. 00159 * @param n The size of an output sample vector. 00160 * @return Returns a pointer to a new \ref gnn_memory_output set. 00161 */ 00162 gnn_output * 00163 gnn_memory_output_new (size_t size, size_t n) 00164 { 00165 int status; 00166 gsl_matrix *matrix; 00167 00168 /* check for valid sizes */ 00169 if (size < 1) 00170 { 00171 GSL_ERROR_VAL ("set size should be stricly positive", GSL_EINVAL, NULL); 00172 } 00173 if (n < 1) 00174 { 00175 GSL_ERROR_VAL ("vector size should be stricly positive", 00176 GSL_EINVAL, NULL); 00177 } 00178 00179 /* allocate matrix */ 00180 matrix = gsl_matrix_alloc (size, n); 00181 if (matrix == NULL) 00182 { 00183 GSL_ERROR_VAL ("could not allocate internal matrix", 00184 GSL_ENOMEM, NULL); 00185 } 00186 00187 return gnn_memory_output_new_from_matrix (matrix); 00188 } 00189 00190 /** 00191 * @brief Builds an output set from a matrix. 00192 * @ingroup gnn_memory_output_doc 00193 * 00194 * This function creates a new \ref gnn_memory_output from a matrix, 00195 * where the samples are stored row-wise. That is, each row will 00196 * contain a different sample, and each column corresponds to a different 00197 * sample's component. 00198 * 00199 * @param m The output sample matrix. 00200 * @return Returns a pointer to a new \ref gnn_memory_output set. 00201 */ 00202 gnn_output * 00203 gnn_memory_output_new_from_matrix (gsl_matrix *m) 00204 { 00205 size_t P; 00206 size_t n; 00207 int status; 00208 gnn_output *set; 00209 gnn_memory_output *mset; 00210 00211 assert (m != NULL); 00212 00213 /* alloc memory for input set */ 00214 mset = (gnn_memory_output *) malloc (sizeof (gnn_memory_output)); 00215 00216 /* get view as a input set */ 00217 set = (gnn_output *) mset; 00218 00219 /* get sizes */ 00220 P = m->size1; 00221 n = m->size2; 00222 00223 /* initialize */ 00224 status = gnn_output_random_access_init (set, P, n, 00225 NULL, gnn_memory_output_put, gnn_memory_output_destroy); 00226 if (status) 00227 { 00228 gnn_output_destroy (set); 00229 GSL_ERROR_VAL ("could not initialize gnn_memory_output", 00230 GSL_EFAILED, NULL); 00231 } 00232 00233 /* set fields */ 00234 mset->matrix = m; 00235 00236 return set; 00237 } 00238 00239 /** 00240 * @brief Returns the set's internal matrix. 00241 * @ingroup gnn_memory_output_doc 00242 * 00243 * This function returns a pointer to the set's internal matrix. This matrix 00244 * stores the output vectors row-wise. 00245 * 00246 * @param set A pointer to a \ref gnn_output. 00247 * @return Returns a pointer to the internal matrix. 00248 */ 00249 const gsl_matrix * 00250 gnn_memory_output_get_matrix (gnn_output *set) 00251 { 00252 gnn_memory_output *mset; 00253 00254 assert (set != NULL); 00255 00256 mset = (gnn_memory_output *) set; 00257 return mset->matrix; 00258 } 00259 00260 /** 00261 * @brief Saves the outputs to a file. 00262 * @ingroup gnn_memory_output_doc 00263 * 00264 * This function saves the outputs stored in the matrix to a file, in 00265 * plain ASCII format. 00266 * 00267 * @param set A pointer to the output set. 00268 * @param filename A string with the name of the output file. 00269 * @return Returns 0 if succeeded. 00270 */ 00271 int 00272 gnn_memory_output_save (gnn_output *set, const char *filename) 00273 { 00274 int status; 00275 gnn_memory_output *mset; 00276 00277 assert (set != NULL); 00278 00279 mset = (gnn_memory_output *) set; 00280 00281 status = gnn_matrix_save (filename, mset->matrix); 00282 00283 00284 return status; 00285 } 00286 00287 00288 00289 00290 00291
1.2.18