-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeuralNetwork.cpp
More file actions
180 lines (142 loc) · 4.57 KB
/
NeuralNetwork.cpp
File metadata and controls
180 lines (142 loc) · 4.57 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
#include <cmath>
#include <iostream>
#include <fstream>
#include "NeuralNetwork.h"
const Function NeuralNetwork::activationFunctions[ActivationFunction::NUM_FUNCTIONS] = {
[](double a) { return 1 / (1 + exp(-a)); }, // sigmoid
[](double a) { return (a > 0) ? a : 0; }, // relu
[](double a) { return (a > 0) ? a : 0.01*a; } // leaky relu
};
const Function NeuralNetwork::activationDerivatives[ActivationFunction::NUM_FUNCTIONS] = {
[](double a) { // sigmoid
double activation = NeuralNetwork::activationFunctions[SIGMOID](a);
return activation * (1 - activation);
},
[](double a) { return (a > 0) ? 1.0 : 0; }, // relu
[](double a) { return (a > 0) ? 1.0 : 0.01; } // leaky relu
};
NeuralNetwork::NeuralNetwork(std::vector<int> layerSizes, ActivationFunction f) {
for (int i = 0; i < layerSizes.size() - 1; i++) {
layers.push_back(Layer(layerSizes[i], layerSizes[i + 1], f));
}
}
NeuralNetwork::NeuralNetwork(std::vector<int> layerSizes, std::vector<ActivationFunction> fs) {
for (int i = 1; i < layerSizes.size(); i++) {
layers.push_back(Layer(layerSizes[i], layerSizes[i + 1], fs[i]));
}
}
NeuralNetwork::NeuralNetwork(std::string filename) {
std::ifstream infile;
infile.open(filename);
if (!infile.is_open()) {
std::cerr << "Unable to open file: " << filename << std::endl;
return;
}
json data = json::parse(infile);
epoch = data["epoch"];
for (auto& l : data["layers"]) {
layers.push_back(Layer(l));
}
infile.close();
}
void NeuralNetwork::learn(Data data, double learnRate) {
// TODO: separate data into batches; add batchSize
// TODO: learn on each batch
// for now, just full batch training
learnBatch(data, learnRate);
epoch++;
}
void NeuralNetwork::learnBatch(Data batch, double learnRate) {
clearGradients(); // temp, so gradients are still there to print after the batch
// std::cerr << "cost: " << cost(batch) << std::endl;
// run through all points in the batch
for (auto& point : batch) {
updateAllGradients(point);
}
// apply gradients to each layer
// divide by batch size to average gradients in the batch
applyGradients(learnRate / batch.size());
// clear gradients for the next batch
// clearGradients();
}
unsigned int NeuralNetwork::getEpoch() { return epoch; }
void NeuralNetwork::printWeights() {
for (auto& l : layers) l.printWeights();
}
void NeuralNetwork::printBiases() {
for (auto& l : layers) l.printBiases();
}
void NeuralNetwork::printGradientsW() {
for (auto& l : layers) l.printGradientsW();
}
void NeuralNetwork::printGradientsB() {
for (auto& l : layers) l.printGradientsB();
}
void NeuralNetwork::save(std::string filename) {
std::ofstream outfile;
outfile.open(filename, std::ofstream::trunc);
if (!outfile.is_open()) {
std::cerr << "Unable to open file: " << filename << std::endl;
return;
}
json data = json::object();
data["epoch"] = epoch;
data["layers"] = json::array();
for (Layer& l : layers) {
data["layers"].push_back(l.toJSON());
}
outfile << data.dump();
outfile.close();
}
double NeuralNetwork::nodeCost(double output, double expected) {
double diff = output - expected;
return diff * diff;
}
double NeuralNetwork::nodeCostDerivative(double output, double expected) {
return 2 * (output - expected);
}
double NeuralNetwork::cost(DataPoint point) {
Vector outputs = predict(point.inputs);
double cost = 0;
for (int i = 0; i < outputs.size(); i++) {
cost += nodeCost(outputs[i], point.expectedOutputs[i]);
}
return cost;
}
double NeuralNetwork::cost(Data points) {
double totalCost = 0;
for (auto p : points) {
totalCost += cost(p);
}
return totalCost / points.size();
}
Vector NeuralNetwork::predict(Vector inputs) {
for (auto& l : layers) {
inputs = l.runLayer(inputs);
}
return inputs;
}
void NeuralNetwork::applyGradients(double learnRate) {
for (auto& l : layers) {
l.applyGradients(learnRate);
}
}
void NeuralNetwork::clearGradients() {
for (auto& l : layers) {
l.resetGradients();
}
}
void NeuralNetwork::updateAllGradients(DataPoint point) {
// run inputs through the network
predict(point.inputs);
// update gradients of output layer
Layer& outputLayer = layers[layers.size() - 1];
Vector nodeValues = outputLayer.calculateOutputLayerNodeValues(point.expectedOutputs);
outputLayer.updateGradients(nodeValues);
// update gradients of hidden layers
for (int i = layers.size() - 2; i >= 0; i--) {
Layer& hiddenLayer = layers[i];
nodeValues = hiddenLayer.calculateHiddenLayerNodeValues(layers[i + 1], nodeValues);
hiddenLayer.updateGradients(nodeValues);
}
}