IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jan 31, 2018, 9:53:19 AM (8 years ago)
Author:
eugene
Message:

more work on Nnet

Location:
trunk/Ohana/src/opihi/cmd.data
Files:
2 edited

Legend:

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

    r40325 r40336  
    105105      gprint (GP_ERR, "invalid bias vector length %s (%d vs %d)\n", argv[Narg + 1], vector->Nelements, nnet[0].Nnodes[L]);
    106106      gprint (GP_ERR, "USAGE: nnet set (nnet) [weights] [biases] ... [weights] [biases] : set nnet weights (images) and biases (vectors)\n");
     107      return (FALSE);   
    107108    }
    108109
     
    110111      gprint (GP_ERR, "invalid weight image size for %s (Nx = %d vs %d)\n", argv[Narg + 0], (int) matrix->matrix.Naxis[0], nnet[0].Nnodes[L-1]);
    111112      gprint (GP_ERR, "USAGE: nnet set (nnet) [weights] [biases] ... [weights] [biases] : set nnet weights (images) and biases (vectors)\n");
     113      return (FALSE);   
    112114    }
    113115
     
    115117      gprint (GP_ERR, "invalid weight image size for %s (Ny = %d vs %d)\n", argv[Narg + 0], (int) matrix->matrix.Naxis[1], nnet[0].Nnodes[L]);
    116118      gprint (GP_ERR, "USAGE: nnet set (nnet) [weights] [biases] ... [weights] [biases] : set nnet weights (images) and biases (vectors)\n");
     119      return (FALSE);   
    117120    }
    118121
  • trunk/Ohana/src/opihi/cmd.data/nnet_train.c

    r40325 r40336  
    4545  }
    4646
     47  Vector *resid = NULL;
     48  if ((N = get_argument (argc, argv, "-resid"))) {
     49    remove_argument (N, &argc, argv);
     50    if ((resid = SelectVector (argv[N], ANYVECTOR, FALSE)) == NULL) {
     51      gprint (GP_ERR, "USAGE: nnet train (nnet) [input] [input] ... [output] [output] ...\n");
     52      gprint (GP_ERR, "cannot assign residual vector %s\n", argv[N]);
     53      return FALSE;
     54    }
     55    remove_argument (N, &argc, argv);
     56  }
     57 
     58  Buffer *result = NULL;
     59  if ((N = get_argument (argc, argv, "-result"))) {
     60    remove_argument (N, &argc, argv);
     61    if ((result = SelectBuffer (argv[N], ANYBUFFER, FALSE)) == NULL) {
     62      gprint (GP_ERR, "USAGE: nnet train (nnet) [input] [input] ... [output] [output] ...\n");
     63      gprint (GP_ERR, "cannot assign result image %s\n", argv[N]);
     64      return FALSE;
     65    }
     66    remove_argument (N, &argc, argv);
     67  }
     68 
    4769  QUADRATIC_COST = 0;
    4870  if ((N = get_argument (argc, argv, "-quadratic-cost"))) {
     
    5476    gprint (GP_ERR, "USAGE: nnet train (nnet) [input] [input] ... [output] [output] ...\n");
    5577    gprint (GP_ERR, "OPTIONS: -Nepoch [N] -Nmini [N]\n");
     78    FREE (resid);
    5679    return FALSE;
    5780  }
     
    6083  if (nnet == NULL) {
    6184    gprint (GP_ERR, "nnet %s not found, create it first\n", argv[1]);
     85    FREE (resid);
    6286    return FALSE;
    6387  }
     
    7195  if (argc != Ninput + Noutput + 2) {
    7296    gprint (GP_ERR, "need %d input and %d output vectors, but we have %d total\n", nnet[0].Nnodes[0], nnet[0].Nnodes[Nlayer - 1], argc - 2);
     97    FREE (resid);
    7398    return FALSE;
    7499  }
     
    86111      free (inVec);
    87112      free (outVec);
    88       return (FALSE);   
     113      FREE (resid);
     114      return FALSE;   
    89115    }
    90116    if (Ntrial && (inVec[i][0].Nelements != Ntrial)) {
     
    92118      free (inVec);
    93119      free (outVec);
    94       return (FALSE);   
     120      FREE (resid);
     121      return FALSE;   
    95122    }
    96123    if (!Ntrial) Ntrial = inVec[i][0].Nelements;
     
    99126      free (inVec);
    100127      free (outVec);
    101       return (FALSE);   
     128      FREE (resid);
     129      return FALSE;   
    102130    }
    103131  }   
     
    108136      free (inVec);
    109137      free (outVec);
    110       return (FALSE);   
     138      FREE (resid);
     139      return FALSE;   
    111140    }
    112141    if (outVec[i][0].Nelements != Ntrial) {
     
    114143      free (inVec);
    115144      free (outVec);
    116       return (FALSE);   
     145      FREE (resid);
     146      return FALSE;   
    117147    }
    118148  }   
     
    123153  ALLOCATE_PTR (seq, int,   Ntrial);
    124154  ALLOCATE_PTR (rnd, float, Ntrial);
     155
     156  if (resid) ResetVector (resid, OPIHI_FLT, Nepoch);
     157
     158  if (1) {
     159    int Npts = 0;
     160    float s1 = 0.0, s2 = 0.0;
     161    for (int i = 0; i < Ntrial; i++) {
     162
     163      // store the input values for this row (trial) in the input value vector
     164      for (int j = 0; j < Ninput; j++) {
     165        nnet[0].svalue[0][j] = inVec[j][0].elements.Flt[i];
     166      }
     167         
     168      nnet_feedforward (nnet);
     169         
     170      // store the output value vector values in output vectors for this row
     171      for (int j = 0; j < Noutput; j++) {
     172        float dS = outVec[j][0].elements.Flt[i] - nnet[0].svalue[Nlayer-1][j];
     173        s1 += dS;
     174        s2 += SQ(dS);
     175        Npts ++;
     176      }
     177    }
     178    float mean = s1 / Npts;
     179    float sigma = sqrt(s2 / Npts - mean*mean);
     180    gprint (GP_ERR, "start, %f +/- %f\n", mean, sigma);
     181  }
    125182
    126183  // train for Nepochs
     
    137194      // update the weights and biases using the mini batch subset
    138195      nnet_descent_step (nnet, inVec, outVec, seq, pass, Nmini, eta, lambda);
    139       gprint (GP_ERR, "epoch %d of %d, pass %d of %d\n", epoch, Nepoch, pass, Npass);
     196    }
     197
     198    if (resid) {
     199      int Npts = 0;
     200      float s1 = 0.0, s2 = 0.0;
     201      for (int i = 0; i < Ntrial; i++) {
     202
     203        // store the input values for this row (trial) in the input value vector
     204        for (int j = 0; j < Ninput; j++) {
     205          nnet[0].svalue[0][j] = inVec[j][0].elements.Flt[i];
     206        }
     207         
     208        nnet_feedforward (nnet);
     209         
     210        // store the output value vector values in output vectors for this row
     211        for (int j = 0; j < Noutput; j++) {
     212          float dS = outVec[j][0].elements.Flt[i] - nnet[0].svalue[Nlayer-1][j];
     213          s1 += dS;
     214          s2 += SQ(dS);
     215          Npts ++;
     216        }
     217      }
     218      float mean = s1 / Npts;
     219      float sigma = sqrt(s2 / Npts - mean*mean);
     220      if (epoch % 10 == 0) gprint (GP_ERR, "epoch %d of %d, %f +/- %f\n", epoch, Nepoch, mean, sigma);
     221      resid[0].elements.Flt[epoch] = sigma;
     222    } else {
     223      if (epoch % 10 == 0) gprint (GP_ERR, "epoch %d of %d\n", epoch, Nepoch);
    140224    }
    141225  } 
     226
     227  if (result) {
     228
     229    gfits_free_matrix (&result[0].matrix);
     230    gfits_free_header (&result[0].header);
     231    if (!CreateBuffer (result, Noutput, Ntrial, -32, 1.0, 0.0)) return FALSE;
     232
     233
     234    float *value = (float *) result[0].matrix.buffer;
     235
     236    for (int i = 0; i < Ntrial; i++) {
     237      // store the input values for this row (trial) in the input value vector
     238      for (int j = 0; j < Ninput; j++) {
     239        nnet[0].svalue[0][j] = inVec[j][0].elements.Flt[i];
     240      }
     241         
     242      nnet_feedforward (nnet);
     243         
     244      for (int j = 0; j < Noutput; j++) {
     245        value[j + i*Noutput] = nnet[0].svalue[Nlayer-1][j];
     246      }
     247    }
     248  }
    142249
    143250  free (seq);
     
    329436void nnet_sortseq (float *X, int *Y, int N) {
    330437
    331 # define SWAPFUNC(A,B){ float ftmp; int itmp; \
     438# define SWAPFUNC(A,B){ float ftmp; int itmp;   \
    332439    ftmp = X[A]; X[A] = X[B]; X[B] = ftmp;      \
    333     itmp = Y[A]; Y[A] = Y[B]; Y[B] = itmp;       \
    334 }
     440    itmp = Y[A]; Y[A] = Y[B]; Y[B] = itmp;      \
     441  }
    335442# define COMPARE(A,B)(X[A] < X[B])
    336443
Note: See TracChangeset for help on using the changeset viewer.