-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
310 lines (281 loc) · 8.1 KB
/
main.cpp
File metadata and controls
310 lines (281 loc) · 8.1 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <vector>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <queue>
using namespace std;
struct Node
{
double collector;
vector<Node *> connections;
vector<double> weights;
double error;
Node(vector<Node *> connections)
{
this->connections = connections;
this->collector = 0;
for (int i = 0; i < connections.size(); i++)
{
weights.push_back((double)rand() / RAND_MAX);
}
}
Node()
{
this->collector = 0;
}
};
double waveFunction(double x)
{
return (cos(x) + 1) * 0.5;
}
class NeuralNetwork
{
private:
vector<int> structure;
vector<vector<Node *>> network;
vector<vector<double>> inputs;
double sigmoid(double x)
{
return 1.0 / (1.0 + exp(-x));
}
double sigmoid_derivative(double collector)
{
return collector * (1.0 - collector);
}
// update the weights
void update_weights(double l_rate)
{
for (int i = 1; i < network.size(); i++)
{
vector<double> inputs;
for (auto neuron : network[i - 1])
{
inputs.push_back(neuron->collector);
}
for (auto neuron : network[i])
{
for (int j = 0; j < inputs.size(); j++)
{
neuron->weights[j] -= l_rate * neuron->error * inputs[j];
}
// neuron->weights.back() -= l_rate * neuron->error;
}
}
}
// feed forward the inputs
void feed_forward(vector<double> row)
{
for (int i = 0; i < structure[0]; i++)
{
network[0][i]->collector = row[i];
}
for (int i = 1; i < structure.size(); i++)
{
for (int j = 0; j < structure[i]; j++)
{
double sum = 0;
for (int k = 0; k < structure[i - 1]; k++)
{
sum += network[i - 1][k]->collector * network[i][j]->weights[k];
}
network[i][j]->collector = sigmoid(sum);
}
}
}
// back propagate the error
void back_propagate(vector<double> expected)
{
for (int i = network.size() - 1; i > 0; i--)
{
vector<Node *> layer = network[i];
vector<double> errors;
if (i == network.size() - 1)
{
for (int j = 0; j < layer.size(); j++)
{
errors.push_back(layer[j]->collector - expected[j]);
}
}
else
{
for (int j = 0; j < layer.size(); j++)
{
double error = 0.0;
for (auto node : network[i + 1])
{
error += node->weights[j] * node->error;
}
errors.push_back(error);
}
}
//
for (int j = 0; j < layer.size(); j++)
{
layer[j]->error = errors[j] * sigmoid_derivative(layer[j]->collector);
}
errors.clear(); // Clear the vector for the next iteration
}
}
public:
NeuralNetwork(string structureFile)
{
// Load structure from file into the structure vector
ifstream file(structureFile);
if (file.is_open())
{
char c;
string val;
while (file >> c)
{
if (c == ',')
{
structure.push_back(stoi(val));
val = "";
}
else
{
val += c;
}
}
structure.push_back(stoi(val));
}
file.close();
// Create network with connections to each node based on structure
for (int i = 0; i < structure.size(); i++)
{
vector<Node *> layer;
for (int j = 0; j < structure[i]; j++)
{
if (i == 0)
{
layer.push_back(new Node());
}
else
{
layer.push_back(new Node(network[i - 1]));
}
}
network.push_back(layer);
}
}
// train the neural network
void train(vector<vector<double>> inputs, int num_epochs, double target_error = 0.05, double l_rate = 0.1)
{
int num_inputs = network[0].size();
cout << "Number of inputs = " << inputs.size() << endl;
// train the network
for (int epoch = 0; epoch < num_epochs; epoch++)
{
double epoch_error = 0;
for (auto row : inputs)
{
vector<double> expected;
feed_forward(row);
for (int j = 0; j < structure[structure.size() - 1]; j++)
{
expected.push_back(row[num_inputs + j]);
}
for (int j = 0; j < structure[structure.size() - 1]; j++)
{
epoch_error += pow(network[structure.size() - 1][j]->collector - expected[j], 2);
}
back_propagate(expected);
update_weights(l_rate);
}
if (epoch_error <= target_error)
{
cout << "Target error reached error " << epoch_error << endl;
return;
}
cout << setprecision(10) << ">Epoch=" << epoch << " l_rate=" << l_rate << " error=" << setprecision(20) << epoch_error << endl;
}
}
// run with a vector of inputs and print output
void run(vector<double> inputs)
{
feed_forward(inputs);
for (int i = 0; i < structure[structure.size() - 1]; i++)
{
cout << network[structure.size() - 1][i]->collector << " ";
}
cout << endl;
}
void run_generative(vector<double> inputs, int num_generations)
{
deque<double> input_queue;
double output;
cout << fixed;
// initialize input queue
for(int i = 0; i < structure[0]; i++)
{
input_queue.push_back(inputs[i]);
}
double x = 0;
for(int i = 0; i < num_generations; i++)
{
// load input queue into nn
feed_forward({input_queue.begin(), input_queue.end()});
output = get_output();
cout << setprecision(1) << "f(" << x << "): " << setprecision(4) << output << endl;
input_queue.pop_front();
input_queue.push_back(output);
x += 0.1;
}
}
double get_output()
{
return network[structure.size() - 1][0]->collector;
}
// save the weights to a file
void save(string filename)
{
ofstream file;
file.open(filename);
for (int i = 1; i < structure.size(); i++)
{
for (int j = 0; j < structure[i]; j++)
{
for (int k = 0; k < structure[i - 1]; k++)
{
file << network[i][j]->weights[k] << " ";
}
file << endl;
}
}
file.close();
}
};
// main function
int main()
{
// seed random number generator
srand(time(NULL));
// create the neural network
NeuralNetwork nn("network.csv");
// generate inputs for the neural network incrementing by 0.1
vector<vector<double>> inputs;
for(int i = -400; i < 400; i++)
{
vector<double> input;
for(double j = 0; j <= 1; j += 0.1)
{
input.push_back(waveFunction(i+j));
}
inputs.push_back(input);
}
// train the neural network
nn.train(inputs, 100000, 0.01, 0.2);
// test the neural network with the inputs -1 through 0
vector<double> input;
for (double i = -1; i <= 0; i += 0.1)
{
input.push_back(waveFunction(i));
}
nn.run_generative(input, 200);
return 0;
}