forked from taskflow/taskflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseq.cpp
More file actions
47 lines (39 loc) · 1.1 KB
/
Copy pathseq.cpp
File metadata and controls
47 lines (39 loc) · 1.1 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
#include "dnn.hpp"
void run_sequential(MNIST& D, unsigned num_threads) {
const auto iter_num = D.images.rows()/D.batch_size;
for(auto e=0u; e<D.epoch; e++) {
for(auto it=0u; it<iter_num; it++) {
// Foward propagation
for(size_t i=0; i<D.acts.size(); i++) {
if(i == 0){
D.forward(i, D.images.middleRows(D.beg_row, D.batch_size));
}
else {
D.forward(i, D.Ys[i-1]);
}
}
// Calculate loss
D.loss(D.labels);
// Backward propagation
for(int i=D.acts.size()-1; i>=0; i--) {
if(i > 0) {
D.backward(i, D.Ys[i-1].transpose());
}
else {
D.backward(i, D.images.middleRows(D.beg_row, D.batch_size).transpose());
}
}
// Update parameters
for(int i=D.acts.size()-1; i>=0; i--) {
D.update(i);
}
// Get next batch
D.beg_row += D.batch_size;
if(D.beg_row >= D.images.rows()) {
D.beg_row = 0;
}
} // End of iterations
// Shuffle input
D.shuffle(D.images, D.labels, D.images.rows());
} // End of epoch
}