Changeset 40371 for trunk/Ohana/src/opihi/cmd.data/nnet_commands.c
- Timestamp:
- Mar 16, 2018, 4:06:05 PM (8 years ago)
- File:
-
- 1 edited
-
trunk/Ohana/src/opihi/cmd.data/nnet_commands.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/opihi/cmd.data/nnet_commands.c
r40336 r40371 62 62 CreateNnetData (nnet, LargeWeightInit); 63 63 64 return TRUE; 65 } 66 67 int nnet_print (int argc, char **argv) { 68 69 if (argc != 2) { 70 gprint (GP_ERR, "USAGE: nnet print (nnet)\n"); 71 return FALSE; 72 } 73 74 Nnet *nnet = FindNnet (argv[1]); 75 if (nnet == NULL) { 76 gprint (GP_ERR, "nnet %s not found, create it first\n", argv[1]); 77 return FALSE; 78 } 79 80 PrintNnet (nnet); 64 81 return TRUE; 65 82 } … … 146 163 } 147 164 165 int nnet_read (int argc, char **argv) { 166 167 # define D_LINE 0x10000 168 char word[128]; 169 char line[D_LINE]; 170 171 if (argc < 3) { 172 gprint (GP_ERR, "USAGE: nnet read (nnet) (filename) : set nnet weights and biases based on a file\n"); 173 gprint (GP_ERR, "the first line of the file specifies the number of layers, each layer in the network is written as a matrix of numbers (weights) and a vector\n"); 174 return FALSE; 175 } 176 177 // open the file and read the number of layers 178 FILE *f = fopen (argv[2], "r"); 179 if (f == NULL) { 180 gprint (GP_ERR, "USAGE: nnet read (nnet) (filename) : set nnet weights and biases based on a file\n"); 181 gprint (GP_ERR, "file %s could not be opened\n", argv[2]); 182 return FALSE; 183 } 184 185 // read the number of layers 186 // NLAYER 3 187 int Nlayer; 188 scan_line_maxlen (f, line, D_LINE); 189 sscanf (line, "%127s %d", word, &Nlayer); 190 if (strcmp(word, "NLAYER")) { 191 gprint (GP_ERR, "warning: NLAYER keyword not found\n"); 192 } 193 194 Nnet *nnet = CreateNnet (argv[1], Nlayer); 195 196 // read the number of nodes 197 // LAYERS 2 4 2 198 scan_line_maxlen (f, line, D_LINE); 199 char *tmpword = getword (line); 200 if (strcmp (tmpword, "LAYERS")) { 201 gprint (GP_ERR, "warning: LAYERS keyword not found\n"); 202 } 203 FREE (tmpword); 204 for (int i = 0; i < Nlayer; i++) { 205 int Nnode; 206 int status = iparse (&Nnode, i + 2, line); // numbering is fields 1 2 3 207 if (!status) { 208 gprint (GP_ERR, "error: failed to find all Nnode values\n"); 209 fclose (f); 210 DeleteNnet (nnet); 211 return FALSE; 212 } 213 nnet[0].Nnodes[i] = Nnode; 214 } 215 216 // this creates the data for each node and inits with gaussian weights 217 CreateNnetData (nnet, FALSE); 218 219 // read the weights and biases from the data file 220 for (int L = 1; L < nnet[0].Nlayer; L++) { 221 222 char word1[128], word2[128], word3[128]; 223 int Nx, Ny, layer; 224 scan_line_maxlen (f, line, D_LINE); 225 sscanf (line, "%127s %d %127s %d %127s %d", word1, &layer, word2, &Nx, word3, &Ny); 226 gprint (GP_ERR, "LAYER: %d, Nx: %d, Ny: %d\n", layer, Nx, Ny); 227 228 if (layer != L - 1) { 229 gprint (GP_ERR, "warning: expect layer = %d, got %d\n", L - 1, layer); 230 } 231 if (Nx != nnet[0].Nnodes[L - 1]) { 232 gprint (GP_ERR, "warning: expect Nx = %d, got %d\n", nnet[0].Nnodes[L - 1], Nx); 233 } 234 if (Ny != nnet[0].Nnodes[L]) { 235 gprint (GP_ERR, "warning: expect Ny = %d, got %d\n", nnet[0].Nnodes[L], Ny); 236 } 237 238 double value; 239 for (int j = 0; j < Ny; j++) { 240 scan_line_maxlen (f, line, D_LINE); 241 for (int i = 0; i < Nx; i++) { 242 int k = i + j*nnet[0].Nnodes[L-1]; 243 int status = dparse (&value, i + 1, line); 244 if (!status) { 245 gprint (GP_ERR, "error: failed to read entry from a line (layer: %d, i: %d, j: %d)\n", layer, i, j); 246 fclose (f); 247 DeleteNnet (nnet); 248 return FALSE; 249 } 250 nnet[0].weight[L][k] = value; 251 } 252 dparse (&value, Nx + 1, line); 253 nnet[0].biases[L][j] = value; 254 } 255 } 256 257 return TRUE; 258 } 259 148 260 int nnet_get (int argc, char **argv) { 149 261
Note:
See TracChangeset
for help on using the changeset viewer.
