-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboardTraining.CPP
300 lines (252 loc) · 12.4 KB
/
boardTraining.CPP
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//Written by: Rijo Simon
//Email: rssimon@alaska.edu
#include <iostream>
#include <stdlib.h>
#include <chrono>
#include <ctime>
#include <math.h> //For tanh
using namespace std;
// returns a float in the range -1.0f -> - 1.0f
#define RANDOM_CLAMP (((float)rand()-(float)rand())/RAND_MAX)
//returns a float between 0 & 1
#define RANDOM_NUM ((float)rand()/(RAND_MAX+1))
//using namespace std;
class Dendrite {
public:
double d_weight; //Weight of the neuron
unsigned long d_points_to; //The index of the neuron of the next layer to which it points
Dendrite(double weight=0.0, unsigned long points_to=0){ //Constructor
d_weight=weight;
d_points_to=points_to; //Give it a initial value
}
};
class Neuron {
public:
unsigned long n_ID; //ID of a particular neuron in a layer
//Used to find a particular neuron in an array
double n_value; //Value which Neuron currently is holding
double n_bias; //Bias of the neuron
double n_delta; //Used in back prop. Note it is backprop specific
Dendrite *Dendrites; //Dendrites
//Constructor assigning initial values
Neuron(unsigned long ID=0,double value=0.0,double bias=0.0){
n_ID=ID;
n_value=value;
n_bias=bias;
n_delta=0.0;
}
void SetDendrites(unsigned long dendrite){ //Set the dendrites from the neuron to given dendrite
Dendrites=new Dendrite[dendrite];
for(int i=0;i<dendrite;i++){
Dendrites[i].d_points_to=i; // Initialize the dendrite to attach to next layer
}
}
};
class Layer{
public:
Neuron *Neurons; //Pointer to array of neurons
/*Layer(unsigned long size=1){ //size is no. of neurons in it
Neurons = new Neuron [size];
} */
void Initialize(unsigned long size) { //Initialize the layer
Neurons = new Neuron [size];
}
~Layer(){ //destructor deletes Neurons from the memory
delete Neurons;
}
Neuron GetNeuron(unsigned long index){ //Give the neuron at index
return Neurons[index];
}
void SetNeuron(Neuron neuron,unsigned long index){ //sets the neuron
Neurons[index]=neuron;
}
};
class Network { //The real neural network
public:
double net_learning_rate; //Learning rate of network
Layer *Layers; //The total layers in network
unsigned long net_tot_layers; //Number of layers
double *net_inputs; //Input array
double *net_outputs;//Output layers
unsigned long *net_layers; //Array which tells no. of neurons in each layer
//double GetRand(void);
Network() {
//Blank Constructor
}
int SetData(double learning_rate,unsigned long layers[],unsigned long tot_layers) { //Function to set various parameters of the net
if (tot_layers<2) return(-1); //Return error if total no. of layers < 2
//Because input and output layers are necessary
net_learning_rate=learning_rate;
net_layers= new unsigned long [tot_layers]; //Initialize the layers array
Layers=new Layer[tot_layers];
for(int i=0;i<tot_layers;i++){
net_layers[i]=layers[i];
Layers[i].Initialize(layers[i]); //Initialize each layer with the specified size
}
net_inputs=new double[layers[0]];
net_outputs=new double[layers[tot_layers-1]];
net_tot_layers=tot_layers;
return 0;
}
int SetInputs(double inputs[]){ //Function to set the inputs
for(int i=0;i<net_layers[0];i++){
Layers[0].Neurons[i].n_value=inputs[i];
}
return 0;}
void RandomizeWB(void){ //Randomize weights and biases
int i,j,k;
for(i=0;i<net_tot_layers;i++){
for(j=0;j<net_layers[i];j++){
if(i!=(net_tot_layers-1)){ //Last layer does not require weights
Layers[i].Neurons[j].SetDendrites(net_layers[i+1]); //Initialize the dendites
for(k=0;k<net_layers[i+1];k++){
Layers[i].Neurons[j].Dendrites[k].d_weight=GetRand(); //Let weight be the random value
}
}
if(i!=0){ //First layer does not need biases
Layers[i].Neurons[j].n_bias=GetRand();
}
}
}
}
double * GetOutput(void){ //Gives the output of the net
double *outputs;
int i,j,k;
outputs=new double[net_layers[net_tot_layers-1]]; //Temp ouput array
for(i=1;i<net_tot_layers;i++){
for(j=0;j<net_layers[i];j++){
Layers[i].Neurons[j].n_value=0;
for(k=0;k<net_layers[i-1];k++){
Layers[i].Neurons[j].n_value=Layers[i].Neurons[j].n_value+Layers[i-1].Neurons[k].n_value*Layers[i-1].Neurons[k].Dendrites[j].d_weight; //Multiply and add all the inputs
}
Layers[i].Neurons[j].n_value=Layers[i].Neurons[j].n_value+Layers[i].Neurons[j].n_bias; //Add bias
Layers[i].Neurons[j].n_value=Limiter(Layers[i].Neurons[j].n_value); //Squash that value
}
}
for(i=0;i<net_layers[net_tot_layers-1];i++){
outputs[i]=Layers[net_tot_layers-1].Neurons[i].n_value;
}
return outputs; //Return the outputs
}
void Update(void){ //Just a dummy function
//double *temp; //Temperory pointer
//temp=GetOutput();
GetOutput();
}
/* void SetOutputs(double outputs[]){ //Set the values of the output layer
for(int i=0;i<net_layers[net_tot_layers-1];i++){
Layers[net_tot_layers-1].Neurons[i].n_value=outputs[i]; //Set the value
}
} */
double Limiter(double value){ //Limiet to limit value between 1 and -1
//return tanh(value); //Currently using tanh
return (1.0/(1+exp(-value)));
}
double GetRand(void){ //Return a random number between range -1 to 1 using time to seed the srand function
time_t timer;
struct tm *tblock;
timer=time(NULL);
tblock=localtime(&timer);
int seed=int(tblock->tm_sec+100*RANDOM_CLAMP+100*RANDOM_NUM);
//srand(tblock->tm_sec);
srand(seed);
return (RANDOM_CLAMP+RANDOM_NUM);
}
double SigmaWeightDelta(unsigned long layer_no, unsigned long neuron_no){ //Calculate sum of weights * delta. Used in back prop. layer_no is layer number. Layer number and neuron number can be zero
double result=0.0;
for(int i=0;i<net_layers[layer_no+1];i++) { //Go through all the neurons in the next layer
result=result+Layers[layer_no].Neurons[neuron_no].Dendrites[i].d_weight*Layers[layer_no+1].Neurons[i].n_delta; //Comput the summation
}
return result;
} //neuron_no is neuron number. This function is used in back prop
/*For output layer:
Delta = (TargetO - ActualO) * ActualO * (1 - ActualO)
Weight = Weight + LearningRate * Delta * Input
For hidden layers:
Delta = ActualO * (1-ActualO) * Summation(Weight_from_current_to_next AND Delta_of_next)
Weight = Weight + LearningRate * Delta * Input
*/
int Train(double inputs[],double outputs[]){ //The standard Backprop Learning algorithm
int i,j,k;
double Target, Actual, Delta;
SetInputs(inputs); //Set the inputs
Update(); //Update all the values
//SetOutputs(outputs); //Set the outputs
for(i=net_tot_layers-1;i>0;i--){ //Go from last layer to first layer
for(j=0;j<net_layers[i];j++) {//Go thru every neuron
if(i==net_tot_layers-1){ //Output layer, Needs special atential
Target=outputs[j]; //Target value
Actual=Layers[i].Neurons[j].n_value; //Actual value
Delta= (Target - Actual) * Actual * (1 - Actual); //Function to compute error
Layers[i].Neurons[j].n_delta=Delta; //Compute the delta
for(k=0;k<net_layers[i-1];k++) {
Layers[i-1].Neurons[k].Dendrites[j].d_weight += Delta*net_learning_rate*Layers[i-1].Neurons[k].n_value; //Calculate the new weights
}
Layers[i].Neurons[j].n_bias = Layers[i].Neurons[j].n_bias + Delta*net_learning_rate*1; //n_value is always 1 for bias
} else { //Here
//Target value
Actual=Layers[i].Neurons[j].n_value; //Actual value
Delta= Actual * (1 - Actual)* SigmaWeightDelta(i,j); //Function to compute error
for(k=0;k<net_layers[i-1];k++){
Layers[i-1].Neurons[k].Dendrites[j].d_weight += Delta*net_learning_rate*Layers[i-1].Neurons[k].n_value; //Calculate the new weights
}
if(i!=0) //Input layer does not have a bias
Layers[i].Neurons[j].n_bias = Layers[i].Neurons[j].n_bias + Delta*net_learning_rate*1; //n_value is always 1 for bias
}
}
} return 0;
}
~Network(){ //delete Layers;
}
};
int main()
{ Network my;
unsigned long inp=2;
unsigned long hid=2;
unsigned long outp=1;
unsigned long layers[3];
layers[0]=inp;
layers[1]=hid;
layers[2]=outp;
int i=0,j=0;
unsigned long iter=0;
cout<<"Enter number of training Iterations : ";
cin>>iter;
my.SetData(0.1,layers,3);
double input[]={1,0};
double *outputs;
my.RandomizeWB();
double tr_inp[4][2]={{0.0,0.0},{1.0,0.0},{0.0,1.0},{1.0,1.0}};
double tr_out[4][1]={{0.0},{1.0},{1.0},{0.0}};
cout<<"\nStarting Training... ";
std::chrono::time_point<std::chrono::system_clock> start, end;
start = std::chrono::system_clock::now();
for(i=0;i<iter;i++){
//cout<<"\nTraining : "<<i+1;
for(j=0;j<4;j++){
my.Train(tr_inp[j],tr_out[j]);
}}
end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end-start;
std::time_t end_time = std::chrono::system_clock::to_time_t(end);
std::cout << "finished computation at " << std::ctime(&end_time)<< "elapsed time: " << elapsed_seconds.count() << "s\n";
cout<<"\nEnding Training. ";
cout<<"\n\nStarting Testing... \n";
for(j=0;j<4;j++){
cout<<"\n\nCase number : "<<j+1;
my.SetInputs(tr_inp[j]);
outputs=my.GetOutput();
for(i=0;i<inp;i++){
cout<<"\nInput"<<i+1<<" : "<<tr_inp[j][i];
}
for(i=0;i<outp;i++){
cout<<"\nOutput"<<i+1<<" : "<<outputs[i];
}
delete outputs;
double *outputs;
}
cout<<"\n\nEnd Testing.\n\n";
//cin.get();
//system("PAUSE");
return 0;
}