-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot.cpp
More file actions
122 lines (108 loc) · 4.45 KB
/
plot.cpp
File metadata and controls
122 lines (108 loc) · 4.45 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
#include <vector>
#include "matplotlibcpp.h"
#include "model.cpp"
using namespace std;
namespace plt = matplotlibcpp;
/*
Usage:
This C++ code snippet demonstrates loading a dataset, training an LSTM model, visualizing training metrics, and making predictions.
Key functions include `convert_json_to_weights` for reading model weights from a JSON file, `txt_to_metrics` for reading training metrics from a text file, and `plot_training` for visualizing loss and accuracy with Matplotlib.
The `main` function initializes the dataset and model, loads pre-trained weights, visualizes training metrics, and performs predictions on a test dataset.
Paths for the dataset, weights, and metrics should be specified by replacing placeholders in the code.
*/
unordered_map<string, Tensor2f> convert_json_to_weights(const string& path) {
unordered_map<string, Tensor2f> weights;
ifstream file(path);
string line;
string key;
vector<vector<float>> values;
auto process_line = [](string line) -> vector<float>{
if(line.back() == ','){
line.pop_back();
}
string trimmed_line = line.substr(1, line.length() - 1);
stringstream ss(trimmed_line);
float value;
char comma;
vector<float> values;
while (ss >> value) {
values.push_back(value);
if (!(ss >> comma)){
break;
}
}
return values;
};
while(getline(file , line)){
if(line.length() == 2 || line == "]"){ // Closing bracket condition "]," or "]"
Tensor2f tensor(values.size(), values[0].size());
for (int i = 0; i < values.size(); ++i) {
for (int j = 0; j < values[i].size(); ++j) {
tensor(i, j) = values[i][j];
}
}
values.clear();
weights[key] = tensor;
}else if(line.length() == 6){ // Key condition
key = line.substr(1 , 2);
}else if(line.length() > 6){ // Values condition
vector<float> row = process_line(line);
values.push_back(process_line(line));
}else{ // Json end condition
continue;
}
}
return weights;
}
pair<vector<double>, vector<double>> txt_to_metrics(string path) {
ifstream File(path);
string line;
vector<double> loss;
vector<double> accuracy;
while (getline(File, line)) {
stringstream ss(line);
double loss_value, accuracy_value;
ss >> loss_value >> accuracy_value;
loss.push_back(loss_value);
accuracy.push_back(accuracy_value);
}
return {loss, accuracy};
}
void plot_training(vector<double> loss , vector<double> accuracy){
plt::figure();
plt::plot(loss, {{"label" , "Loss"}});
plt::plot(accuracy , {{"label" , "Accuracy"}});
plt::ylim(0 , 2);
plt::legend();
plt::title("Performance");
plt::xlabel("Iterations");
plt::save("performance.png");
plt::show();
}
int main() {
string dataset_path = ""; // Provide the dataset path
Dataset* processor = new Dataset(dataset_path);
Tensor3f dataset = processor->preprocess();
LSTM model = LSTM();
string weights_path = ""; // Provide path to weights.json file
unordered_map<string , Tensor2f> weights = convert_json_to_weights(weights_path);
model.set_weights(weights);
string metrics_path = ""; // Provide path to metrics.txt file
auto training_metrics = txt_to_metrics(metrics_path);
plot_training(training_metrics.first , training_metrics.second);
// Testing over 2000 instances
Eigen :: array<Eigen :: Index , 3> start = {12001 , 0 , 0};
Eigen :: array<Eigen :: Index , 3> extent = {1999 , dataset.dimension(1) , dataset.dimension(2)};
Tensor3f batch = dataset.slice(start , extent);
cout << "Prediction... "<< endl;
vector<Tensor2f> temp = model.predict(batch); // Each row is a timestep
Tensor3f prediction(temp[0].dimension(0), temp.size(), temp[0].dimension(1)); prediction.setZero();
for(int i = 0; i < temp.size(); i++){
prediction.chip(i, 1) = temp[i];
}
vector<string> originals = processor->convert_data_to_string(batch);
vector<string> names = processor->convert_data_to_string(prediction);
auto metrics = model.loss_acc_calculation(temp , batch);
cout << "Test Accuracy : " << metrics.second << endl;
delete processor;
}