DiSMEC++
prediction.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_PREDICTION_H
7 #define DISMEC_PREDICTION_H
8 
9 #include "parallel/task.h"
10 #include "parallel/numa.h"
11 #include "matrix_types.h"
12 #include "fwd.h"
13 #include <memory>
14 
15 namespace dismec::prediction {
16  using model::Model;
17 
35  public:
37  PredictionBase(const DatasetBase* data, std::shared_ptr<const Model> model);
38 
39  protected:
41  std::shared_ptr<const Model> m_Model;
42 
45  void make_thread_local_features(long num_threads);
46 
47  void init_thread(thread_id_t thread_id) final;
48 
59  void do_prediction(long begin, long end, thread_id_t thread_id, Eigen::Ref<PredictionMatrix> target);
60 
61  private:
64 
67  std::vector<std::shared_ptr<const GenericFeatureMatrix>> m_ThreadLocalFeatures;
68  };
69 
71  public:
72 
73  FullPredictionTaskGenerator(const DatasetBase* data, std::shared_ptr<const Model> model);
74 
75  void prepare(long num_threads, long chunk_size) override;
76  void run_tasks(long begin, long end, thread_id_t thread_id) override;
77  [[nodiscard]] long num_tasks() const override;
78 
79  [[nodiscard]] const PredictionMatrix& get_predictions() const { return m_Predictions; }
80  private:
82  };
83 
85  public:
86  TopKPredictionTaskGenerator(const DatasetBase* data, std::shared_ptr<const Model> model, long K);
87 
88  void update_model(std::shared_ptr<const Model> model);
89 
90  void run_tasks(long begin, long end, thread_id_t thread_id) override;
91 
92  [[nodiscard]] long num_tasks() const override;
93  void prepare(long num_threads, long chunk_size) override;
94  void finalize() override;
95 
96  [[nodiscard]] const PredictionMatrix& get_top_k_values() const { return m_TopKValues; }
97  [[nodiscard]] const IndexMatrix& get_top_k_indices() const { return m_TopKIndices; }
98 
99  [[nodiscard]] const std::array<std::int64_t, 4>& get_confusion_matrix() const { return m_ConfusionMatrix; }
100 
101  static constexpr const int TRUE_POSITIVES = 0;
102  static constexpr const int FALSE_POSITIVES = 1;
103  static constexpr const int TRUE_NEGATIVES = 2;
104  static constexpr const int FALSE_NEGATIVES = 3;
105  private:
106  long m_K;
107 
110 
111  std::vector<PredictionMatrix> m_ThreadLocalPredictionCache;
112  std::vector<PredictionMatrix> m_ThreadLocalTopKValues;
113  std::vector<IndexMatrix> m_ThreadLocalTopKIndices;
114  std::vector<std::array<std::int64_t, 4>> m_ThreadLocalConfusionMatrix;
115 
116  std::vector<std::vector<long>> m_GroundTruth;
117  std::array<std::int64_t, 4> m_ConfusionMatrix;
118  };
119 }
120 #endif //DISMEC_PREDICTION_H
Helper class to ensure that each NUMA node has its own copy of some immutable data.
Definition: numa.h:72
Base class for all parallelized operations.
Definition: task.h:21
Strong typedef for an int to signify a thread id.
Definition: thread_id.h:20
void run_tasks(long begin, long end, thread_id_t thread_id) override
Definition: prediction.cpp:70
const PredictionMatrix & get_predictions() const
Definition: prediction.h:79
FullPredictionTaskGenerator(const DatasetBase *data, std::shared_ptr< const Model > model)
Definition: prediction.cpp:59
void prepare(long num_threads, long chunk_size) override
Called to notify the TaskGenerator about the number of threads.
Definition: prediction.cpp:75
Base class for handling predictions.
Definition: prediction.h:34
void do_prediction(long begin, long end, thread_id_t thread_id, Eigen::Ref< PredictionMatrix > target)
Predicts the scores for a subset of the instances given by the half-open interval [begin,...
Definition: prediction.cpp:51
void init_thread(thread_id_t thread_id) final
Called once a thread has spun up, but before it runs its first task.
Definition: prediction.cpp:38
PredictionBase(const DatasetBase *data, std::shared_ptr< const Model > model)
Constructor, checks that data and model are compatible.
Definition: prediction.cpp:17
std::vector< std::shared_ptr< const GenericFeatureMatrix > > m_ThreadLocalFeatures
Definition: prediction.h:67
std::shared_ptr< const Model > m_Model
Model (possibly partial) for which prediction is run.
Definition: prediction.h:41
const DatasetBase * m_Data
Data on which the prediction is run.
Definition: prediction.h:40
parallel::NUMAReplicator< const GenericFeatureMatrix > m_FeatureReplicator
The NUMAReplicator that generates NUMA-local copies for the feature matrices.
Definition: prediction.h:63
void make_thread_local_features(long num_threads)
Definition: prediction.cpp:34
static constexpr const int TRUE_POSITIVES
Definition: prediction.h:101
const IndexMatrix & get_top_k_indices() const
Definition: prediction.h:97
void finalize() override
Called after all threads have finished their tasks.
Definition: prediction.cpp:124
std::vector< PredictionMatrix > m_ThreadLocalTopKValues
Definition: prediction.h:112
std::vector< PredictionMatrix > m_ThreadLocalPredictionCache
Definition: prediction.h:111
void update_model(std::shared_ptr< const Model > model)
Definition: prediction.cpp:210
const PredictionMatrix & get_top_k_values() const
Definition: prediction.h:96
std::vector< std::vector< long > > m_GroundTruth
Definition: prediction.h:116
std::vector< IndexMatrix > m_ThreadLocalTopKIndices
Definition: prediction.h:113
static constexpr const int TRUE_NEGATIVES
Definition: prediction.h:103
TopKPredictionTaskGenerator(const DatasetBase *data, std::shared_ptr< const Model > model, long K)
Definition: prediction.cpp:80
static constexpr const int FALSE_POSITIVES
Definition: prediction.h:102
const std::array< std::int64_t, 4 > & get_confusion_matrix() const
Definition: prediction.h:99
static constexpr const int FALSE_NEGATIVES
Definition: prediction.h:104
void run_tasks(long begin, long end, thread_id_t thread_id) override
Definition: prediction.cpp:133
void prepare(long num_threads, long chunk_size) override
Called to notify the TaskGenerator about the number of threads.
Definition: prediction.cpp:103
std::array< std::int64_t, 4 > m_ConfusionMatrix
Definition: prediction.h:117
std::vector< std::array< std::int64_t, 4 > > m_ThreadLocalConfusionMatrix
Definition: prediction.h:114
Forward-declares types.
types::DenseRowMajor< long > IndexMatrix
Matrix used for indices in sparse predictions.
Definition: matrix_types.h:81
types::DenseRowMajor< real_t > PredictionMatrix
Dense matrix in Row Major format used for predictions.
Definition: matrix_types.h:75