DiSMEC++
model.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_MODEL_H
7 #define DISMEC_MODEL_H
8 
9 #include <vector>
10 #include <memory>
11 #include <variant>
12 #include "matrix_types.h"
13 #include "data/types.h"
14 
15 namespace dismec::model
16 {
24  long label_count;
25  long total_labels;
26  };
27 
63  class Model {
64  public:
65  using PredictionMatrixOut = Eigen::Ref<PredictionMatrix>;
68 
69  explicit Model(PartialModelSpec spec);
70 
71  virtual ~Model() = default;
72 
73  // general data shape
78  [[nodiscard]] long num_labels() const noexcept { return m_NumLabels; };
79 
81  [[nodiscard]] virtual long num_features() const = 0;
82 
87  [[nodiscard]] long num_weights() const noexcept { return labels_end() - labels_begin(); }
88 
90  [[nodiscard]] virtual bool has_sparse_weights() const = 0;
91 
92  // submodel data
94  [[nodiscard]] bool is_partial_model() const;
95 
98  [[nodiscard]] label_id_t labels_begin() const noexcept { return m_LabelsBegin; }
99 
102  [[nodiscard]] label_id_t labels_end() const noexcept { return m_LabelsEnd; }
103 
105  [[nodiscard]] long contained_labels() const noexcept { return m_LabelsEnd - m_LabelsBegin; }
106 
114  void get_weights_for_label(label_id_t label, Eigen::Ref<DenseRealVector> target) const;
115 
124  void set_weights_for_label(label_id_t label, const WeightVectorIn& weights);
141  void predict_scores(const FeatureMatrixIn& instances, PredictionMatrixOut target) const;
142 
143  protected:
146  [[nodiscard]] label_id_t adjust_label(label_id_t label) const;
147 
148  private:
155  virtual void predict_scores_unchecked(const FeatureMatrixIn& instances,
156  PredictionMatrixOut target) const = 0;
157 
165  virtual void get_weights_for_label_unchecked(label_id_t label, Eigen::Ref<DenseRealVector> target) const = 0;
166 
174  virtual void set_weights_for_label_unchecked(label_id_t label, const WeightVectorIn& weights) = 0;
175 
178 
179 
186  };
187 
188 }
189 
190 #endif //DISMEC_MODEL_H
Strong typedef for an int to signify a label id.
Definition: types.h:20
A model combines a set of weight with some meta-information about these weights.
Definition: model.h:63
label_id_t labels_end() const noexcept
Definition: model.h:102
GenericInMatrix FeatureMatrixIn
Definition: model.h:66
virtual long num_features() const =0
How many weights are in each weight vector, i.e. how many features should the input have.
Model(PartialModelSpec spec)
Definition: model.cpp:13
long num_labels() const noexcept
How many labels are in the underlying dataset.
Definition: model.h:78
virtual void get_weights_for_label_unchecked(label_id_t label, Eigen::Ref< DenseRealVector > target) const =0
Unchecked version of get_weights_for_label().
long contained_labels() const noexcept
How many labels are in this submodel.
Definition: model.h:105
Eigen::Ref< PredictionMatrix > PredictionMatrixOut
Definition: model.h:65
virtual void predict_scores_unchecked(const FeatureMatrixIn &instances, PredictionMatrixOut target) const =0
Unchecked version of predict_scores().
virtual bool has_sparse_weights() const =0
whether this model stores the weights in a sparse format, or a dense format.
void set_weights_for_label(label_id_t label, const WeightVectorIn &weights)
Sets the weights for a label.
Definition: model.cpp:51
bool is_partial_model() const
returns true if this instance only stores part of the weights of an entire model
Definition: model.cpp:37
label_id_t m_LabelsEnd
Definition: model.h:177
long num_weights() const noexcept
How many weights vectors are in this model.
Definition: model.h:87
label_id_t m_LabelsBegin
Definition: model.h:176
GenericInVector WeightVectorIn
Definition: model.h:67
virtual void set_weights_for_label_unchecked(label_id_t label, const WeightVectorIn &weights)=0
Unchecked version of set_weights_for_label().
long m_NumLabels
Total number of labels of the complete model.
Definition: model.h:185
virtual ~Model()=default
void get_weights_for_label(label_id_t label, Eigen::Ref< DenseRealVector > target) const
Gets the weights for the given label as a dense vector.
Definition: model.cpp:41
label_id_t labels_begin() const noexcept
Definition: model.h:98
void predict_scores(const FeatureMatrixIn &instances, PredictionMatrixOut target) const
Calculates the scores for all examples and all labels in this model.
Definition: model.cpp:60
label_id_t adjust_label(label_id_t label) const
Definition: model.cpp:28
types::GenericVectorRef< const real_t > GenericInVector
Definition: matrix_types.h:35
types::GenericMatrixRef< const real_t > GenericInMatrix
Definition: matrix_types.h:33
Specifies how to interpret a weight matrix for a partial model.
Definition: model.h:22
label_id_t first_label
First label in the partial model.
Definition: model.h:23
long label_count
Number of labels in the partial model.
Definition: model.h:24
long total_labels
Total number of labels.
Definition: model.h:25