- Timestamp:
- Jan 31, 2018, 9:53:19 AM (8 years ago)
- Location:
- trunk/Ohana/src/opihi/cmd.data
- Files:
-
- 2 edited
-
nnet_commands.c (modified) (3 diffs)
-
nnet_train.c (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/opihi/cmd.data/nnet_commands.c
r40325 r40336 105 105 gprint (GP_ERR, "invalid bias vector length %s (%d vs %d)\n", argv[Narg + 1], vector->Nelements, nnet[0].Nnodes[L]); 106 106 gprint (GP_ERR, "USAGE: nnet set (nnet) [weights] [biases] ... [weights] [biases] : set nnet weights (images) and biases (vectors)\n"); 107 return (FALSE); 107 108 } 108 109 … … 110 111 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]); 111 112 gprint (GP_ERR, "USAGE: nnet set (nnet) [weights] [biases] ... [weights] [biases] : set nnet weights (images) and biases (vectors)\n"); 113 return (FALSE); 112 114 } 113 115 … … 115 117 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]); 116 118 gprint (GP_ERR, "USAGE: nnet set (nnet) [weights] [biases] ... [weights] [biases] : set nnet weights (images) and biases (vectors)\n"); 119 return (FALSE); 117 120 } 118 121 -
trunk/Ohana/src/opihi/cmd.data/nnet_train.c
r40325 r40336 45 45 } 46 46 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 47 69 QUADRATIC_COST = 0; 48 70 if ((N = get_argument (argc, argv, "-quadratic-cost"))) { … … 54 76 gprint (GP_ERR, "USAGE: nnet train (nnet) [input] [input] ... [output] [output] ...\n"); 55 77 gprint (GP_ERR, "OPTIONS: -Nepoch [N] -Nmini [N]\n"); 78 FREE (resid); 56 79 return FALSE; 57 80 } … … 60 83 if (nnet == NULL) { 61 84 gprint (GP_ERR, "nnet %s not found, create it first\n", argv[1]); 85 FREE (resid); 62 86 return FALSE; 63 87 } … … 71 95 if (argc != Ninput + Noutput + 2) { 72 96 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); 73 98 return FALSE; 74 99 } … … 86 111 free (inVec); 87 112 free (outVec); 88 return (FALSE); 113 FREE (resid); 114 return FALSE; 89 115 } 90 116 if (Ntrial && (inVec[i][0].Nelements != Ntrial)) { … … 92 118 free (inVec); 93 119 free (outVec); 94 return (FALSE); 120 FREE (resid); 121 return FALSE; 95 122 } 96 123 if (!Ntrial) Ntrial = inVec[i][0].Nelements; … … 99 126 free (inVec); 100 127 free (outVec); 101 return (FALSE); 128 FREE (resid); 129 return FALSE; 102 130 } 103 131 } … … 108 136 free (inVec); 109 137 free (outVec); 110 return (FALSE); 138 FREE (resid); 139 return FALSE; 111 140 } 112 141 if (outVec[i][0].Nelements != Ntrial) { … … 114 143 free (inVec); 115 144 free (outVec); 116 return (FALSE); 145 FREE (resid); 146 return FALSE; 117 147 } 118 148 } … … 123 153 ALLOCATE_PTR (seq, int, Ntrial); 124 154 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 } 125 182 126 183 // train for Nepochs … … 137 194 // update the weights and biases using the mini batch subset 138 195 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); 140 224 } 141 225 } 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 } 142 249 143 250 free (seq); … … 329 436 void nnet_sortseq (float *X, int *Y, int N) { 330 437 331 # define SWAPFUNC(A,B){ float ftmp; int itmp; \438 # define SWAPFUNC(A,B){ float ftmp; int itmp; \ 332 439 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 } 335 442 # define COMPARE(A,B)(X[A] < X[B]) 336 443
Note:
See TracChangeset
for help on using the changeset viewer.
