-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
321 lines (267 loc) · 10.9 KB
/
main.cpp
File metadata and controls
321 lines (267 loc) · 10.9 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
311
312
313
314
315
316
317
318
319
320
321
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <random>
#include <stdexcept>
#include <string>
#include <vector>
using namespace std;
class Neuron {
public:
vector<double> weights;
double bias;
vector<double> input_cache;
double pre_activation;
double output;
Neuron() : bias(0.0), pre_activation(0.0), output(0.0) {}
Neuron(int input_size, mt19937& rng) : bias(0.0), pre_activation(0.0), output(0.0) {
normal_distribution<double> dist(0.0, sqrt(2.0 / static_cast<double>(max(1, input_size))));
weights.resize(input_size);
for (double& weight : weights) {
weight = dist(rng);
}
}
static double activate(double value, const string& activation) {
if (activation == "sigmoid") {
if (value >= 0.0) {
return 1.0 / (1.0 + exp(-value));
}
double exp_value = exp(value);
return exp_value / (1.0 + exp_value);
}
if (activation == "tanh") {
return tanh(value);
}
if (activation == "ReLU") {
return max(0.0, value);
}
if (activation == "linear") {
return value;
}
if (activation == "leaky_ReLU") {
return value > 0.0 ? value : 0.01 * value;
}
throw invalid_argument("Unsupported activation function: " + activation);
}
static double activation_derivative(double pre_activation_value, double output_value, const string& activation) {
if (activation == "sigmoid") {
return output_value * (1.0 - output_value);
}
if (activation == "tanh") {
return 1.0 - (output_value * output_value);
}
if (activation == "ReLU") {
return pre_activation_value > 0.0 ? 1.0 : 0.0;
}
if (activation == "linear") {
return 1.0;
}
if (activation == "leaky_ReLU") {
return pre_activation_value > 0.0 ? 1.0 : 0.01;
}
throw invalid_argument("Unsupported activation function: " + activation);
}
double forward(const vector<double>& input, const string& activation) {
if (input.size() != weights.size()) {
throw invalid_argument("Input size does not match neuron weight size.");
}
input_cache = input;
pre_activation = bias;
for (size_t index = 0; index < input.size(); ++index) {
pre_activation += weights[index] * input[index];
}
output = activate(pre_activation, activation);
return output;
}
};
class Layer {
public:
vector<Neuron> neurons;
string activation_function;
vector<double> input_vector;
vector<double> output_vector;
Layer() {}
Layer(int neuron_count, int input_size, const string& activation, mt19937& rng)
: activation_function(activation) {
neurons.reserve(neuron_count);
for (int index = 0; index < neuron_count; ++index) {
neurons.emplace_back(input_size, rng);
}
}
vector<double> forward(const vector<double>& input) {
input_vector = input;
output_vector.clear();
output_vector.reserve(neurons.size());
for (Neuron& neuron : neurons) {
output_vector.push_back(neuron.forward(input, activation_function));
}
return output_vector;
}
vector<double> backward(const vector<double>& gradient_wrt_output, double learning_rate) {
if (gradient_wrt_output.size() != neurons.size()) {
throw invalid_argument("Gradient size must match neuron count.");
}
vector<double> gradient_wrt_input(input_vector.size(), 0.0);
for (size_t neuron_index = 0; neuron_index < neurons.size(); ++neuron_index) {
Neuron& neuron = neurons[neuron_index];
double local_gradient = gradient_wrt_output[neuron_index] *
Neuron::activation_derivative(neuron.pre_activation, neuron.output, activation_function);
for (size_t weight_index = 0; weight_index < neuron.weights.size(); ++weight_index) {
double old_weight = neuron.weights[weight_index];
double gradient_wrt_weight = local_gradient * neuron.input_cache[weight_index];
neuron.weights[weight_index] -= learning_rate * gradient_wrt_weight;
gradient_wrt_input[weight_index] += local_gradient * old_weight;
}
neuron.bias -= learning_rate * local_gradient;
}
return gradient_wrt_input;
}
};
class NeuralNetwork {
public:
vector<Layer> hidden_layers;
Layer output_layer;
string loss_function;
NeuralNetwork(
int input_size,
int hidden_layer_count,
int layer_width,
int output_layer_width,
const string& hidden_layer_activation,
const string& output_activation,
const string& loss_function
) : loss_function(loss_function) {
if (input_size <= 0 || hidden_layer_count < 0 || layer_width <= 0 || output_layer_width <= 0) {
throw invalid_argument("Invalid network dimensions.");
}
random_device random_device_source;
mt19937 rng(random_device_source());
int current_input_size = input_size;
hidden_layers.reserve(hidden_layer_count);
for (int layer_index = 0; layer_index < hidden_layer_count; ++layer_index) {
hidden_layers.emplace_back(layer_width, current_input_size, hidden_layer_activation, rng);
current_input_size = layer_width;
}
output_layer = Layer(output_layer_width, current_input_size, output_activation, rng);
}
vector<double> forward_pass(const vector<double>& input_vector) {
vector<double> activations = input_vector;
for (Layer& layer : hidden_layers) {
activations = layer.forward(activations);
}
return output_layer.forward(activations);
}
double calculate_loss(const vector<double>& predictions, const vector<double>& target_vector) const {
if (predictions.size() != target_vector.size()) {
throw invalid_argument("Prediction and target sizes must match.");
}
const double epsilon = 1e-9;
double loss = 0.0;
if (loss_function == "mean_squared_error") {
for (size_t index = 0; index < target_vector.size(); ++index) {
double difference = predictions[index] - target_vector[index];
loss += difference * difference;
}
return loss / static_cast<double>(target_vector.size());
}
if (loss_function == "binary_cross_entropy") {
for (size_t index = 0; index < target_vector.size(); ++index) {
double prediction = clamp(predictions[index], epsilon, 1.0 - epsilon);
loss += -(target_vector[index] * log(prediction) + (1.0 - target_vector[index]) * log(1.0 - prediction));
}
return loss / static_cast<double>(target_vector.size());
}
throw invalid_argument("Unsupported loss function: " + loss_function);
}
vector<double> loss_gradient(const vector<double>& predictions, const vector<double>& target_vector) const {
if (predictions.size() != target_vector.size()) {
throw invalid_argument("Prediction and target sizes must match.");
}
vector<double> gradient(predictions.size(), 0.0);
const double epsilon = 1e-9;
const double output_count = static_cast<double>(predictions.size());
if (loss_function == "mean_squared_error") {
for (size_t index = 0; index < predictions.size(); ++index) {
gradient[index] = 2.0 * (predictions[index] - target_vector[index]) / output_count;
}
return gradient;
}
if (loss_function == "binary_cross_entropy") {
for (size_t index = 0; index < predictions.size(); ++index) {
double prediction = clamp(predictions[index], epsilon, 1.0 - epsilon);
gradient[index] = (prediction - target_vector[index]) / (prediction * (1.0 - prediction) * output_count);
}
return gradient;
}
throw invalid_argument("Unsupported loss function: " + loss_function);
}
void backward_pass(const vector<double>& target_vector, double learning_rate) {
vector<double> gradient = loss_gradient(output_layer.output_vector, target_vector);
gradient = output_layer.backward(gradient, learning_rate);
for (int layer_index = static_cast<int>(hidden_layers.size()) - 1; layer_index >= 0; --layer_index) {
gradient = hidden_layers[layer_index].backward(gradient, learning_rate);
}
}
void train(
const vector<vector<double>>& input_data,
const vector<vector<double>>& target_data,
int epochs,
double learning_rate,
int log_interval = 200
) {
if (input_data.empty() || input_data.size() != target_data.size()) {
throw invalid_argument("Input and target datasets must be non-empty and same size.");
}
if (epochs <= 0 || learning_rate <= 0.0) {
throw invalid_argument("Epochs and learning rate must be positive.");
}
for (int epoch = 1; epoch <= epochs; ++epoch) {
double epoch_loss = 0.0;
for (size_t sample_index = 0; sample_index < input_data.size(); ++sample_index) {
const vector<double>& sample_input = input_data[sample_index];
const vector<double>& sample_target = target_data[sample_index];
vector<double> prediction = forward_pass(sample_input);
epoch_loss += calculate_loss(prediction, sample_target);
backward_pass(sample_target, learning_rate);
}
epoch_loss /= static_cast<double>(input_data.size());
if (epoch == 1 || epoch % log_interval == 0 || epoch == epochs) {
cout << "Epoch " << epoch << " - Loss: " << fixed << setprecision(6) << epoch_loss << endl;
}
}
}
vector<double> predict(const vector<double>& input) {
return forward_pass(input);
}
};
int main() {
vector<vector<double>> xor_inputs = {
{0.0, 0.0},
{0.0, 1.0},
{1.0, 0.0},
{1.0, 1.0}
};
vector<vector<double>> xor_targets = {
{0.0},
{1.0},
{1.0},
{0.0}
};
NeuralNetwork network(
2,
2,
4,
1,
"tanh",
"sigmoid",
"binary_cross_entropy"
);
network.train(xor_inputs, xor_targets, 5000, 0.1, 500);
cout << "\nPredictions after training:" << endl;
for (const vector<double>& sample : xor_inputs) {
vector<double> prediction = network.predict(sample);
cout << sample[0] << " XOR " << sample[1] << " -> " << fixed << setprecision(6) << prediction[0] << endl;
}
return 0;
}