-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
157 lines (128 loc) · 3.48 KB
/
main.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
#include "NeuralNetwork.cpp"
#include "mnist/mnist_reader.hpp"
#include "mnist/mnist_utils.hpp"
#include <cstdio>
const int BUCKET_SIZE = 1;
const int NUM_RETRAIN = 3;
//const bool VERBOSE = false;
template <class T>
void print_vec(vector<T> v)
{
for (auto const& c : v)
{
printf("%.4f ", (float)c);
//cout << (float) c << " ";
}
cout << endl;
}
template <class T>
int max_index(vector<T> v)
{
T record = (T) 0;
int index = 0;
for(int i = 0; i < v.size(); i++)
{
if(v[i] > record)
{
record = v[i];
index = i;
}
}
return index;
}
float soft_plus(float n)
{
return log(1+exp(n));
}
// Also includes inverse softplus first
float d_soft_plus(float n)
{
return 1-exp(-n);
}
void use_nn(float lr)
{
// Create network with appropriete input and output nodes
vector<int> nodes = {784, 20, 10};
NeuralNetwork brain(nodes, lr, soft_plus, d_soft_plus);
// Load MNIST data
auto dataset = mnist::read_dataset<vector, vector, uint8_t, uint8_t>("mnist/");
normalize_dataset(dataset);
// Charge form of output data from 3 -> {0, 0, 0, 1, 0, 0, 0, 0, 0, 0}
vector<vector<float>> target_data;
for (auto const& label : dataset.training_labels)
{
vector<float> target;
target.resize(10, 0);
target[label] = 1.0;
target_data.push_back(target);
}
// Cast images from unsigned char to float for 2D vector
vector<vector<float>> train_images;
train_images.reserve(dataset.training_images.size());
for (auto&& v : dataset.training_images) train_images.emplace_back(v.begin(), v.end());
// Train over data numerous times
for(int k = 0; k < NUM_RETRAIN; k++)
{
// Display rough estimate of how many loops through data set left
cout << NUM_RETRAIN-k << endl;
for(int i = 0; i < train_images.size(); i++)
{
// Train nn
brain.train(train_images[i], target_data[i]);
}
// Break data into bucket sizes and train on the bucketed data
// for(int i = 0; i < train_images.size(); i+=BUCKET_SIZE)
// {
// // Break image data into buckets
// auto image_start = train_images.begin()+i;
// vector<vector<float>> image_bucket(image_start, image_start+BUCKET_SIZE);
// // Break label data into buckets
// auto target_start = target_data.begin()+i;
// vector<vector<float>> target_bucket(target_start, target_start+BUCKET_SIZE);
// // Train nn
// brain.train(image_bucket, target_bucket);
// }
}
float wrong = 0;
// Convert test data to float
vector<vector<float>> test_images;
test_images.reserve(dataset.test_images.size());
for (auto&& v : dataset.test_images) test_images.emplace_back(v.begin(), v.end());
// Test nn on test data
//cout << "Testing..." << endl;
for(int i = 0; i < dataset.test_labels.size(); i++)
{
// Get a prediction
auto guess = brain.predict(test_images[i]);
// Test if incorrect
if(dataset.test_labels[i] != max_index(guess))
{
wrong++;
}
// if(i < 30)
// {
// cout << (int) dataset.test_labels[i];
// if(dataset.test_labels[i] != max_index(guess))
// {
// cout << " | ";
// }
// else
// {
// cout << " $$ | ";
// }
// print_vec(guess);
// }
}
// Output results
cout << endl << "Learning rate: " << lr << endl;
cout << "Total Incorrect: " << wrong << endl;
cout << "\% correct: " << 100*((float)dataset.test_labels.size()-wrong)/(float)dataset.test_labels.size() << "%" << endl << endl;
}
int main(int argc, char** argv)
{
// float rates[12] = {0.001, 0.003, 0.005, 0.008, 0.01, 0.05, 0.8, 0.1, 0.3, 0.5, 0.8, 1};
for (int i = 0; i < 3; i++)
{
use_nn(0.004);
}
}