Changeset 40319 for trunk/Ohana/src/opihi/cmd.data/nnet_commands.c
- Timestamp:
- Jan 23, 2018, 10:11:50 AM (9 years ago)
- File:
-
- 1 edited
-
trunk/Ohana/src/opihi/cmd.data/nnet_commands.c (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/opihi/cmd.data/nnet_commands.c
r40317 r40319 12 12 } 13 13 14 /** Fix These ... 15 int nnet_init (int argc, char **argv) { 16 14 int nnet_delete (int argc, char **argv) { 15 16 # if (0) 17 17 int status; 18 18 Nnet *nnet; 19 19 20 20 if (argc != 2) { 21 gprint (GP_ERR, "USAGE: nnet init (nnet)\n");22 return FALSE;23 }24 25 nnet = FindNnet (argv[1]);26 if (nnet != NULL) {27 status = DeleteNnet (nnet);28 if (!status) abort ();29 }30 31 CreateNnet (argv[1]);32 return TRUE;33 }34 35 int nnet_delete (int argc, char **argv) {36 37 int status;38 Nnet *nnet;39 40 if (argc != 2) {41 21 gprint (GP_ERR, "USAGE: nnet delete (nnet)\n"); 42 22 return FALSE; … … 51 31 status = DeleteNnet (nnet); 52 32 if (!status) abort (); 33 # endif 34 53 35 return TRUE; 54 36 } … … 56 38 int nnet_show (int argc, char **argv) { 57 39 40 # if (0) 58 41 Nnet *nnet; 59 42 … … 70 53 71 54 ListPages (nnet); 72 return TRUE; 73 } 74 **/ 55 # endif 56 57 return TRUE; 58 } 75 59 76 60 int nnet_create (int argc, char **argv) { … … 97 81 } 98 82 83 int nnet_set (int argc, char **argv) { 84 85 Buffer *matrix; 86 Vector *vector; 87 88 if (argc < 5) { 89 gprint (GP_ERR, "USAGE: nnet set (nnet) [weights] [biases] ... [weights] [biases] : set nnet weights (images) and biases (vectors)\n"); 90 return FALSE; 91 } 92 93 Nnet *nnet = FindNnet (argv[1]); 94 if (nnet == NULL) { 95 gprint (GP_ERR, "nnet %s not found, create it first\n", argv[1]); 96 return FALSE; 97 } 98 99 // compare argc and nnet[0].Nlayer 100 if (argc - 2 != nnet[0].Nlayer * 2) { 101 gprint (GP_ERR, "ERROR: invalid number of arguments\n"); 102 gprint (GP_ERR, "USAGE: nnet set (nnet) [weights] [biases] ... [weights] [biases] : set nnet weights (images) and biases (vectors)\n"); 103 return FALSE; 104 } 105 106 // check for existence of each matrix & vector (and check sizes) 107 for (int L = 1; L < nnet[0].Nlayer; L++) { 108 int Narg = 2*(L - 1) + 2; 109 if ((matrix = SelectBuffer (argv[Narg + 0], OLDBUFFER, FALSE)) == NULL) { 110 gprint (GP_ERR, "unknown input image %s\n", argv[Narg + 0]); 111 gprint (GP_ERR, "USAGE: nnet set (nnet) [weights] [biases] ... [weights] [biases] : set nnet weights (images) and biases (vectors)\n"); 112 return (FALSE); 113 } 114 if ((vector = SelectVector (argv[Narg + 1], OLDVECTOR, FALSE)) == NULL) { 115 gprint (GP_ERR, "unknown input vector %s\n", argv[Narg + 1]); 116 gprint (GP_ERR, "USAGE: nnet set (nnet) [weights] [biases] ... [weights] [biases] : set nnet weights (images) and biases (vectors)\n"); 117 return (FALSE); 118 } 119 120 if (vector->Nelements != nnet[0].Nnodes[L]) { 121 gprint (GP_ERR, "invalid bias vector length %s (%d vs %d)\n", argv[Narg + 1], vector->Nelements, nnet[0].Nnodes[L]); 122 gprint (GP_ERR, "USAGE: nnet set (nnet) [weights] [biases] ... [weights] [biases] : set nnet weights (images) and biases (vectors)\n"); 123 } 124 125 if (matrix->matrix.Naxis[0] != nnet[0].Nnodes[L-1]) { 126 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]); 127 gprint (GP_ERR, "USAGE: nnet set (nnet) [weights] [biases] ... [weights] [biases] : set nnet weights (images) and biases (vectors)\n"); 128 } 129 130 if (matrix->matrix.Naxis[1] != nnet[0].Nnodes[L]) { 131 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]); 132 gprint (GP_ERR, "USAGE: nnet set (nnet) [weights] [biases] ... [weights] [biases] : set nnet weights (images) and biases (vectors)\n"); 133 } 134 135 } 136 137 for (int L = 1; L < nnet[0].Nlayer; L++) { 138 139 int Narg = 2*(L - 1) + 2; 140 matrix = SelectBuffer (argv[Narg + 0], OLDBUFFER, FALSE); // I've checked the argv entry above so I do not need to check again... 141 vector = SelectVector (argv[Narg + 1], OLDVECTOR, FALSE); 142 143 opihi_flt *vecvalue = vector[0].elements.Flt; 144 for (int i = 0; i < nnet[0].Nnodes[L]; i++) { 145 nnet[0].biases[L][i] = vecvalue[i]; 146 } 147 148 float *matvalue = (float *) matrix[0].matrix.buffer; 149 for (int j = 0; j < nnet[0].Nnodes[L]; j++) { 150 for (int i = 0; i < nnet[0].Nnodes[L-1]; i++) { 151 int k = i + j*nnet[0].Nnodes[L-1]; 152 nnet[0].weight[L][k] = matvalue[k]; 153 } 154 } 155 156 } 157 158 return TRUE; 159 } 160 161 int nnet_get (int argc, char **argv) { 162 163 Buffer *matrix; 164 Vector *vector; 165 166 if (argc < 5) { 167 gprint (GP_ERR, "USAGE: nnet get (nnet) [weights] [biases] ... [weights] [biases] : get nnet weights (images) and biases (vectors)\n"); 168 return FALSE; 169 } 170 171 Nnet *nnet = FindNnet (argv[1]); 172 if (nnet == NULL) { 173 gprint (GP_ERR, "nnet %s not found, create it first\n", argv[1]); 174 return FALSE; 175 } 176 177 // compare argc and nnet[0].Nlayer 178 if (argc - 2 != nnet[0].Nlayer * 2) { 179 gprint (GP_ERR, "ERROR: invalid number of arguments\n"); 180 gprint (GP_ERR, "USAGE: nnet set (nnet) [weights] [biases] ... [weights] [biases] : set nnet weights (images) and biases (vectors)\n"); 181 return FALSE; 182 } 183 184 // check for existence of each matrix & vector (and check sizes) 185 for (int L = 1; L < nnet[0].Nlayer; L++) { 186 int Narg = 2*(L - 1) + 2; 187 if ((matrix = SelectBuffer (argv[Narg + 0], ANYBUFFER, FALSE)) == NULL) { 188 gprint (GP_ERR, "unknown input image %s\n", argv[Narg + 0]); 189 gprint (GP_ERR, "USAGE: nnet set (nnet) [weights] [biases] ... [weights] [biases] : set nnet weights (images) and biases (vectors)\n"); 190 return (FALSE); 191 } 192 if ((vector = SelectVector (argv[Narg + 1], ANYVECTOR, FALSE)) == NULL) { 193 gprint (GP_ERR, "unknown input vector %s\n", argv[Narg + 1]); 194 gprint (GP_ERR, "USAGE: nnet set (nnet) [weights] [biases] ... [weights] [biases] : set nnet weights (images) and biases (vectors)\n"); 195 return (FALSE); 196 } 197 } 198 199 for (int L = 1; L < nnet[0].Nlayer; L++) { 200 201 int Narg = 2*(L - 1) + 2; 202 matrix = SelectBuffer (argv[Narg + 0], OLDBUFFER, FALSE); // I've checked the argv entry above so I do not need to check again... 203 vector = SelectVector (argv[Narg + 1], OLDVECTOR, FALSE); 204 205 ResetVector (vector, OPIHI_FLT, nnet[0].Nnodes[L]); 206 ResetBuffer (matrix, nnet[0].Nnodes[L-1], nnet[0].Nnodes[L], -32, 0.0, 1.0); 207 208 opihi_flt *vecvalue = vector[0].elements.Flt; 209 for (int i = 0; i < nnet[0].Nnodes[L]; i++) { 210 vecvalue[i] = nnet[0].biases[L][i]; 211 } 212 213 float *matvalue = (float *) matrix[0].matrix.buffer; 214 for (int j = 0; j < nnet[0].Nnodes[L]; j++) { 215 for (int i = 0; i < nnet[0].Nnodes[L-1]; i++) { 216 int k = i + j*nnet[0].Nnodes[L-1]; 217 matvalue[k] = nnet[0].weight[L][k]; 218 } 219 } 220 221 } 222 223 return TRUE; 224 } 225
Note:
See TracChangeset
for help on using the changeset viewer.
