-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLayer.cpp
More file actions
68 lines (51 loc) · 1.82 KB
/
Layer.cpp
File metadata and controls
68 lines (51 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "Layer.h"
#include <stdlib.h>
#include <stdio.h>
Layer::Layer(int iWidth, int oWidth) {
inputWidth = iWidth;
outputWidth = oWidth;
neurons = (Neuron*) malloc(sizeof(Neuron)*outputWidth);
for(Neuron* n = neurons; n < neurons+outputWidth; ++n) *n = Neuron(inputWidth);
}
void Layer::eval(float* i, float* res) {
for(int x = 0; x < outputWidth; x++) {
res[x] = neurons[x].eval(i);
}
}
void Layer::train(float** pLayerError, float* layerOutput, float* lastLayerInput) {
float*& layerError = *pLayerError;
//TODO(Sam): let network handle this and just pass layer the mem
float* newLayerError = cachedLayerError ? cachedLayerError : new float[inputWidth];
for(float* f = newLayerError; f < newLayerError+inputWidth; ++f) *f = 0;
for(int x = 0; x < outputWidth; ++x) {
float output = layerOutput[x];
float errDeriv;
//check errDeriv overFlow
if(output == 1) {
#if(PRINT_LAYER_OVERFLOW == 1)
printf("\t - WARNING! output == 1\n");
#endif
errDeriv = layerError[x]*derivOverflowVal;
} else if(layerError[x] && !output) {
#if(PRINT_LAYER_UNDERFLOW == 1)
printf("\t - WARNING! output == 0 with LayerError\n");
#endif
errDeriv = layerError[x] * outputUnderflowVal;
} else errDeriv = layerError[x] * output*(1-output);
neurons[x].train(errDeriv, lastLayerInput, newLayerError);
}
//check layerError overflow
for(float* f = newLayerError; f < newLayerError+inputWidth; ++f) {
if(*f > maxLayerError) {
#if(PRINT_LAYER_MAXERROR_OVERFLOW)
printf("\t - WARNING! maxLayerError overflow!\n");
#endif
*f = maxLayerError;
}
}
layerError = newLayerError;
}
//TODO: play with these
float Layer::outputUnderflowVal = .1f*(1-.1f);
float Layer::derivOverflowVal = .9f*(1-.9f);
float Layer::maxLayerError = 1000.f;