-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathFullyConnectedLayer.cpp
More file actions
177 lines (144 loc) · 4.59 KB
/
FullyConnectedLayer.cpp
File metadata and controls
177 lines (144 loc) · 4.59 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "FullyConnectedLayer.h"
#include "activationFunctions.h"
#include "StandardUnit.h"
#include <algorithm>
#include <cstdio>
#include <random>
using namespace std;
void FullyConnectedLayer::backProp(int pass){
for(int i=0; i <height;i++){
for(int j=0; j <width;j++){
Neuron* n = neurons[i][j][0];
int ph = n->iy;
int pw = n->ix;
int pd = n->iz;
//printf("got neuron at %d %d looking through: %d %d %d\n",i,j,ph,pw,pd);
float errorZGradient = n->errorZGradient;
//printf("errorZ %d %d %f\n",i,j,errorZGradient);
for(int y=0;y<ph;y++){
for(int x=0;x<pw;x++){
for(int z = 0; z < pd;z++){
Neuron* input = n->getInput(y,x,z);
float output = input->getOutput(pass);
float weight = n->weights[y][x][z];
input->addToYError(errorZGradient * weight,pass);
float errorW = weightDecay*weight+output*errorZGradient;
float deltaW = -epsilon*errorW;// = //INSERT//////////////////////////////////////////////
n->deltaWeights[y][x][z]+=deltaW;
//if(y==0 && x==0 && z==0)printf("%d %d %d changing the weight by: %f to %f output %f epsilon %f errorz %f\n",y,x,z,deltaW,n->weights[y][x][z], output, epsilon, errorZGradient);
}
}
}
float deltaBias = -epsilon*errorZGradient;
//printf("changing bias by %f \n",deltaBias);
//printf("deltaBias %d %d %f z %f\n",i,j,deltaBias,errorZGradient);
*(n->deltaBias)+=deltaBias;
}
}
//printf("to next layer\n");
previousLayer->backProp(pass);
}
void FullyConnectedLayer::updateWeights(int pass){
for(int i=0; i <height;i++){
for(int j=0; j <width;j++){
Neuron* n = neurons[i][j][0];
int ph = n->iy;
int pw = n->ix;
int pd = n->iz;
for(int y=0;y<ph;y++){
for(int x=0;x<pw;x++){
for(int z = 0; z < pd;z++){
n->weights[y][x][z]+=n->deltaWeights[y][x][z];
n->deltaWeights[y][x][z]*=momentum;
}
}
}
*(n->bias) += *(n->deltaBias);
*n->deltaBias = 0;
}
}
previousLayer->updateWeights(pass);
}
Neuron**** FullyConnectedLayer::initNeuronArray(int width, int height){
Neuron**** array = new Neuron*** [height];
for(int i =0; i<height;i++){
array[i] = new Neuron** [width];
for(int j = 0; j < width; j++){
array[i][j] = new Neuron*[1];
StandardUnit* fcu = new StandardUnit();
array[i][j][0] = fcu;
fcu->bias = new float;
*(fcu->bias) = initBias();
fcu->deltaBias = new float;
*(fcu->deltaBias) = 0;
fcu->weights = initWeightArray(previousLayer->width,previousLayer->height, previousLayer->depth);
fcu->deltaWeights = initDeltaWeightArray(previousLayer->width,previousLayer->height, previousLayer->depth);
fcu->layer = this;
fcu->lx = j;
fcu->ly = i;
fcu->lz = 0;
fcu->ix = previousLayer->width;
fcu->iy = previousLayer->height;
fcu->iz = previousLayer->depth;
}
}
printf("created fully connected array\n");
return array;
}
float*** FullyConnectedLayer::initDeltaWeightArray(int width, int height,int depth){
float*** array = new float** [height];
for(int i =0; i<height;i++){
array[i] = new float* [width];
for(int j = 0; j < width; j++){
array[i][j] = new float[depth];
for(int k = 0; k < depth; k++){
array[i][j][k] = 0;
}
}
}
return array;
}
float*** FullyConnectedLayer::initWeightArray(int width, int height,int depth){
float*** array = new float** [height];
for(int i =0; i<height;i++){
array[i] = new float* [width];
for(int j = 0; j < width; j++){
array[i][j] = new float[depth];
for(int k = 0; k < depth; k++){
array[i][j][k] = initWeight();
}
}
}
return array;
}
float FullyConnectedLayer::initBias(){
return 0;//FIX------------
}
float FullyConnectedLayer::initWeight(){
//unsigned int b = eng();
float a = normal(generator);
//printf("got float: %f\n",a);
return a;
}
Neuron* FullyConnectedLayer::getInput(int, int, int, int ix, int iy, int iz){
return previousLayer->neurons[iy][ix][iz];
}
//depth assumed to be one
FullyConnectedLayer::FullyConnectedLayer(int width,int height, Layer* previousLayer, float (*activationFunction) (float),float (*actGrad)(float)){
int rand = (int)previousLayer;
normal = std::normal_distribution<float>(0,(float).1);
generator = std::default_random_engine (rand);
printf("creating fully connected layer\n");
epsilon = (float)DEFAULT_EPSILON;
momentum = (float)DEFAULT_MOMENTUM;
this->activationFunction = activationFunction;
this->activationGradient = actGrad;
this->activationGradient(0);
this->width = width;
this->height = height;
this->depth = 1;
this->previousLayer = previousLayer;
neurons = initNeuronArray(width,height);
//connectLayer();
printf("done connecting fully connected layer\n");
}