DiSMEC++
evaluate.h
Go to the documentation of this file.
1 // Copyright (c) 2021, Aalto University, developed by Erik Schultheis
2 // All rights reserved.
3 //
4 // SPDX-License-Identifier: MIT
5 
6 #ifndef DISMEC_SRC_PREDICTION_EVALUATE_H
7 #define DISMEC_SRC_PREDICTION_EVALUATE_H
8 
9 #include "parallel/task.h"
10 #include "matrix_types.h"
11 #include "data/types.h"
12 #include <memory>
13 
14 namespace dismec::prediction {
15 
16  struct sTrueLabelInfo {
18  long Rank;
19  };
20  struct sPredLabelInfo {
22  bool Correct;
23  };
24 
27  class MacroMetricReporter;
28 
35  public:
36  using LabelList = std::vector<std::vector<label_id_t>>;
37 
38  EvaluateMetrics(const LabelList* sparse_labels, const IndexMatrix* sparse_predictions, long num_labels);
39  ~EvaluateMetrics() override;
40 
41  void prepare(long num_threads, long chunk_size) override;
42  void init_thread(thread_id_t thread_id) override;
43 
44  void add_precision_at_k(long k, std::string name = {});
45  void add_dcg_at_k(long k, bool normalize, std::string name = {});
46  void add_abandonment_at_k(long k, std::string name = {});
47  MacroMetricReporter* add_macro_at_k(long k);
48 
49  [[nodiscard]] std::vector<std::pair<std::string, double>> get_metrics() const;
50 
51  void run_task(long task_id, thread_id_t thread_id);
52  void run_tasks(long begin, long end, thread_id_t thread_id) override;
53 
54  void finalize() override;
55 
56  [[nodiscard]] long num_tasks() const override;
57 
58  using prediction_t = Eigen::Ref<const Eigen::Matrix<long, 1, Eigen::Dynamic>>;
59  static void process_prediction(const std::vector<label_id_t>& raw_labels, const prediction_t& raw_prediction,
60  std::vector<sTrueLabelInfo>& proc_labels, std::vector<sPredLabelInfo>& proc_pred);
61 
62  private:
66 
67  std::vector<std::vector<std::unique_ptr<MetricCollectionInterface>>> m_Collectors;
68  std::vector<std::unique_ptr<MetricReportInterface>> m_Metrics;
69 
70  // thread local work variables
71  std::vector<std::vector<sTrueLabelInfo>> m_ThreadLocalTrueLabels;
72  std::vector<std::vector<sPredLabelInfo>> m_ThreadLocalPredictedLabels;
73  };
74 
75 }
76 
77 
78 #endif //DISMEC_SRC_PREDICTION_EVALUATE_H
Strong typedef for an int to signify a label id.
Definition: types.h:20
Base class for all parallelized operations.
Definition: task.h:21
dismec::parallel::thread_id_t thread_id_t
Definition: task.h:23
Strong typedef for an int to signify a thread id.
Definition: thread_id.h:20
This TaskGenerator enables the calculation of evaluation metrics on top-k style sparse predictions.
Definition: evaluate.h:34
const IndexMatrix * m_Predictions
Definition: evaluate.h:64
void add_dcg_at_k(long k, bool normalize, std::string name={})
Definition: evaluate.cpp:85
MacroMetricReporter * add_macro_at_k(long k)
Definition: evaluate.cpp:120
std::vector< std::vector< sTrueLabelInfo > > m_ThreadLocalTrueLabels
Definition: evaluate.h:71
void prepare(long num_threads, long chunk_size) override
Called to notify the TaskGenerator about the number of threads.
Definition: evaluate.cpp:143
void add_precision_at_k(long k, std::string name={})
Definition: evaluate.cpp:71
EvaluateMetrics(const LabelList *sparse_labels, const IndexMatrix *sparse_predictions, long num_labels)
Definition: evaluate.cpp:13
void finalize() override
Called after all threads have finished their tasks.
Definition: evaluate.cpp:149
long num_tasks() const override
Definition: evaluate.cpp:67
std::vector< std::vector< std::unique_ptr< MetricCollectionInterface > > > m_Collectors
Definition: evaluate.h:67
Eigen::Ref< const Eigen::Matrix< long, 1, Eigen::Dynamic > > prediction_t
Definition: evaluate.h:58
void run_tasks(long begin, long end, thread_id_t thread_id) override
Definition: evaluate.cpp:61
std::vector< std::unique_ptr< MetricReportInterface > > m_Metrics
Definition: evaluate.h:68
std::vector< std::pair< std::string, double > > get_metrics() const
Definition: evaluate.cpp:134
void add_abandonment_at_k(long k, std::string name={})
Definition: evaluate.cpp:106
void init_thread(thread_id_t thread_id) override
Called once a thread has spun up, but before it runs its first task.
Definition: evaluate.cpp:157
static void process_prediction(const std::vector< label_id_t > &raw_labels, const prediction_t &raw_prediction, std::vector< sTrueLabelInfo > &proc_labels, std::vector< sPredLabelInfo > &proc_pred)
Definition: evaluate.cpp:24
std::vector< std::vector< sPredLabelInfo > > m_ThreadLocalPredictedLabels
Definition: evaluate.h:72
std::vector< std::vector< label_id_t > > LabelList
Definition: evaluate.h:36
void run_task(long task_id, thread_id_t thread_id)
Definition: evaluate.cpp:47
Base class for all metrics that can be calculated during the evaluation phase.
Definition: metrics.h:28
types::DenseRowMajor< long > IndexMatrix
Matrix used for indices in sparse predictions.
Definition: matrix_types.h:81