-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.h
58 lines (50 loc) · 1.87 KB
/
logger.h
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
#include <vector>
#include "metrics.h"
#include <iostream>
#include <map>
#include <string>
class Logger {
public:
Logger(const std::vector<size_t>& k): k_(k){
}
void UpdateMetrics(const std::vector<size_t>& predictions, const std::vector<size_t>& target) {
for (size_t i = 0; i < names_metrics.size(); ++i) {
if (i == 0 || i == 3) {
for (auto elem: k_) {
std::string cur_name_metric = names_metrics[i] + std::to_string(elem);
cnt_metrics_[cur_name_metric] += 1;
if (i == 0) {
sum_metrics_[cur_name_metric] += metrics::MAPK({predictions}, {target}, elem);
} else {
sum_metrics_[cur_name_metric] += metrics::PrecisionK({predictions}, {target}, elem);
}
}
} else {
std::string cur_name_metric = names_metrics[i];
cnt_metrics_[cur_name_metric] += 1;
if (i == 1) {
sum_metrics_[cur_name_metric] += metrics::MRR({predictions}, {target});
} else {
sum_metrics_[cur_name_metric] += metrics::NDCG({predictions}, {target});
}
}
}
}
void PrintResult() {
std::cout << "=========RESULTS=========\n";
for (auto elem: sum_metrics_) {
std::cout << elem.first << ": " << (elem.second) / cnt_metrics_[elem.first] << '\n';
}
std::cout << "=========================\n";
}
private:
std::vector<size_t> k_;
std::map<std::string, double> sum_metrics_;
std::map<std::string, double> cnt_metrics_;
const std::vector<std::string> names_metrics = {
"MAP@",
"MRR",
"NDCG",
"Precision@"
};
};