IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 26, 2018, 12:54:01 PM (8 years ago)
Author:
eugene
Message:

add jdtolst function; add nnet write function

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/cmd.data/nnet_commands.c

    r40371 r40376  
    258258}
    259259
     260int nnet_write (int argc, char **argv) {
     261
     262  if (argc < 3) {
     263    gprint (GP_ERR, "USAGE: nnet write (nnet) (filename) : save nnet weights and biases to a file\n");
     264    return FALSE;
     265  }
     266
     267  Nnet *nnet = FindNnet (argv[1]);
     268  if (nnet == NULL) {
     269    gprint (GP_ERR, "nnet %s not found, create it first\n", argv[1]);
     270    return FALSE;
     271  }
     272
     273  // open the file and read the number of layers
     274  FILE *f = fopen (argv[2], "w");
     275  if (f == NULL) {
     276    gprint (GP_ERR, "USAGE: nnet write (nnet) (filename) : save nnet weights and biases to a file\n");
     277    gprint (GP_ERR, "file %s could not be opened\n", argv[2]);
     278    return FALSE;
     279  }
     280
     281  // write the number of layers
     282  // NLAYER 3
     283  fprintf (f, "NLAYER %d\n", nnet->Nlayer);
     284
     285  // write the number of nodes for each layer
     286  // LAYERS 2 4 2
     287  fprintf (f, "LAYERS");
     288  for (int i = 0; i < nnet->Nlayer; i++) {
     289    fprintf (f, " %d", nnet->Nnodes[i]);
     290  }
     291  fprintf (f, "\n");
     292
     293  // read the weights and biases from the data file
     294  for (int L = 1; L < nnet[0].Nlayer; L++) {
     295   
     296    fprintf (f, "LAYER %d NX %d NY %d\n", L - 1, nnet->Nnodes[L-1], nnet->Nnodes[L]);
     297
     298    for (int j = 0; j < nnet->Nnodes[L]; j++) {
     299      for (int i = 0; i < nnet->Nnodes[L-1]; i++) {
     300        int k = i + j*nnet[0].Nnodes[L-1];
     301        fprintf (f, "%f ", nnet[0].weight[L][k]);
     302      }
     303      fprintf (f, "%f\n", nnet[0].biases[L][j]);
     304    }
     305  }
     306  fclose (f);
     307
     308  return TRUE;
     309}
     310
    260311int nnet_get (int argc, char **argv) {
    261312
Note: See TracChangeset for help on using the changeset viewer.