-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFindBestLearningRate.cpp
110 lines (92 loc) · 4.26 KB
/
FindBestLearningRate.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
#include <iostream>
#include <ctime>
#include <fstream>
#include <sstream>
#include <cassert>
#include <iomanip>
#include <algorithm>
#include "NeuralNet.h"
int main() {
//------------------------------------------------------------------------------------------------------------------
// Modify net characteristics below - number of nets to create, the starting learning alpha, learning rate increment
//------------------------------------------------------------------------------------------------------------------
int networks_to_create = 5, ntc = networks_to_create;
double test_learning_rate = .125, tlr = test_learning_rate;
double lr_increment = .005;
std::vector<double> holding_vector;
int numRuns = 0;
//------------------------------------------------------------------------------------------------------------------
while (ntc > 0) {
const int num_input_nodes = 64;
const int num_output_nodes = 10;
double learning_rate = tlr;
std::map<unsigned, unsigned> hidden_map; // hidden layer map. key = layer num, value = nodes per layer
hidden_map.insert(std::pair<int, int>(0, 70)); /**< Add more layers if desired **/
// create neural network
NeuralNet red(num_input_nodes, num_output_nodes, learning_rate, hidden_map, false); /**< Default param false. true for bias node option **/
// informative print
std::cout << "Input node count = " << num_input_nodes
<< " | Output node count = " << num_output_nodes
<< " | Learning rate = " << learning_rate
<< " | Bias active? " << ((red.biasIsActive()) ? "true" : "false")
<< " \n";
// output identity
std::map<unsigned, double> output_map;
for (int i = 0; i < num_output_nodes; i++)
output_map.insert(std::pair<unsigned, double>(i, i));
red.set_output_identity(output_map);
// training
double label_1;
std::vector<double> data_vector; std::string line; double i;
std::ifstream training_file("../optdigits_train.txt");
while (std::getline(training_file, line)) {
std::istringstream iss(line);
while (iss >> i) {
data_vector.push_back(i);
if (iss.peek() == ',') iss.ignore();
}
label_1 = data_vector.back();
data_vector.pop_back();
red.insert_data(data_vector);
red.forward_propagate();
red.back_propagate(label_1);
red.clear_network();
data_vector.clear();
}
training_file.close();
std::cout << "WEIGHTS TRAINED\n";
int total_lines = 1797; int correct = 0; std::string line_test; double label_2, u;
// testing
std::ifstream test_file("../optdigits_test.txt");
assert(test_file.is_open());
while (std::getline(test_file, line_test)) {
std::istringstream iss2(line_test);
while (iss2 >> u) {
data_vector.push_back(u);
if (iss2.peek() == ',') iss2.ignore();
}
label_2 = data_vector.back();
data_vector.pop_back();
red.insert_data(data_vector);
red.forward_propagate();
if (red.choose_answer(label_2, false)) /**< Uses default param = false. true to print network's beliefs **/
correct++;
red.clear_network();
data_vector.clear();
}
double accuracy = ((double) correct / total_lines) * 100;
std::cout << "Correct: " << correct << " out of " << total_lines << '\n';
std::printf("Accuracy: %.2f%%\n", accuracy);
std::cout << "Run #" << numRuns++ << "\n\n";
holding_vector.push_back(accuracy);
tlr += lr_increment;
ntc--;
}
auto macc_it = std::max_element(holding_vector.begin(), holding_vector.end());
int run_of_macc = (int) (macc_it - holding_vector.begin());
double lr_of_macc = test_learning_rate + (lr_increment * run_of_macc);
std::cout << "Max accuracy: " << *macc_it << " %\n";
std::cout << "Learning rate when max acc found: " << lr_of_macc << '\n';
std::cout << "Check run: " << run_of_macc << std::endl;
return 0;
}