-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNN.cpp
More file actions
69 lines (60 loc) · 1.95 KB
/
NN.cpp
File metadata and controls
69 lines (60 loc) · 1.95 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
#include <vector>
#include <memory>
#include <iostream>
#include "layer.cpp"
#include "losses.cpp"
class NN
{
public:
std::vector<std::unique_ptr<Layer>> layers;
// Add layers dynamically
void add(Layer *layer)
{
layers.emplace_back(layer);
}
// Make prediction using feed forward process
std::vector<double> predict(std::vector<double> input) const
{
return forward_propagation(input);
}
// Forward propagation
std::vector<double> forward_propagation(const std::vector<double> input) const
{
std::vector<double> output = input;
for (const auto &layer : layers)
{
output = layer->forward(output);
}
return output;
}
// Backward propagation
void backward_propagation(const std::vector<double> &error, double learning_rate)
{
std::vector<double> grad = error;
for (auto it = layers.rbegin(); it != layers.rend(); ++it)
{
grad = (*it)->backward(grad, learning_rate);
}
}
// Training function
void fit(const std::vector<std::vector<double>> &X, const std::vector<std::vector<double>> &y, int epochs, double learning_rate)
{
for (int epoch = 0; epoch < epochs; ++epoch)
{
double total_loss = 0.0;
for (size_t i = 0; i < X.size(); ++i)
{
// Forward pass
std::vector<double> output = forward_propagation(X[i]);
// Compute loss
double loss = BCELoss(y[i], output);
total_loss += loss;
std::vector<double> loss_derivative = BCELossDerivative(y[i], output);
// Backward pass
backward_propagation(loss_derivative, learning_rate);
}
// Print loss for monitoring
std::cout << "Epoch " << epoch + 1 << "/" << epochs << " - Loss: " << total_loss / X.size() << std::endl;
}
}
};