forked from facebookresearch/fastText
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeter.h
61 lines (49 loc) · 1.31 KB
/
meter.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
59
60
61
/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <unordered_map>
#include <vector>
#include "dictionary.h"
#include "real.h"
namespace fasttext {
class Meter {
struct Metrics {
uint64_t gold;
uint64_t predicted;
uint64_t predictedGold;
Metrics() : gold(0), predicted(0), predictedGold(0) {}
double precision() const {
return predictedGold / double(predicted);
}
double recall() const {
return predictedGold / double(gold);
}
double f1Score() const {
return 2 * predictedGold / double(predicted + gold);
}
};
public:
Meter() : metrics_(), nexamples_(0), labelMetrics_() {}
void log(
const std::vector<int32_t>& labels,
const std::vector<std::pair<real, int32_t>>& predictions);
double precision(int32_t);
double recall(int32_t);
double f1Score(int32_t);
double precision() const;
double recall() const;
uint64_t nexamples() const {
return nexamples_;
}
void writeGeneralMetrics(std::ostream& out, int32_t k) const;
private:
Metrics metrics_{};
uint64_t nexamples_;
std::unordered_map<int32_t, Metrics> labelMetrics_;
};
} // namespace fasttext