00001 /*************************************************************************** 00002 * @file gnn_memory_input.c 00003 * @brief Input Set Residing in Memory Implementation. 00004 * 00005 * @date : 21-09-03 22:04 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_input_doc gnn_memory_input : Memory Input Samples. 00029 * @ingroup gnn_input_doc 00030 * @brief Input sample sets that reside in memory. 00031 * 00032 * The \ref gnn_memory_input samples set stores its samples in memory. It has 00033 * a matrix, where the samples are stored row-wise. 00034 * 00035 * A \ref gnn_memory_input can be build directly providing the necessary 00036 * matrix using the \ref gnn_memory_input_new_from_matrix. Alternatively, 00037 * the matrix can be loaded using the \ref gnn_memory_input_new_from_file. 00038 */ 00039 00040 00041 00042 /******************************************/ 00043 /* Include Files */ 00044 /******************************************/ 00045 00046 #include "gnn_utilities.h" 00047 #include "gnn_memory_input.h" 00048 00049 00050 00051 /******************************************/ 00052 /* Static Declaration */ 00053 /******************************************/ 00054 00055 /** 00056 * @brief The datatype for memory inputs. 00057 * @ingroup gnn_memory_input_doc 00058 * 00059 * This is the datatype for memory inputs. It extends the basic 00060 * \ref gnn_input structure to include the additional pointer 00061 * to the samples matrix, and a vector view. 00062 */ 00063 typedef struct _gnn_memory_input gnn_memory_input; 00064 00065 struct _gnn_memory_input 00066 { 00067 gnn_input set; /**< Set \ref gnn_input */ 00068 gsl_matrix *matrix; /**< A pointer to the input patterns. */ 00069 gsl_vector_view v; /**< A view to the last required sample. */ 00070 }; 00071 00072 static const gsl_vector * 00073 gnn_memory_input_get (gnn_input *set, size_t k); 00074 00075 static void 00076 gnn_memory_input_destroy (gnn_input *set); 00077 00078 00079 00080 /******************************************/ 00081 /* Static Implementation */ 00082 /******************************************/ 00083 00084 /** 00085 * @brief The "get" function for a memory input set. 00086 * @ingroup gnn_memory_input_doc 00087 * 00088 * This function is returns the k-th pattern in the set. 00089 * 00090 * @param set A pointer to a \ref gnn_memory_input. 00091 * @param k The index of the pattern to be retrieved. 00092 * @return A pointer to the sample vector. 00093 */ 00094 static const gsl_vector * 00095 gnn_memory_input_get (gnn_input *set, size_t k) 00096 { 00097 gnn_memory_input *mset; 00098 00099 assert (set != NULL); 00100 00101 /* get memory dataset view */ 00102 mset = (gnn_memory_input *) set; 00103 00104 /* check index */ 00105 if (k < 0 || mset->matrix->size1 <= k) 00106 { 00107 GSL_ERROR_VAL ("pattern index out of bounds", GSL_EINVAL, NULL); 00108 } 00109 00110 /* get pattern views */ 00111 mset->v = gsl_matrix_row (mset->matrix, k); 00112 00113 return &(mset->v.vector); 00114 } 00115 00116 /** 00117 * @brief Destroy function. 00118 * @ingroup gnn_memory_input_doc 00119 * 00120 * This is the \ref gnn_memory_input destroy function. 00121 * @param set A pointer to a \ref gnn_memory_input dataset. 00122 */ 00123 static void 00124 gnn_memory_input_destroy (gnn_input *set) 00125 { 00126 gnn_memory_input *mset; 00127 00128 assert (set != NULL); 00129 00130 /* get memory dataset view */ 00131 mset = (gnn_memory_input *) set; 00132 00133 /* free matrices and the weight vector */ 00134 if (mset->matrix != NULL) 00135 gsl_matrix_free (mset->matrix); 00136 } 00137 00138 00139 00140 /******************************************/ 00141 /* Public Interface */ 00142 /******************************************/ 00143 00144 /** 00145 * @brief Builds an input set from a matrix. 00146 * @ingroup gnn_memory_input_doc 00147 * 00148 * This function creates a new \ref gnn_memory_input from a matrix, 00149 * where the samples are stored row-wise. That is, each row should 00150 * contain a different sample, and each column corresponds to a different 00151 * sample's component. 00152 * 00153 * @param m The sample matrix. 00154 * @return Returns a pointer to a new \ref gnn_memory_input set. 00155 */ 00156 gnn_input * 00157 gnn_memory_input_new_from_matrix (gsl_matrix *m) 00158 { 00159 size_t P; 00160 size_t n; 00161 int status; 00162 gnn_input *set; 00163 gnn_memory_input *mset; 00164 00165 assert (m != NULL); 00166 00167 /* alloc memory for input set */ 00168 mset = (gnn_memory_input *) malloc (sizeof (gnn_memory_input)); 00169 00170 /* get view as a input set */ 00171 set = (gnn_input *) mset; 00172 00173 /* get sizes */ 00174 P = m->size1; 00175 n = m->size2; 00176 00177 /* initialize */ 00178 status = gnn_input_init (set, 00179 P, 00180 n, 00181 NULL, 00182 gnn_memory_input_get, 00183 gnn_memory_input_destroy); 00184 if (status) 00185 { 00186 gnn_input_destroy (set); 00187 GSL_ERROR_VAL ("could not initialize gnn_memory_input", 00188 GSL_EFAILED, NULL); 00189 } 00190 00191 /* set fields */ 00192 mset->matrix = m; 00193 00194 return set; 00195 } 00196 00197 /** 00198 * @brief Builds a input set from a file. 00199 * @ingroup gnn_memory_input_doc 00200 * 00201 * This functions creates a new \ref gnn_memory_input from a file where the 00202 * samples are stored. Their elements should be in ASCII format separated 00203 * by blank spaces. Each row should start on a new line. 00204 * 00205 * Please read \ref gnn_memory_input_new_from_matrix for further details on 00206 * the needed format. 00207 * 00208 * @param filename The filename of the input matrix. 00209 * @return Returns a pointer to a new \ref gnn_memory_input set. 00210 */ 00211 gnn_input * 00212 gnn_memory_input_new_from_file (const char *filename) 00213 { 00214 int status; 00215 gsl_matrix *matrix = NULL; 00216 00217 assert (filename != NULL); 00218 00219 /* load input and output matrices */ 00220 matrix = gnn_matrix_load (filename); 00221 if (matrix == NULL) 00222 { 00223 GSL_ERROR_VAL ("could not load samples", GSL_EFAILED, NULL); 00224 } 00225 00226 /* create and return dataset */ 00227 return gnn_memory_input_new_from_matrix (matrix); 00228 } 00229 00230 00231
1.2.18