[go: up one dir, main page]

File: meter.h

package info (click to toggle)
fasttext 0.9.2%2Bds-8
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 4,940 kB
  • sloc: cpp: 5,459; python: 2,427; javascript: 635; sh: 621; makefile: 106; xml: 81; perl: 43
file content (91 lines) | stat: -rw-r--r-- 2,510 bytes parent folder | download | duplicates (4)
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
/**
 * 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"
#include "utils.h"

namespace fasttext {

class Meter {
  struct Metrics {
    uint64_t gold;
    uint64_t predicted;
    uint64_t predictedGold;
    mutable std::vector<std::pair<real, real>> scoreVsTrue;

    Metrics() : gold(0), predicted(0), predictedGold(0), scoreVsTrue() {}

    double precision() const {
      if (predicted == 0) {
        return std::numeric_limits<double>::quiet_NaN();
      }
      return predictedGold / double(predicted);
    }
    double recall() const {
      if (gold == 0) {
        return std::numeric_limits<double>::quiet_NaN();
      }
      return predictedGold / double(gold);
    }
    double f1Score() const {
      if (predicted + gold == 0) {
        return std::numeric_limits<double>::quiet_NaN();
      }
      return 2 * predictedGold / double(predicted + gold);
    }

    std::vector<std::pair<real, real>> getScoreVsTrue() {
      return scoreVsTrue;
    }
  };
  std::vector<std::pair<uint64_t, uint64_t>> getPositiveCounts(
      int32_t labelId) const;

 public:
  Meter() = delete;
  explicit Meter(bool falseNegativeLabels)
      : metrics_(),
        nexamples_(0),
        labelMetrics_(),
        falseNegativeLabels_(falseNegativeLabels) {}

  void log(const std::vector<int32_t>& labels, const Predictions& predictions);

  double precision(int32_t);
  double recall(int32_t);
  double f1Score(int32_t);
  std::vector<std::pair<real, real>> scoreVsTrue(int32_t labelId) const;
  double precisionAtRecall(int32_t labelId, double recall) const;
  double precisionAtRecall(double recall) const;
  double recallAtPrecision(int32_t labelId, double recall) const;
  double recallAtPrecision(double recall) const;
  std::vector<std::pair<double, double>> precisionRecallCurve(
      int32_t labelId) const;
  std::vector<std::pair<double, double>> precisionRecallCurve() const;
  double precision() const;
  double recall() const;
  double f1Score() 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_;
  bool falseNegativeLabels_;
};

} // namespace fasttext