-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexport_results.cpp
More file actions
308 lines (256 loc) · 9.83 KB
/
export_results.cpp
File metadata and controls
308 lines (256 loc) · 9.83 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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//
// Created for exporting model results to JSON for visualization
//
#include "include/NNModel.hpp"
#include "include/TrainingPipeline.hpp"
#include "include/NNCustumDatasets.hpp"
#include "include/SimpleNN.hpp"
#include "external/third_party/doctest.hpp"
#include <fstream>
#include <sstream>
#include <iomanip>
// Function to export model predictions to JSON
void exportModelResults(const std::string& modelName,
torch::Tensor predictions,
torch::Tensor probabilities,
torch::Tensor trueLabels,
double accuracy,
double loss,
const std::string& filename = "model_results.json") {
std::ostringstream json;
json << std::fixed << std::setprecision(6);
// Start JSON object for this model
json << " \"" << modelName << "\": {\n";
// Export predictions array - handle both Float32 and Float64
json << " \"predictions\": [";
if (predictions.dtype() == torch::kFloat32) {
auto pred_data = predictions.data_ptr<float>();
for (int i = 0; i < predictions.numel(); ++i) {
json << pred_data[i];
if (i < predictions.numel() - 1) json << ", ";
}
} else
{
auto pred_data = predictions.data_ptr<double>();
for (int i = 0; i < predictions.numel(); ++i) {
json << pred_data[i];
if (i < predictions.numel() - 1) json << ", ";
}
}
json << "],\n";
// Export probabilities array - handle both Float32 and Float64
json << " \"probabilities\": [";
if (probabilities.dtype() == torch::kFloat32)
{
auto prob_data = probabilities.data_ptr<float>();
for (int i = 0; i < probabilities.numel(); ++i)
{
json << prob_data[i];
if (i < probabilities.numel() - 1) json << ", ";
}
} else {
auto prob_data = probabilities.data_ptr<double>();
for (int i = 0; i < probabilities.numel(); ++i)
{
json << prob_data[i];
if (i < probabilities.numel() - 1) json << ", ";
}
}
json << "],\n";
// Export true labels array - handle both Float32 and Float64
json << " \"true_labels\": [";
if (trueLabels.dtype() == torch::kFloat32) {
auto label_data = trueLabels.data_ptr<float>();
for (int i = 0; i < trueLabels.numel(); ++i) {
json << label_data[i];
if (i < trueLabels.numel() - 1) json << ", ";
}
} else {
auto label_data = trueLabels.data_ptr<double>();
for (int i = 0; i < trueLabels.numel(); ++i) {
json << label_data[i];
if (i < trueLabels.numel() - 1) json << ", ";
}
}
json << "],\n";
// Export metrics
json << " \"accuracy\": " << accuracy << ",\n";
json << " \"loss\": " << loss << "\n";
json << " }";
// Read existing file if it exists
std::string existing_content = "";
std::ifstream inFile(filename);
if (inFile.good()) {
std::getline(inFile, existing_content, '\0');
inFile.close();
}
// Write complete JSON
std::ofstream outFile(filename);
outFile << "{\n";
// Add existing content (except closing brace)
if (!existing_content.empty() && existing_content != "{}") {
size_t last_brace = existing_content.find_last_of('}');
if (last_brace != std::string::npos) {
outFile << existing_content.substr(1, last_brace - 1);
outFile << ",\n";
}
}
// Add new model data
outFile << json.str();
outFile << "\n}\n";
outFile.close();
std::cout << "Exported results for " << modelName << " to " << filename << std::endl;
}
// Modified PyTorch NN Model with export functionality
void runPyTorchModelWithExport()
{
std::string path = "/home/moinshaikh/CLionProjects/BreastCancerPrediction/database/data.csv";
csv::CSVFormat format;
format.delimiter(',').no_header();
TrainingPipeline::Pipeline pipeline(path, format);
pipeline.Encoding();
torch::Tensor X = torch::stack(pipeline.getFeatures(), 1);
torch::Tensor y = pipeline.getheadersTensors()["diagnosis"];
X = pipeline.Normalization(X).to(torch::kFloat32);
y = y.to(torch::kFloat32);
auto split = pipeline.splitTensors(X, y, 0.2);
auto X_train = split.X_train;
auto y_train = split.Y_train;
auto X_test = split.X_test;
auto y_test = split.Y_test;
int epochs = 100;
float learning_rate = 0.01;
NNModel model(X_train.size(1));
auto loss_fn = torch::nn::BCELoss();
auto optimizer = torch::optim::SGD(model->parameters(), learning_rate);
// Training loop
for (int epoch = 0; epoch < epochs; ++epoch) {
auto yPred = model->forward(X_train);
auto loss_val = loss_fn(yPred, y_train);
optimizer.zero_grad();
loss_val.backward();
optimizer.step();
}
// Test evaluation and export
torch::NoGradGuard no_grad;
auto test_pred = model->forward(X_test);
auto test_loss = loss_fn(test_pred, y_test);
auto predicted = (test_pred >= 0.5).to(torch::kFloat32);
auto correct = predicted.eq(y_test).sum().item<int64_t>();
double accuracy = static_cast<double>(correct) / X_test.size(0);
// Export results
exportModelResults("PyTorch_NN", predicted, test_pred, y_test,
accuracy, test_loss.item<double>());
}
// Modified Scratch NN Model with export functionality
void runScratchModelWithExport()
{
std::string path = "/home/moinshaikh/CLionProjects/BreastCancerPrediction/database/data.csv";
csv::CSVFormat format;
format.delimiter(',').no_header();
TrainingPipeline::Pipeline pipeline(path, format);
pipeline.Encoding();
torch::Tensor X = torch::stack(pipeline.getFeatures(), 1);
torch::Tensor y = pipeline.getheadersTensors()["diagnosis"];
X = pipeline.Normalization(X);
auto split = pipeline.splitTensors(X, y, 0.2);
auto X_train = split.X_train;
auto Y_train = split.Y_train;
auto X_test = split.X_test;
auto Y_test = split.Y_test;
int epochs = 32;
float learning_rate = 0.01;
SimpleNN model(X_train.size(1));
// Training Loop
for (int epoch = 0; epoch < epochs; ++epoch)
{
// Forward pass
auto y_pred = model->forward(X_train);
// Loss
auto loss = model->loss_function(y_pred, Y_train);
// Backward pass
loss.backward();
// Parameter update (manual, no optimizer)
{
torch::NoGradGuard no_grad;
model->weight -= learning_rate * model->weight.grad();
model->bias -= learning_rate * model->bias.grad();
}
// Zero gradients
model->weight.grad().zero_();
model->bias.grad().zero_();
}
// Test evaluation
torch::NoGradGuard no_grad;
auto test_pred = model->forward(X_test);
auto test_loss = model->loss_function(test_pred, Y_test);
// Accuracy: threshold at 0.5
auto predicted = (test_pred >= 0.5).to(torch::kFloat64);
auto correct = predicted.eq(Y_test).sum().item<int64_t>();
double accuracy = static_cast<double>(correct) / X_test.size(0);
// Export results
exportModelResults("Scratch_NN", predicted, test_pred, Y_test,
accuracy, test_loss.item<double>());
}
// Modified Custom Dataset Model with export functionality
void runCustomDatasetWithExport()
{
std::string path = "/home/moinshaikh/CLionProjects/BreastCancerPrediction/database/data.csv";
csv::CSVFormat format;
format.delimiter(',').no_header();
TrainingPipeline::Pipeline pipeline(path, format);
pipeline.Encoding();
torch::Tensor X = torch::stack(pipeline.getFeatures(), 1);
torch::Tensor y = pipeline.getheadersTensors()["diagnosis"];
X = pipeline.Normalization(X).to(torch::kFloat32);
y = y.to(torch::kFloat32);
auto split = pipeline.splitTensors(X, y, 0.2);
auto X_train = split.X_train;
auto y_train = split.Y_train;
auto X_test = split.X_test;
auto y_test = split.Y_test;
int epochs = 100;
float learning_rate = 0.01;
// Create custom dataset and dataloader
auto train_dataset = CustomDatasets(X_train, y_train)
.map(torch::data::transforms::Stack<>());
auto train_loader = torch::data::make_data_loader(
std::move(train_dataset),
torch::data::DataLoaderOptions().batch_size(32));
NNModel model(X_train.size(1));
auto loss_fn = torch::nn::BCELoss();
auto optimizer = torch::optim::SGD(model->parameters(), learning_rate);
// Training loop with batches
for (int epoch = 0; epoch < epochs; ++epoch)
{
for (auto& batch : *train_loader)
{
auto data = batch.data;
auto targets = batch.target.view({-1, 1});
auto yPred = model->forward(data);
auto loss_val = loss_fn(yPred, targets);
optimizer.zero_grad();
loss_val.backward();
optimizer.step();
}
}
// Test evaluation
torch::NoGradGuard no_grad;
auto test_pred = model->forward(X_test);
auto test_loss = loss_fn(test_pred, y_test);
auto predicted = (test_pred >= 0.5).to(torch::kFloat32);
auto correct = predicted.eq(y_test).sum().item<int64_t>();
double accuracy = static_cast<double>(correct) / X_test.size(0);
// Export results
exportModelResults("Custom_Dataset", predicted, test_pred, y_test,
accuracy, test_loss.item<double>());
}
TEST_CASE("exportData")
{
std::remove("model_results.json");
runPyTorchModelWithExport();
runScratchModelWithExport();
runCustomDatasetWithExport();
std::cout << "All model results exported to model_results.json\n";
std::cout << "Run 'python visualize_models.py' to generate visualizations\n";
}