-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetwork.cpp
182 lines (148 loc) · 4.84 KB
/
Network.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
#include "Network.h"
#include "NN.cpp"
#include <vector>
Network::Network(const vector <unsigned> &topology)
{
srand((unsigned int)time(NULL));
unsigned numLayers = topology.size();
for (unsigned layerNum = 0; layerNum < numLayers; layerNum++) {
m_layers.push_back(Layer());
unsigned numOutputs = layerNum == topology.size() - 1 ? 0 : topology[layerNum + 1];
// We have a new layer, now fill it with neurons, and a bias neuron in each layer.
for (unsigned neuronNum = 0; neuronNum < topology[layerNum]; neuronNum++) {
m_layers.back().push_back(Neuron(numOutputs, neuronNum));
}
}
//for stone and bray sfa
for(int i=0;i<topology.back();i++)
{
NormalizeWeights(i);
}
}
void Network::NormalizeWeights(int connection_index)
{
double sum_weights_squared = 0.0f;
double checksum = 0.0f;
for (unsigned layerNum = 1; layerNum < m_layers.size(); layerNum++) {
Layer& layer = m_layers[layerNum];
Layer& prevLayer = m_layers[layerNum - 1];
for (unsigned n = 0; n < prevLayer.size(); n++) {
Neuron* neuron = &(prevLayer[n]);
sum_weights_squared = sum_weights_squared +neuron->m_outputWeights[connection_index].weight;// pow(neuron->m_outputWeights[0].weight,2);
}
}
double average = sum_weights_squared/101.0f;
sum_weights_squared = 0.0f;
for (unsigned layerNum = 1; layerNum < m_layers.size(); layerNum++) {
Layer& layer = m_layers[layerNum];
Layer& prevLayer = m_layers[layerNum - 1];
for (unsigned n = 0; n < prevLayer.size(); n++) {
Neuron* neuron = &(prevLayer[n]);
neuron->m_outputWeights[connection_index].weight -= average;
sum_weights_squared += pow(neuron->m_outputWeights[connection_index].weight,2);
// pow(neuron->m_outputWeights[0].weight,2);
}
}
for (unsigned layerNum = 1; layerNum < m_layers.size(); layerNum++) {
Layer& layer = m_layers[layerNum];
Layer& prevLayer = m_layers[layerNum - 1];
for (unsigned n = 0; n < prevLayer.size(); n++) {
Neuron* neuron = &(prevLayer[n]);
double weight_squared = pow(neuron->m_outputWeights[connection_index].weight,2);
double newWeight = neuron->m_outputWeights[connection_index].weight/sqrt(sum_weights_squared);
neuron->m_outputWeights[connection_index].weight = newWeight;
// cout<<newWeight<<endl;
// cout<<neuron->m_outputWeights[0].weight<<endl;
checksum+= pow(newWeight,2);
}
}
cout<<checksum<<endl;
double checksumfactor = 1.0f/checksum;
checksum = 0.0f;
/*
for (unsigned layerNum = 1; layerNum < m_layers.size(); layerNum++) {
Layer& layer = m_layers[layerNum];
Layer& prevLayer = m_layers[layerNum - 1];
for (unsigned n = 0; n < prevLayer.size(); n++) {
Neuron* neuron = &(prevLayer[n]);
double weight_squared = pow(neuron->m_outputWeights[0].weight,2);
double newWeight =(neuron->m_outputWeights[0].weight*sqrt(checksumfactor));// sqrt(1.0f-sum_weights_squared + weight_squared);
neuron->m_outputWeights[0].weight = newWeight;
// cout<<newWeight<<endl;
// cout<<neuron->m_outputWeights[0].weight<<endl;
checksum+= pow(newWeight,2);
}
double checksumfactor = 1.0f/ checksum;
cout<<checksum<<endl;
}
*/
}
void Network::UpdateWeights()
{
for (unsigned layerNum = 1; layerNum < m_layers.size(); layerNum++) {
Layer& layer = m_layers[layerNum];
Layer& prevLayer = m_layers[layerNum - 1];
for (unsigned n = 0; n < prevLayer.size(); n++) {
Neuron* neuron = &(prevLayer[n]);
neuron->NaivelyUpdateWeights();
}
}
}
void Network::feedForward(vector <double> &inputVals)
{
assert(inputVals.size() == m_layers[0].size());
// Assign (latch) the input values into the input neurons
for (unsigned i = 0; i < inputVals.size(); i++) {
m_layers[0][i].setOutputVal(inputVals[i]);
}
// forward propagate
for (unsigned layerNum = 1; layerNum < m_layers.size(); ++layerNum) {
Layer &prevLayer = m_layers[layerNum - 1];
for (unsigned n = 0; n < m_layers[layerNum].size(); n++) {
m_layers[layerNum][n].feedForward(prevLayer);
}
}
}
void Network::getResults(vector <double> &resultVals)
{
resultVals.clear();
for (unsigned n = 0; n < m_layers.back().size() ; n++) {
resultVals.push_back(m_layers.back()[n].getOutputVal());
}
}
vector<double> Network::GetWeights() const
{
//this will hold the weights
vector<double> weights;
//for each layer
for (int i = 0; i<m_layers.size()-1; ++i)
{
//for each neuron
for (int j = 0; j<m_layers[i].size(); ++j)
{
//for each weight
for (int k = 0; k<m_layers[i][j].m_outputWeights.size(); ++k)
{
weights.push_back(m_layers[i][j].m_outputWeights[k].weight);
}
}
}
return weights;
}
void Network::PutWeights(vector<double> &weights)
{
int cWeight = 0;
//for each layer
for (int i = 0; i<m_layers.size()-1; ++i)
{
//for each neuron
for (int j = 0; j<m_layers[i].size(); ++j)
{
//for each weight
for (int k = 0; k<m_layers[i][j].m_outputWeights.size(); ++k)
{
m_layers[i][j].m_outputWeights[k].weight = weights[cWeight++];
}
}
}
}