DiSMEC++
|
Implementation of the Model class that stores the weights as a single, dense matrix. More...
#include <dense.h>
Public Types | |
using | WeightMatrix = types::DenseColMajor< real_t > |
using | weight_matrix_ptr = std::shared_ptr< WeightMatrix > |
Public Types inherited from dismec::model::Model | |
using | PredictionMatrixOut = Eigen::Ref< PredictionMatrix > |
using | FeatureMatrixIn = GenericInMatrix |
using | WeightVectorIn = GenericInVector |
Public Member Functions | |
DenseModel (const weight_matrix_ptr &weights) | |
Creates a (complete) dense model with the given weight matrix. More... | |
DenseModel (weight_matrix_ptr weights, PartialModelSpec partial) | |
Creates a (potentially partial) dense model with the given weight matrix. More... | |
DenseModel (long num_features, long num_labels) | |
DenseModel (long num_features, PartialModelSpec partial) | |
bool | has_sparse_weights () const final |
A dense model doesn't have sparse weights. More... | |
long | num_features () const override |
How many weights are in each weight vector, i.e. how many features should the input have. More... | |
const WeightMatrix & | get_raw_weights () const |
provides read-only access to the raw weight matrix. More... | |
Public Member Functions inherited from dismec::model::Model | |
Model (PartialModelSpec spec) | |
virtual | ~Model ()=default |
long | num_labels () const noexcept |
How many labels are in the underlying dataset. More... | |
long | num_weights () const noexcept |
How many weights vectors are in this model. More... | |
bool | is_partial_model () const |
returns true if this instance only stores part of the weights of an entire model More... | |
label_id_t | labels_begin () const noexcept |
label_id_t | labels_end () const noexcept |
long | contained_labels () const noexcept |
How many labels are in this submodel. More... | |
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. More... | |
void | set_weights_for_label (label_id_t label, const WeightVectorIn &weights) |
Sets the weights for a label. More... | |
void | predict_scores (const FeatureMatrixIn &instances, PredictionMatrixOut target) const |
Calculates the scores for all examples and all labels in this model. More... | |
Private Member Functions | |
void | get_weights_for_label_unchecked (label_id_t label, Eigen::Ref< DenseRealVector > target) const override |
Unchecked version of get_weights_for_label(). More... | |
void | set_weights_for_label_unchecked (label_id_t label, const WeightVectorIn &weights) override |
Unchecked version of set_weights_for_label(). More... | |
void | predict_scores_unchecked (const FeatureMatrixIn &instances, PredictionMatrixOut target) const override |
Unchecked version of predict_scores(). More... | |
Private Attributes | |
weight_matrix_ptr | m_Weights |
The matrix of weights. More... | |
Additional Inherited Members | |
Protected Member Functions inherited from dismec::model::Model | |
label_id_t | adjust_label (label_id_t label) const |
Implementation of the Model class that stores the weights as a single, dense matrix.
using dismec::model::DenseModel::weight_matrix_ptr = std::shared_ptr<WeightMatrix> |
|
explicit |
Creates a (complete) dense model with the given weight matrix.
If you want to create only a partial model, the DenseModel(std::shared_ptr<Eigen::MatrixXd> weights, PartialModelSpec partial) constructor can be used.
weights | Weights for each label. |
Deliberately using const& here, instead of moving the shared_ptr, because that is still cheap and with move it becomes difficult to avoid reading from moved-from shared_ptr
DenseModel::DenseModel | ( | weight_matrix_ptr | weights, |
PartialModelSpec | partial | ||
) |
Creates a (potentially partial) dense model with the given weight matrix.
weights | Weights for the labels as specified in partial . The number of columns needs to match the number of weights as specified in partial . |
partial | Specifies where to place this partial model inside the complete model. |
If | the number of weight vectors (columns of weights ) does not match the specification in partial , or if the given label range is invalid. |
Definition at line 37 of file dense.cpp.
References dismec::model::PartialModelSpec::label_count, and m_Weights.
DenseModel::DenseModel | ( | long | num_features, |
long | num_labels | ||
) |
DenseModel::DenseModel | ( | long | num_features, |
PartialModelSpec | partial | ||
) |
|
inline |
|
overrideprivatevirtual |
Unchecked version of get_weights_for_label().
Since we do not know whether the weights saved in the model are sparse or dense vectors, we cannot simply return a const reference here. Instead, the user is required to provide a pre-allocated buffer target
into which the weights will be copied.
If | target does not have the correct size, or if label is invalid. |
get_weights_for_label
and can assume label is a valid label index that has been corrected for partial models (i.e. such that the first label of the partial model will get the index 0
). Target can be assumed to be of correct size. Implements dismec::model::Model.
Definition at line 64 of file dense.cpp.
References m_Weights, and dismec::opaque_int_type< Tag, T >::to_index().
|
inlinefinalvirtual |
A dense model doesn't have sparse weights.
Implements dismec::model::Model.
|
overridevirtual |
How many weights are in each weight vector, i.e. how many features should the input have.
Implements dismec::model::Model.
Definition at line 59 of file dense.cpp.
References m_Weights.
|
overrideprivatevirtual |
Unchecked version of predict_scores().
This is just the matrix multiplication of the input instances and the weight matrix. This function can be called safely from multiple threads. Note that Eigen::Ref requires that the passed submatrix has an inner stride of 1, i.e. that features for a single instance are provided as contiguous memory (in case of dense features), and the same for the pre-allocated buffer for the targets. This can be achieved e.g. by using rows of a row-major matrix.
instances | Feature vector of the instances for which we want to predict the scores. This is handled as a Eigen::Ref parameter so that subsets of a large dataset can be passed without needing data to be copied. Should have number of columns equal to the number of features. The GenericInMatrix allows different data formats to be passed – however, some data formats may be more efficient than others. |
target | This is the matrix to which the scores will be written. Has to have the correct size, i.e. the same number of rows as instances and number of columns equal to the number of labels. |
If | instances and target have different number of rows, or if the number of columns (rows) in instances (target ) does not match get_num_features() (get_num_labels() ). |
predict_scores
and can assume that the shapes of instances
and target
have been verified. Implements dismec::model::Model.
Definition at line 76 of file dense.cpp.
References m_Weights, and dismec::types::visit().
|
overrideprivatevirtual |
Unchecked version of set_weights_for_label().
This assigns the given vector as weights for the label
th label. For non-overlapping label
parameters this function can safely be called concurrently from different threads. This function can be used for both dense and sparse internal weight representation. If the model internally uses a sparse representation, the zeros will be filtered out.
If | weights does not have the correct size, or if label is invalid. |
set_weights_for_label
and can assume label is a valid label index that has been corrected for partial models (i.e. such that the first label of the partial model will get the index 0
). weights
can be assumed to be of correct size. Implements dismec::model::Model.
Definition at line 69 of file dense.cpp.
References m_Weights, dismec::opaque_int_type< Tag, T >::to_index(), and dismec::types::visit().
|
private |
The matrix of weights.
Each column in this matrix corresponds to the weight vector for one label.
Definition at line 65 of file dense.h.
Referenced by DenseModel(), get_raw_weights(), get_weights_for_label_unchecked(), num_features(), predict_scores_unchecked(), and set_weights_for_label_unchecked().