| 6 |
| * http://CodePlea.com |
| 7 | * |
| 8 | * This software is provided 'as-is', without any express or implied |
| 9 | * warranty. In no event will the authors be held liable for any damages |
| 10 | * arising from the use of this software. |
| 11 | * |
| 12 | * Permission is granted to anyone to use this software for any purpose, |
| 13 | * including commercial applications, and to alter it and redistribute it |
| 14 | * freely, subject to the following restrictions: |
| 15 | * |
| 16 | * 1. The origin of this software must not be misrepresented; you must not |
| 17 | * claim that you wrote the original software. If you use this software |
| 18 | * in a product, an acknowledgement in the product documentation would be |
| 19 | * appreciated but is not required. |
| 20 | * 2. Altered source versions must be plainly marked as such, and must not be |
| 21 | * misrepresented as being the original software. |
| 22 | * 3. This notice may not be removed or altered from any source distribution. |
| 23 | * |
| 24 | */ |
| 25 |
| 26 | #include "genann.h" |
| 27 |
| 28 | #include <stdlib.h> |
| 29 | #include <string.h> |
| 30 | #include <math.h> |
| 31 | #include <assert.h> |
| 32 | #include <stdio.h> |
| 33 |
| 34 | #define LOOKUP_SIZE 4096 |
| 35 |
| 36 | double genann_act_sigmoid(double a) { |
| 37 | if (a < -45.0) return 0; |
| 38 | if (a > 45.0) return 1; |
| 39 | return 1.0 / (1 + exp(-a)); |
| 40 | } |
| 41 |
| 42 |
| 43 | double genann_act_sigmoid_cached(double a) { |
| 44 | /* If you're optimizing for memory usage, just |
| 45 | * delete this entire function and replace references |
| 46 | * of genann_act_sigmoid_cached to genann_act_sigmoid |
| 47 | */ |
| 48 | const double min = -15.0; |
| 49 | const double max = 15.0; |
| 50 | static double interval; |
| 51 | static int initialized = 0; |
| 52 | static double lookup[LOOKUP_SIZE]; |
| 53 |
| 54 | /* Calculate entire lookup table on first run. */ |
| 55 | if (!initialized) { |
| 56 | interval = (max - min) / LOOKUP_SIZE; |
| 57 | int i; |
| 58 | for (i = 0; i < LOOKUP_SIZE; ++i) { |
| 59 | lookup[i] = genann_act_sigmoid(min + interval * i); |
| 60 | } |
| 61 | /* This is down here to make this thread safe. */ |
| 62 | initialized = 1; |
| 63 | } |
| 64 |
| 65 | int i; |
| 66 | i = (int)((a-min)/interval+0.5); |
| 67 | if (i <= 0) return lookup[0]; |
| 68 | if (i >= LOOKUP_SIZE) return lookup[LOOKUP_SIZE-1]; |
| 69 | return lookup[i]; |
| 70 | } |
| 71 |
| 72 |
| 73 | double genann_act_threshold(double a) { |
| 74 | return a > 0; |
| 75 | } |
| 76 |
| 77 |
| 78 | double genann_act_linear(double a) { |
| 79 | return a; |
| 80 | } |
| 81 |
| 82 |
| 83 | genann *genann_init(int inputs, int hidden_layers, int hidden, int outputs) { |
| 84 | if (hidden_layers < 0) return 0; |
| 85 | if (inputs < 1) return 0; |
| 86 | if (outputs < 1) return 0; |
| 87 | if (hidden_layers > 0 && hidden < 1) return 0; |
| 88 |
| 89 |
| 90 | const int hidden_weights = hidden_layers ? (inputs+1) * hidden + (hidden_layers-1) * (hidden+1) * hidden : 0; |
| 91 | const int output_weights = (hidden_layers ? (hidden+1) : (inputs+1)) * outputs; |
| 92 | const int total_weights = (hidden_weights + output_weights); |
| 93 |
| 94 | const int total_neurons = (inputs + hidden * hidden_layers + outputs); |
| 95 |
| 96 | /* Allocate extra size for weights, outputs, and deltas. */ |
| 97 | const int size = sizeof(genann) + sizeof(double) * (total_weights + total_neurons + (total_neurons - inputs)); |
| 98 | genann *ret = malloc(size); |
| 99 | if (!ret) return 0; |
| 100 |
| 101 | ret->inputs = inputs; |
| 102 | ret->hidden_layers = hidden_layers; |
| 103 | ret->hidden = hidden; |
| 104 | ret->outputs = outputs; |
| 105 |
| 106 | ret->total_weights = total_weights; |
| 107 | ret->total_neurons = total_neurons; |
| 108 |
| 109 | /* Set pointers. */ |
| 110 | ret->weight = (double*)((char*)ret + sizeof(genann)); |
| 111 | ret->output = ret->weight + ret->total_weights; |
| 112 | ret->delta = ret->output + ret->total_neurons; |
| 113 |
| 114 | genann_randomize(ret); |
| 115 |
| 116 | ret->activation_hidden = genann_act_sigmoid_cached; |
| 117 | ret->activation_output = genann_act_sigmoid_cached; |
| 118 |
| 119 | return ret; |
| 120 | } |
| 121 |
| 122 |
| 123 | genann *genann_read(FILE *in) { |
| 124 | int inputs, hidden_layers, hidden, outputs; |
| 125 | fscanf(in, "%d %d %d %d", &inputs, &hidden_layers, &hidden, &outputs); |
| 126 |
| 127 | genann *ann = genann_init(inputs, hidden_layers, hidden, outputs); |
| 128 |
| 129 | int i; |
| 130 | for (i = 0; i < ann->total_weights; ++i) { |
| 131 | fscanf(in, " %le", ann->weight + i); |
| 132 | } |
| 133 |
| 134 | return ann; |
| 135 | } |
| 136 |
| 137 |
| 138 | genann *genann_copy(genann const *ann) { |
| 139 | const int size = sizeof(genann) + sizeof(double) * (ann->total_weights + ann->total_neurons + (ann->total_neurons - ann->inputs)); |
| 140 | genann *ret = malloc(size); |
| 141 | if (!ret) return 0; |
| 142 |
| 143 | memcpy(ret, ann, size); |
| 144 |
| 145 | /* Set pointers. */ |
| 146 | ret->weight = (double*)((char*)ret + sizeof(genann)); |
| 147 | ret->output = ret->weight + ret->total_weights; |
| 148 | ret->delta = ret->output + ret->total_neurons; |
| 149 |
| 150 | return ret; |
| 151 | } |
| 152 |
| 153 |
| 154 | void genann_randomize(genann *ann) { |
| 155 | int i; |
| 156 | for (i = 0; i < ann->total_weights; ++i) { |
| 157 | double r = GENANN_RANDOM(); |
| 158 | /* Sets weights from -0.5 to 0.5. */ |
| 159 | ann->weight[i] = r - 0.5; |
| 160 | } |
| 161 | } |
| 162 |
| 163 |
| 164 | void genann_free(genann *ann) { |
| 165 | /* The weight, output, and delta pointers go to the same buffer. */ |
| 166 | free(ann); |
| 167 | } |
| 168 |
| 169 |
| 170 | double const *genann_run(genann const *ann, double const *inputs) { |
| 171 | double const *w = ann->weight; |
| 172 | double *o = ann->output + ann->inputs; |
| 173 | double const *i = ann->output; |
| 174 |
| 175 | /* Copy the inputs to the scratch area, where we also store each neuron's |
| 176 | * output, for consistency. This way the first layer isn't a special case. */ |
| 177 | memcpy(ann->output, inputs, sizeof(double) * ann->inputs); |
| 178 |
| 179 | int h, j, k; |
| 180 |
| 181 | const genann_actfun act = ann->activation_hidden; |
| 182 | const genann_actfun acto = ann->activation_output; |
| 183 |
| 184 | /* Figure hidden layers, if any. */ |
| 185 | for (h = 0; h < ann->hidden_layers; ++h) { |
| 186 | for (j = 0; j < ann->hidden; ++j) { |
| 187 | double sum = 0; |
| 188 | for (k = 0; k < (h == 0 ? ann->inputs : ann->hidden) + 1; ++k) { |
| 189 | if (k == 0) { |
| 190 | sum += *w++ * -1.0; |
| 191 | } else { |
| 192 | sum += *w++ * i[k-1]; |
| 193 | } |
| 194 | } |
| 195 | *o++ = act(sum); |
| 196 | } |
| 197 |
| 198 |
| 199 | i += (h == 0 ? ann->inputs : ann->hidden); |
| 200 | } |
| 201 |
| 202 | double const *ret = o; |
| 203 |
| 204 | /* Figure output layer. */ |
| 205 | for (j = 0; j < ann->outputs; ++j) { |
| 206 | double sum = 0; |
| 207 | for (k = 0; k < (ann->hidden_layers ? ann->hidden : ann->inputs) + 1; ++k) { |
| 208 | if (k == 0) { |
| 209 | sum += *w++ * -1.0; |
| 210 | } else { |
| 211 | sum += *w++ * i[k-1]; |
| 212 | } |
| 213 | } |
| 214 | *o++ = acto(sum); |
| 215 | } |
| 216 |
| 217 | /* Sanity check that we used all weights and wrote all outputs. */ |
| 218 | assert(w - ann->weight == ann->total_weights); |
| 219 | assert(o - ann->output == ann->total_neurons); |
| 220 |
| 221 | return ret; |
| 222 | } |
| 223 |
| 224 |
| 225 | void genann_train(genann const *ann, double const *inputs, double const *desired_outputs, double learning_rate) { |
| 226 | /* To begin with, we must run the network forward. */ |
| 227 | genann_run(ann, inputs); |
| 228 |
| 229 | int h, j, k; |
| 230 |
| 231 | /* First set the output layer deltas. */ |
| 232 | { |
| 233 | double const *o = ann->output + ann->inputs + ann->hidden * ann->hidden_layers; /* First output. */ |
| 234 | double *d = ann->delta + ann->hidden * ann->hidden_layers; /* First delta. */ |
| 235 | double const *t = desired_outputs; /* First desired output. */ |
| 236 |
| 237 |
| 238 | /* Set output layer deltas. */ |
| 239 | if (ann->activation_output == genann_act_linear) { |
| 240 | for (j = 0; j < ann->outputs; ++j) { |
| 241 | *d++ = *t++ - *o++; |
| 242 | } |
| 243 | } else { |
| 244 | for (j = 0; j < ann->outputs; ++j) { |
| 245 | *d++ = (*t - *o) * *o * (1.0 - *o); |
| 246 | ++o; ++t; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 |
| 251 |
| 252 | /* Set hidden layer deltas, start on last layer and work backwards. */ |
| 253 | /* Note that loop is skipped in the case of hidden_layers == 0. */ |
| 254 | for (h = ann->hidden_layers - 1; h >= 0; --h) { |
| 255 |
| 256 | /* Find first output and delta in this layer. */ |
| 257 | double const *o = ann->output + ann->inputs + (h * ann->hidden); |
| 258 | double *d = ann->delta + (h * ann->hidden); |
| 259 |
| 260 | /* Find first delta in following layer (which may be hidden or output). */ |
| 261 | double const * const dd = ann->delta + ((h+1) * ann->hidden); |
| 262 |
| 263 | /* Find first weight in following layer (which may be hidden or output). */ |
| 264 | double const * const ww = ann->weight + ((ann->inputs+1) * ann->hidden) + ((ann->hidden+1) * ann->hidden * (h)); |
| 265 |
| 266 | for (j = 0; j < ann->hidden; ++j) { |
| 267 |
| 268 | double delta = 0; |
| 269 |
| 270 | for (k = 0; k < (h == ann->hidden_layers-1 ? ann->outputs : ann->hidden); ++k) { |
| 271 | const double forward_delta = dd[k]; |
| 272 | const int windex = k * (ann->hidden + 1) + (j + 1); |
| 273 | const double forward_weight = ww[windex]; |
| 274 | delta += forward_delta * forward_weight; |
| 275 | } |
| 276 |
| 277 | *d = *o * (1.0-*o) * delta; |
| 278 | ++d; ++o; |
| 279 | } |
| 280 | } |
| 281 |
| 282 |
| 283 | /* Train the outputs. */ |
| 284 | { |
| 285 | /* Find first output delta. */ |
| 286 | double const *d = ann->delta + ann->hidden * ann->hidden_layers; /* First output delta. */ |
| 287 |
| 288 | /* Find first weight to first output delta. */ |
| 289 | double *w = ann->weight + (ann->hidden_layers |
| 290 | ? ((ann->inputs+1) * ann->hidden + (ann->hidden+1) * ann->hidden * (ann->hidden_layers-1)) |
| 291 | : (0)); |
| 292 |
| 293 | /* Find first output in previous layer. */ |
| 294 | double const * const i = ann->output + (ann->hidden_layers |
| 295 | ? (ann->inputs + (ann->hidden) * (ann->hidden_layers-1)) |
| 296 | : 0); |
| 297 |
| 298 | /* Set output layer weights. */ |
| 299 | for (j = 0; j < ann->outputs; ++j) { |
| 300 | for (k = 0; k < (ann->hidden_layers ? ann->hidden : ann->inputs) + 1; ++k) { |
| 301 | if (k == 0) { |
| 302 | *w++ += *d * learning_rate * -1.0; |
| 303 | } else { |
| 304 | *w++ += *d * learning_rate * i[k-1]; |
| 305 | } |
| 306 | } |
| 307 |
| 308 | ++d; |
| 309 | } |
| 310 |
| 311 | assert(w - ann->weight == ann->total_weights); |
| 312 | } |
| 313 |
| 314 |
| 315 | /* Train the hidden layers. */ |
| 316 | for (h = ann->hidden_layers - 1; h >= 0; --h) { |
| 317 |
| 318 | /* Find first delta in this layer. */ |
| 319 | double const *d = ann->delta + (h * ann->hidden); |
| 320 |
| 321 | /* Find first input to this layer. */ |
| 322 | double const *i = ann->output + (h |
| 323 | ? (ann->inputs + ann->hidden * (h-1)) |
| 324 | : 0); |
| 325 |
| 326 | /* Find first weight to this layer. */ |
| 327 | double *w = ann->weight + (h |
| 328 | ? ((ann->inputs+1) * ann->hidden + (ann->hidden+1) * (ann->hidden) * (h-1)) |
| 329 | : 0); |
| 330 |
| 331 |
| 332 | for (j = 0; j < ann->hidden; ++j) { |
| 333 | for (k = 0; k < (h == 0 ? ann->inputs : ann->hidden) + 1; ++k) { |
| 334 | if (k == 0) { |
| 335 | *w++ += *d * learning_rate * -1.0; |
| 336 | } else { |
| 337 | *w++ += *d * learning_rate * i[k-1]; |
| 338 | } |
| 339 | } |
| 340 | ++d; |
| 341 | } |
| 342 |
| 343 | } |
| 344 |
| 345 | } |
| 346 |
| 347 |
| 348 | void genann_write(genann const *ann, FILE *out) { |
| 349 | fprintf(out, "%d %d %d %d", ann->inputs, ann->hidden_layers, ann->hidden, ann->outputs); |
| 350 |
| 351 | int i; |
| 352 | for (i = 0; i < ann->total_weights; ++i) { |
| 353 | fprintf(out, " %.20e", ann->weight[i]); |
| 354 | } |
| 355 | } |
| 356 |
| 357 |
| 358 |