Fix json key automatic ordering error when creating Score from json

This commit is contained in:
2024-05-12 18:23:48 +02:00
parent 07a2efb298
commit ebaddf1a6c
23 changed files with 81 additions and 43 deletions

View File

@@ -5,7 +5,7 @@
#include "Scores.h"
#include "Experiment.h"
namespace platform {
using json = nlohmann::json;
using json = nlohmann::ordered_json;
void Experiment::saveResult()
{

View File

@@ -9,7 +9,7 @@
#include "results/Result.h"
namespace platform {
using json = nlohmann::json;
using json = nlohmann::ordered_json;
class Experiment {
public:

View File

@@ -6,7 +6,7 @@
#include <nlohmann/json.hpp>
namespace platform {
using json = nlohmann::json;
using json = nlohmann::ordered_json;
class HyperParameters {
public:
HyperParameters() = default;

View File

@@ -4,7 +4,7 @@
#include <nlohmann/json.hpp>
namespace platform {
using json = nlohmann::json;
using json = nlohmann::ordered_json;
class PartialResult {
public:

View File

@@ -37,11 +37,16 @@ namespace platform {
}
i++;
}
// Compute accuracy with the confusion matrix
compute_accuracy_value();
}
void Scores::compute_accuracy_value()
{
accuracy_value = 0;
for (int i = 0; i < num_classes; i++) {
accuracy_value += confusion_matrix[i][i].item<int>();
}
accuracy_value /= total;
accuracy_value = std::min(accuracy_value, 1.0f);
}
void Scores::init_confusion_matrix()
{
@@ -59,8 +64,7 @@ namespace platform {
throw std::invalid_argument("The number of classes must be the same");
confusion_matrix += a.confusion_matrix;
total += a.total;
accuracy_value += a.accuracy_value;
accuracy_value /= 2;
compute_accuracy_value();
}
float Scores::accuracy()
{
@@ -71,6 +75,7 @@ namespace platform {
// Compute f1_score in a one vs rest fashion
auto precision_value = precision(num_class);
auto recall_value = recall(num_class);
if (precision_value + recall_value == 0) return 0; // Avoid division by zero (0/0 = 0)
return 2 * precision_value * recall_value / (precision_value + recall_value);
}
float Scores::f1_weighted()
@@ -94,6 +99,7 @@ namespace platform {
int tp = confusion_matrix[num_class][num_class].item<int>();
int fp = confusion_matrix.index({ "...", num_class }).sum().item<int>() - tp;
int fn = confusion_matrix[num_class].sum().item<int>() - tp;
if (tp + fp == 0) return 0; // Avoid division by zero (0/0 = 0
return float(tp) / (tp + fp);
}
float Scores::recall(int num_class)
@@ -101,6 +107,7 @@ namespace platform {
int tp = confusion_matrix[num_class][num_class].item<int>();
int fp = confusion_matrix.index({ "...", num_class }).sum().item<int>() - tp;
int fn = confusion_matrix[num_class].sum().item<int>() - tp;
if (tp + fn == 0) return 0; // Avoid division by zero (0/0 = 0
return float(tp) / (tp + fn);
}
std::string Scores::classification_report_line(std::string label, float precision, float recall, float f1_score, int support)

View File

@@ -5,7 +5,7 @@
#include <torch/torch.h>
#include <nlohmann/json.hpp>
namespace platform {
using json = nlohmann::json;
using json = nlohmann::ordered_json;
class Scores {
public:
Scores(torch::Tensor& y_test, torch::Tensor& y_pred, int num_classes, std::vector<std::string> labels = {});
@@ -24,6 +24,7 @@ namespace platform {
std::string classification_report_line(std::string label, float precision, float recall, float f1_score, int support);
void init_confusion_matrix();
void init_default_labels();
void compute_accuracy_value();
int num_classes;
float accuracy_value;
int total;