-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArma_demo.cpp
228 lines (185 loc) · 7.04 KB
/
Arma_demo.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
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
#include <iostream>
#include <armadillo>
#include <cmath>
arma::mat relu(arma::mat input) {
arma::mat out = input;
arma::mat::iterator it_end = input.end();
for (arma::mat::iterator it = input.begin(); it != it_end; ++it) {
if (*it < 0) {
*it = 0;
}
}
return out;
}
arma::mat relu_d(arma::mat input) {
arma::mat out = input;
arma::mat::iterator it_end = input.end();
for (arma::mat::iterator it = input.begin(); it != it_end; ++it) {
if (*it < 0) {
*it = 0;
}
else {
*it = 1;
}
}
return out;
}
arma::mat sigmoid(arma::mat input) {
arma::mat out = input;
arma::mat::iterator it_end = input.end();
for (arma::mat::iterator it = input.begin(); it != it_end; ++it) {
*it = 1 / (1 + exp(-1 * (*it)));
}
return out;
}
arma::mat sigmoid_d(arma::mat input) {
arma::mat out = input;
arma::mat::iterator it_end = input.end();
for (arma::mat::iterator it = input.begin(); it != it_end; ++it) {
*it = (1 / (1 + exp(-1 * (*it)))) * (1 - (1 / (1 + exp(-1 * (*it)))));
}
return out;
}
arma::mat softmax(arma::mat input) {
arma::mat out = input;
double sumExp = 0;
arma::mat::iterator it_end = input.end();
for (arma::mat::iterator it = input.begin(); it != it_end; ++it) {
sumExp += exp(*it);
}
for (arma::mat::iterator it = input.begin(); it != it_end; ++it) {
*it = *it / sumExp;
}
return out;
}
arma::mat softmax_d(arma::mat input) {
arma::mat out = input;
double sumExp = 0;
arma::mat::iterator it_end = out.end();
for (arma::mat::iterator it = out.begin(); it != it_end; ++it) {
sumExp += exp(*it);
}
double sum;
for (arma::mat::iterator it = out.begin(); it != it_end; ++it) {
arma::mat::iterator it2_end = input.end();
sum = 0;
for (arma::mat::iterator it2 = input.begin(); it2 != it2_end; ++it2) {
if (*it2 != *it) {
sum += *it2;
}
}
*it = (*it * sum) / pow(sumExp, 2);
}
return out;
}
arma::mat quadraticLoss(arma::mat out, arma::mat goal) {
arma::mat cost = out;
arma::mat::iterator it_end = cost.end();
arma::mat::iterator it1 = out.begin();
arma::mat::iterator it2 = goal.begin();
for (arma::mat::iterator it = cost.begin(); it != it_end; ++it) {
*it = pow((*it2 - *it1), 2);
++it1;
++it2;
}
return cost;
}
arma::mat quadraticLoss_d(arma::mat out, arma::mat goal) {
arma::mat derivatif = out;
arma::mat::iterator it_end = derivatif.end();
arma::mat::iterator it1 = out.begin();
arma::mat::iterator it2 = goal.begin();
for (arma::mat::iterator it = derivatif.begin(); it != it_end; ++it) {
*it = 0.5 * (*it2 - *it1);
++it1;
++it2;
}
return derivatif;
}
arma::mat calculateLayer(arma::mat input, arma::mat W, arma::mat bias) {
return ((input * W) + bias);
}
void backProp(arma::mat error, arma::mat *W, char sel) {
}
int main()
{
arma::arma_rng::set_seed_random();
double error = 0;
double learn_rate = 0.3;
long long int iteration = 0;
arma::mat inp(1, 7, arma::fill::randu);
arma::mat l1(1, 6, arma::fill::zeros);
arma::mat l2(1, 3, arma::fill::zeros);
arma::mat l3(1, 2, arma::fill::zeros);
arma::mat out(1, 3, arma::fill::zeros);
arma::mat goal(arma::size(out), arma::fill::randu);
arma::mat loss(arma::size(out), arma::fill::zeros);
arma::mat w1(inp.n_cols, l1.n_cols, arma::fill::randu);
arma::mat w2(l1.n_cols, l2.n_cols, arma::fill::randu);
arma::mat w3(l2.n_cols, l3.n_cols, arma::fill::randu);
arma::mat w4(l3.n_cols, out.n_cols, arma::fill::randu);
arma::mat bias_l1(arma::size(l1), arma::fill::ones);
arma::mat bias_l2(arma::size(l2), arma::fill::ones);
arma::mat bias_l3(arma::size(l3), arma::fill::ones);
arma::mat bias_out(arma::size(out), arma::fill::ones);
std::cout << "input :" << std::endl << inp << std::endl;
std::cout << "layer 1 :" << std::endl << relu(l1) << std::endl;
std::cout << "layer 2 :" << std::endl << sigmoid(l2) << std::endl;
std::cout << "layer 3 :" << std::endl << relu(l3) << std::endl;
std::cout << "output :" << std::endl << softmax(out) << std::endl;
std::cout << std::endl;
std::cout << "goal :" << std::endl << goal << std::endl;
std::cout << std::endl;
std::cout << "weight 1 :" << std::endl << w1 << std::endl;
std::cout << "weight 2 :" << std::endl << w2 << std::endl;
std::cout << "weight 3 :" << std::endl << w3 << std::endl;
std::cout << "weight 4 :" << std::endl << w3 << std::endl;
std::cout << "bias layer 1 :" << std::endl << bias_l1 << std::endl;
std::cout << "bias layer 2 :" << std::endl << bias_l2 << std::endl;
std::cout << "bias layer 3 :" << std::endl << bias_l3 << std::endl;
std::cout << "bias layer output :" << std::endl << bias_out << std::endl;
system("pause");
do {
//forward propagation
l1 = calculateLayer(inp, w1, bias_l1);
l2 = calculateLayer(relu(l1), w2, bias_l2);
l3 = calculateLayer(sigmoid(l2), w3, bias_l3);
out = calculateLayer(relu(l3), w4, bias_out);
loss = quadraticLoss(softmax(out), goal);
arma::mat::iterator it_end = loss.end();
for (arma::mat::iterator it = loss.begin(); it != it_end; ++it) {
error += abs(*it);
}
error = error / loss.n_elem;
std::cout << "Error : " << error << std::endl;
//back propagation
w4 += trans(relu(l3)) * (quadraticLoss_d(softmax(out), goal) % softmax_d(out)) * learn_rate;
bias_out += quadraticLoss_d(softmax(out), goal) % softmax_d(out) * learn_rate;
w3 += (trans(sigmoid(l2)) * relu_d(l3)) * accu(quadraticLoss_d(softmax(out), goal) % softmax_d(out)) * learn_rate;
bias_l3 += relu_d(l3) * accu(quadraticLoss_d(softmax(out), goal) % softmax_d(out)) * learn_rate;
w2 += (trans(relu(l1)) * sigmoid_d(l2)) * accu(trans(relu_d(l3)) * (quadraticLoss_d(softmax(out), goal) % softmax_d(out))) * learn_rate;
bias_l2 += sigmoid_d(l2) * accu(trans(relu_d(l3)) * (quadraticLoss_d(softmax(out), goal) % softmax_d(out))) * learn_rate;
w1 += (trans(inp) * relu_d(l1)) * accu(trans(sum(trans(relu_d(l3)) * (quadraticLoss_d(softmax(out), goal) % softmax_d(out)))) * sigmoid_d(l2)) * learn_rate;
bias_l1 += relu_d(l1) * accu(trans(sum(trans(relu_d(l3)) * (quadraticLoss_d(softmax(out), goal) % softmax_d(out)))) * sigmoid_d(l2)) * learn_rate;
iteration++;
} while (error > 0.0001 / out.n_elem);
std::cout << "input :" << std::endl << inp << std::endl;
std::cout << "layer 1 :" << std::endl << relu(l1) << std::endl;
std::cout << "layer 2 :" << std::endl << sigmoid(l2) << std::endl;
std::cout << "layer 3 :" << std::endl << relu(l3) << std::endl;
std::cout << "output :" << std::endl << softmax(out) << std::endl;
std::cout << std::endl;
std::cout << "goal :" << std::endl << goal << std::endl;
std::cout << std::endl;
std::cout << "weight 1 :" << std::endl << w1 << std::endl;
std::cout << "weight 2 :" << std::endl << w2 << std::endl;
std::cout << "weight 3 :" << std::endl << w3 << std::endl;
std::cout << "weight 4 :" << std::endl << w3 << std::endl;
std::cout << "bias layer 1 :" << std::endl << bias_l1 << std::endl;
std::cout << "bias layer 2 :" << std::endl << bias_l2 << std::endl;
std::cout << "bias layer 3 :" << std::endl << bias_l3 << std::endl;
std::cout << "bias layer output :" << std::endl << bias_out << std::endl;
std::cout << "Error : " << error << std::endl;
std::cout << "Iteration : " << iteration << std::endl;
return 0;
}