-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnn.cpp
108 lines (89 loc) · 2.89 KB
/
nn.cpp
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
#include "nn.h"
double Sigmoid(double x) { return 1.0/(1.0+exp(-x/10.0)); }
NeuralNetwork::NeuralNetwork() : nHidden(0), nInputs(0), nOutputs(0) {}
void NeuralNetwork::Initialize(uint32_t ni, uint32_t nh, uint32_t no)
{
//std::cout << "Initializing NN.\n";
nOutputs = no; nHidden = nh; nInputs = ni;
y = new double[nOutputs];
h = new double[nHidden];
t = new double[nHidden];
}
void NeuralNetwork::InitializeWeights(double *xh, double *hh, double *hy)
{
W_xh = xh; W_hh = hh; W_hy = hy;
for (uint32_t i = 0; i < nHidden; i++) { h[i] = 0.0; }
}
void NeuralNetwork::InitializeWeights(double *xh, double *hh, double *hy, double *mem)
{
W_xh = xh; W_hh = hh; W_hy = hy;
for (uint32_t i = 0; i < nHidden; i++) { h[i] = mem[i]; }
}
double* NeuralNetwork::ComputeOutput(double *x)
{
for (uint32_t j = 0; j < nHidden; j++) { t[j] = 0; }
for (uint32_t i = 0; i < nHidden; i++) {
for (uint32_t j = 0; j < nInputs; j++) {
t[i] += W_xh[i*nInputs+j]*x[j];
}
}
for (uint32_t i = 0; i < nHidden; i++) {
for (uint32_t j = 0; j < nHidden; j++) {
t[i] += W_hh[i*nHidden+j]*h[j];
}
}
for (uint32_t j = 0; j < nHidden; j++) { h[j] = Sigmoid(t[j]); }
for (uint32_t i = 0; i < nOutputs; i++) {
for (uint32_t j = 0; j < nHidden; j++) {
y[i] += W_hy[i*nOutputs+j]*h[j];
}
//y[i] = Sigmoid(y[i]);
}
return y;
}
RecurrentNeuralNetwork::RecurrentNeuralNetwork() : nNetworks(0) {}
RecurrentNeuralNetwork::~RecurrentNeuralNetwork()
{
std::cout << "Freeing NNs.\n";
delete[] NNs;
}
void RecurrentNeuralNetwork::Initialize(uint32_t nn, uint32_t *nl)
{
//std::cout << "Initializing RNN.\n";
nNetworks = nn; NetworkLayout = nl;
NNs = new NeuralNetwork[nNetworks];
for (uint32_t i = 0; i < nNetworks; i++) {
NNs[i].Initialize(NetworkLayout[2*i],NetworkLayout[2*i+1],NetworkLayout[2*i+2]);
}
}
void RecurrentNeuralNetwork::InitializeWeights(double *xh, double *hh, double *hy)
{
double *txh, *thh, *thy;
txh = xh; thh = hh; thy = hy;
for (uint32_t i = 0; i < nNetworks; i++) {
NNs[i].InitializeWeights(txh,thh,thy);
txh += NetworkLayout[2*i ]*NetworkLayout[2*i+1];
thh += NetworkLayout[2*i+1]*NetworkLayout[2*i+1];
thy += NetworkLayout[2*i+1]*NetworkLayout[2*i+2];
}
}
void RecurrentNeuralNetwork::InitializeWeights(double *xh, double *hh, double *hy, double *mem)
{
double *txh, *thh, *thy;
txh = xh; thh = hh; thy = hy;
for (uint32_t i = 0; i < nNetworks; i++) {
NNs[i].InitializeWeights(txh,thh,thy,mem);
txh += NetworkLayout[2*i ]*NetworkLayout[2*i+1];
thh += NetworkLayout[2*i+1]*NetworkLayout[2*i+1];
thy += NetworkLayout[2*i+1]*NetworkLayout[2*i+2];
mem += NetworkLayout[2*i+1];
}
}
double * RecurrentNeuralNetwork::ComputeOutput(double *x)
{
GlobalOutput = NNs[0].ComputeOutput(x);
for (uint32_t i = 1; i < nNetworks; i++) {
GlobalOutput = NNs[i].ComputeOutput(GlobalOutput);
}
return GlobalOutput;
}