This repository has been archived by the owner on May 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
neuronTest.cpp
138 lines (130 loc) · 5.52 KB
/
neuronTest.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
#include <stdio.h>
#include "neuron.h"
#include <assert.h>
#define indexW(d,i,j) (d*i+j)
int main() {
int neuronAmount = 3; //one per each activation type
int numberOfDentrites = 7;
Neuron* neuronArray = new Neuron [neuronAmount];
double* synapses = new double[numberOfDentrites];
double* weights = new double[numberOfDentrites*neuronAmount];
double* inputs = new double[numberOfDentrites*neuronAmount];
double* temporalWeights = weights;
double initialWeight = 0.01;
double temporalWeight = initialWeight;
//create weights & input map
for(int neuronIndex = 0 ;neuronIndex < neuronAmount;neuronIndex++ ){
for(int weightsIndex = 0 ; weightsIndex < numberOfDentrites;weightsIndex++ ){
weights[indexW(numberOfDentrites, neuronIndex, weightsIndex)] = temporalWeight;
inputs[indexW(numberOfDentrites, neuronIndex, weightsIndex)] = temporalWeight * 10.0;
temporalWeight += 0.01;
}
}
neuronArray[0].init(numberOfDentrites, ACTIVATION_FUNCTION_LINEAL, BIAS_NONE);
neuronArray[1].init(numberOfDentrites, ACTIVATION_FUNCTION_SIGMOID, BIAS_NONE);
neuronArray[2].init(numberOfDentrites, ACTIVATION_FUNCTION_HIPERBOLIC_TANGENT, BIAS_NONE);
temporalWeight = initialWeight;
for(int neuronIndex = 0 ;neuronIndex < neuronAmount;neuronIndex++ ){
temporalWeights = &weights[indexW(numberOfDentrites,neuronIndex,0)];
neuronArray[neuronIndex].setSynapses(7, temporalWeights);
}
//validate synapsis values
temporalWeight = initialWeight;
for(int neuronIndex = 0 ;neuronIndex < neuronAmount;neuronIndex++ ){
temporalWeights = &weights[indexW(numberOfDentrites,neuronIndex,0)];
double* temporalSynapsis = neuronArray[neuronIndex].getSynapses();
for(int weightsIndex = 0 ; weightsIndex < numberOfDentrites;weightsIndex++ ){
printf("Validating synapsis, the element %i %f == %f \n",weightsIndex,
temporalWeights[weightsIndex] , temporalSynapsis[weightsIndex]);
assert( temporalWeights[weightsIndex] == temporalSynapsis[weightsIndex]);
temporalWeight += 0.01;
}
}
for(int neuronIndex = 0 ;neuronIndex < neuronAmount;neuronIndex++ ){
temporalWeights = &inputs[indexW(numberOfDentrites,neuronIndex,0)];
printf("Validating inputs of the neuron %i \n",neuronIndex);
double outputCollection = neuronArray[neuronIndex].getOutput(numberOfDentrites, temporalWeights);
printf("Validating output, the element %i == %f \n", neuronIndex, outputCollection);
switch (neuronIndex){
case 0:
assert( outputCollection > 0.139 && outputCollection < 0.141);
break;
case 1:
assert( outputCollection > 0.704 && outputCollection < 0.706);
break;
case 2:
assert( outputCollection > 0.97 && outputCollection < 0.98);
break;
default:
break;
}
}
for(int neuronIndex = 0 ;neuronIndex < neuronAmount;neuronIndex++ ){
double outputCollection = neuronArray[neuronIndex].getLastOutcome();
printf("Validating getLastExit, the element %i == %f \n", neuronIndex, outputCollection);
switch (neuronIndex){
case 0:
assert( outputCollection > 0.139 && outputCollection < 0.141);
break;
case 1:
assert( outputCollection > 0.704 && outputCollection < 0.706);
break;
case 2:
assert( outputCollection > 0.97 && outputCollection < 0.98);
break;
default:
break;
}
}
double sigma ;
for(int neuronIndex = 0 ;neuronIndex < neuronAmount;neuronIndex++ ){
double outputCollection = neuronArray[neuronIndex].getLastOutcome();
switch (neuronIndex){
case 0:
//get last sigma
sigma = neuronArray[neuronIndex].calculateSigma(0.15-outputCollection);
printf("Validating sigma %f \n", sigma);
assert( sigma > -0.01 && sigma < 0.01);
break;
case 1:
sigma = neuronArray[neuronIndex].calculateSigma(0.708-outputCollection);
printf("Validating sigma %f \n", sigma);
assert( sigma > -0.01 && sigma < 0.01);
break;
case 2:
sigma = neuronArray[neuronIndex].calculateSigma(0.908-outputCollection);
printf("Validating sigma %f \n", sigma);
assert( sigma > -0.01 && sigma < 0.01);
break;
default:
break;
}
}
for(int neuronIndex = 0 ;neuronIndex < neuronAmount;neuronIndex++ ){
switch (neuronIndex){
case 0:
//get current sigma
sigma = neuronArray[neuronIndex].calculateSigma(0.15);
printf("Validating currentNeuronSigma %f \n", sigma);
assert( sigma > 0.14 && sigma < 0.16);
break;
case 1:
sigma = neuronArray[neuronIndex].calculateSigma(0.708);
printf("Validating currentNeuronSigma %f \n", sigma);
assert( sigma > 0.13 && sigma < 0.15);
break;
case 2:
sigma = neuronArray[neuronIndex].calculateSigma(0.908);
printf("Validating currentNeuronSigma %f \n", sigma);
assert( sigma > 0.02 && sigma < 0.05);
break;
default:
break;
}
}
delete [] inputs;
delete [] weights;
delete [] synapses;
delete [] neuronArray; //invoke the destructor !!
return 0;
}