DiSMEC++
dismec::model::DenseModel Class Reference

Implementation of the Model class that stores the weights as a single, dense matrix. More...

#include <dense.h>

Inheritance diagram for dismec::model::DenseModel:
dismec::model::Model

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 WeightMatrixget_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
 

Detailed Description

Implementation of the Model class that stores the weights as a single, dense matrix.

Definition at line 16 of file dense.h.

Member Typedef Documentation

◆ weight_matrix_ptr

Definition at line 20 of file dense.h.

◆ WeightMatrix

Constructor & Destructor Documentation

◆ DenseModel() [1/4]

DenseModel::DenseModel ( const weight_matrix_ptr weights)
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.

Parameters
weightsWeights 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

Definition at line 31 of file dense.cpp.

◆ DenseModel() [2/4]

DenseModel::DenseModel ( weight_matrix_ptr  weights,
PartialModelSpec  partial 
)

Creates a (potentially partial) dense model with the given weight matrix.

Parameters
weightsWeights for the labels as specified in partial. The number of columns needs to match the number of weights as specified in partial.
partialSpecifies where to place this partial model inside the complete model.
Exceptions
Ifthe 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() [3/4]

DenseModel::DenseModel ( long  num_features,
long  num_labels 
)

Definition at line 46 of file dense.cpp.

◆ DenseModel() [4/4]

DenseModel::DenseModel ( long  num_features,
PartialModelSpec  partial 
)

Definition at line 51 of file dense.cpp.

Member Function Documentation

◆ get_raw_weights()

const WeightMatrix& dismec::model::DenseModel::get_raw_weights ( ) const
inline

provides read-only access to the raw weight matrix.

Definition at line 51 of file dense.h.

References m_Weights.

◆ get_weights_for_label_unchecked()

void DenseModel::get_weights_for_label_unchecked ( label_id_t  label,
Eigen::Ref< DenseRealVector target 
) const
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.

Exceptions
Iftarget does not have the correct size, or if label is invalid.
Note
This function is called from 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().

◆ has_sparse_weights()

bool dismec::model::DenseModel::has_sparse_weights ( ) const
inlinefinalvirtual

A dense model doesn't have sparse weights.

Implements dismec::model::Model.

Definition at line 46 of file dense.h.

◆ num_features()

long DenseModel::num_features ( ) const
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.

◆ predict_scores_unchecked()

void DenseModel::predict_scores_unchecked ( const FeatureMatrixIn instances,
PredictionMatrixOut  target 
) const
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.

Parameters
instancesFeature 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.
targetThis 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.
Exceptions
Ifinstances 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()).
Note
This function is called from 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().

◆ set_weights_for_label_unchecked()

void DenseModel::set_weights_for_label_unchecked ( label_id_t  label,
const WeightVectorIn weights 
)
overrideprivatevirtual

Unchecked version of set_weights_for_label().

This assigns the given vector as weights for the labelth 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.

Exceptions
Ifweights does not have the correct size, or if label is invalid.
Note
This function is called from 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().

Member Data Documentation

◆ m_Weights

weight_matrix_ptr dismec::model::DenseModel::m_Weights
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().


The documentation for this class was generated from the following files: