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
90 lines (72 loc) · 2.02 KB
/
seq.cpp
File metadata and controls
90 lines (72 loc) · 2.02 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
#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());
D.backward(i, D.Ys[i-1]);
}
else {
//D.backward(i, D.images.middleRows(D.beg_row, D.batch_size).transpose());
D.backward(i, D.images.middleRows(D.beg_row, D.batch_size));
}
}
// 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
D.validate();
// Shuffle input
D.shuffle(D.images, D.labels, D.images.rows());
} // End of epoch
}
*/
void run_sequential(unsigned num_epochs, unsigned num_threads) {
MNIST_DNN D;
init_dnn(D, rand_rate());
for(auto e=0u; e<100; e++) {
for(auto it=0u; it<NUM_ITERATIONS; it++) {
// Foward propagation
forward_task(D, IMAGES, LABELS);
// Calculate loss
D.loss(LABELS);
// Backward propagation
for(int i=D.acts.size()-1; i>=0; i--) {
backward_task(D, i, IMAGES);
}
// 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 >= IMAGES.rows()) {
D.beg_row = 0;
}
} // End of iterations
// Shuffle input
shuffle(IMAGES, LABELS);
D.validate(TEST_IMAGES, TEST_LABELS);
} // End of epoch
}