-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathNeuralNetwork.cpp
More file actions
131 lines (118 loc) · 3.73 KB
/
Copy pathNeuralNetwork.cpp
File metadata and controls
131 lines (118 loc) · 3.73 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
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
#include <fstream>
#include <iostream>
#include "NeuralNetwork.h"
Layer::Layer()
{
}
Layer::Layer(int last_layer_dim, int dim, string act, Matrix<Interval> w, Matrix<Interval> b)
{
neuron_number_last_layer = last_layer_dim;
neuron_number_this_layer = dim;
activation = act;
weight = w;
bias = b;
}
NeuralNetwork::NeuralNetwork()
{
}
NeuralNetwork::NeuralNetwork(string filename)
{
std::ifstream input(filename);
std::string line;
// Parse the structure of neural networks
if (getline(input, line))
{
}
else
{
cout << "failed to read file" << endl;
}
try
{
num_of_inputs = stoi(line);
}
catch (std::invalid_argument &e)
{
cout << "Problem during string/integer conversion!" << endl;
cout << line << endl;
}
getline(input, line);
num_of_outputs = stoi(line);
getline(input, line);
num_of_hidden_layers = stoi(line);
// cout << "num_of_inputs" << num_of_inputs << ", " << num_of_outputs << ", " << num_of_hidden_layers;
std::vector<int> network_structure(num_of_hidden_layers + 1, 0);
for (int idx = 0; idx < num_of_hidden_layers; idx++)
{
getline(input, line);
network_structure[idx] = stoi(line);
}
network_structure[network_structure.size() - 1] = num_of_outputs;
// parse the activation function
std::vector<std::string> activation;
for (int idx = 0; idx < num_of_hidden_layers + 1; idx++)
{
getline(input, line);
activation.push_back(line);
}
// Parse the input text file and store weights and bias
// a question here. need to confirm on 10/20/2020 afternoon.
// compute parameters of the input layer
double value;
Interval I(0.0, 0.0);
Matrix<Interval> weight0(network_structure[0], num_of_inputs);
Matrix<Interval> bias0(network_structure[0], 1);
for (int i = 0; i < network_structure[0]; i++)
{
for (int j = 0; j < num_of_inputs; j++)
{
getline(input, line);
value = stod(line);
I.set(value, value);
weight0[i][j] = I;
}
getline(input, line);
value = stod(line);
I.set(value);
bias0[i][0] = I;
}
Layer input_layer(num_of_inputs, network_structure[0], activation[0], weight0, bias0);
// cout << "weight0: " << weight0 << endl;
// cout << "bias0: " << bias0 << endl;
layers.push_back(input_layer);
// compute the parameters of hidden layers
for (int layer_idx = 0; layer_idx < num_of_hidden_layers; layer_idx++)
{
Matrix<Interval> weight(network_structure[layer_idx + 1], network_structure[layer_idx]);
Matrix<Interval> bias(network_structure[layer_idx + 1], 1);
for (int i = 0; i < network_structure[layer_idx + 1]; i++)
{
for (int j = 0; j < network_structure[layer_idx]; j++)
{
getline(input, line);
value = stod(line);
I.set(value);
weight[i][j] = I;
}
getline(input, line);
value = stod(line);
I.set(value);
bias[i][0] = I;
}
// cout << "weight_" + to_string(layer_idx + 1) + ":" << weight << endl;
// cout << "bias_" + to_string(layer_idx + 1) + ":" << bias << endl;
Layer hidden_layer(network_structure[layer_idx], network_structure[layer_idx + 1], activation[layer_idx + 1], weight, bias);
layers.push_back(hidden_layer);
}
// Affine mapping of the output
getline(input, line);
value = stod(line);
// cout << value << endl;
I.set(value);
offset = I;
getline(input, line);
value = stod(line);
// cout << value << endl;
I.set(value);
scale_factor = I;
}