-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNN.cpp
More file actions
71 lines (52 loc) · 1.76 KB
/
Copy pathNN.cpp
File metadata and controls
71 lines (52 loc) · 1.76 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
#include "NN.h"
NN::NN(const int a, const int b, const int c):
NumOfInputNodes(a), NumOfHiddenNodes(b), NumOfOutputNodes(c), weightsIH(new Matrix(NumOfHiddenNodes, NumOfInputNodes)),
weightsHO(new Matrix(NumOfOutputNodes, NumOfHiddenNodes)), biasH(new Matrix(NumOfHiddenNodes, 1)), biasO(new Matrix(NumOfOutputNodes, 1))
{
weightsIH->randomize();
weightsHO->randomize();
biasH->randomize();
biasO->randomize();
}
std::vector<double> NN::predict(const std::vector<double>& input)
{
Matrix inputs = Matrix::fromArray(input);
Matrix hidden = *weightsIH * inputs;
hidden += *biasH;
hidden.map(d_sigmoid);
Matrix output = *weightsHO * hidden;
output += *biasO;
output.map(d_sigmoid);
return output.toArray();
}
void NN::train(const std::vector<double>& input, const std::vector<double>& target)
{
Matrix inputs = Matrix::fromArray(input);
Matrix hidden = *weightsIH * inputs;
hidden += *biasH;
hidden.map(d_sigmoid);
Matrix output = *weightsHO * hidden;
output += *biasO;
output.map(d_sigmoid);
Matrix targets = Matrix::fromArray(target);
Matrix outputErrors = targets - output;
Matrix gradients = Matrix::map(output, d_dsigmoid);
gradients.hadamard(outputErrors);
gradients * 0.1;
Matrix hiddenTranpose = Matrix::transpose(hidden);
Matrix weightsHoDeltas = gradients * hiddenTranpose;
*weightsHO += weightsHoDeltas;
*biasO += gradients;
Matrix weightTranspose = Matrix::transpose(*weightsHO);
Matrix hiddenErrors = weightTranspose * outputErrors;
Matrix hiddenGradient = Matrix::map(hidden, d_dsigmoid);
hiddenGradient.hadamard(hiddenErrors);
hiddenGradient * 0.1;
Matrix inputTranspose = Matrix::transpose(inputs);
Matrix weightIHDeltas = hiddenGradient * inputTranspose;
*weightsIH += weightIHDeltas;
*biasH += hiddenGradient;
}
NN::~NN()
{
}