DiSMEC++
dismec::model::SubModelWrapper< T > Class Template Reference

#include <submodel.h>

Inheritance diagram for dismec::model::SubModelWrapper< T >:
dismec::model::Model

Public Member Functions

 SubModelWrapper (T original, label_id_t begin, label_id_t end)
 
long num_features () const override
 How many weights are in each weight vector, i.e. how many features should the input have. More...
 
bool has_sparse_weights () const override
 whether this model stores the weights in a sparse format, or a dense format. More...
 
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 GenericInVector &weights) override
 Unchecked version of set_weights_for_label(). More...
 
void predict_scores_unchecked (const GenericInMatrix &instances, PredictionMatrixOut target) const override
 Unchecked version of predict_scores(). 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 Types

using model_t = std::remove_reference_t< decltype(*std::declval< T >())>
 

Private Attributes

m_Original
 

Additional Inherited Members

- Public Types inherited from dismec::model::Model
using PredictionMatrixOut = Eigen::Ref< PredictionMatrix >
 
using FeatureMatrixIn = GenericInMatrix
 
using WeightVectorIn = GenericInVector
 
- Protected Member Functions inherited from dismec::model::Model
label_id_t adjust_label (label_id_t label) const
 

Detailed Description

template<class T>
class dismec::model::SubModelWrapper< T >

Definition at line 13 of file submodel.h.

Member Typedef Documentation

◆ model_t

template<class T >
using dismec::model::SubModelWrapper< T >::model_t = std::remove_reference_t<decltype(*std::declval<T>())>
private

Definition at line 14 of file submodel.h.

Constructor & Destructor Documentation

◆ SubModelWrapper()

template<class T >
dismec::model::SubModelWrapper< T >::SubModelWrapper ( original,
label_id_t  begin,
label_id_t  end 
)
inline

Definition at line 17 of file submodel.h.

Member Function Documentation

◆ get_weights_for_label_unchecked()

template<class T >
void dismec::model::SubModelWrapper< T >::get_weights_for_label_unchecked ( label_id_t  label,
Eigen::Ref< DenseRealVector target 
) const
inlineoverridevirtual

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 25 of file submodel.h.

References dismec::model::Model::labels_begin(), dismec::model::SubModelWrapper< T >::m_Original, and dismec::opaque_int_type< Tag, T >::to_index().

◆ has_sparse_weights()

template<class T >
bool dismec::model::SubModelWrapper< T >::has_sparse_weights ( ) const
inlineoverridevirtual

whether this model stores the weights in a sparse format, or a dense format.

Implements dismec::model::Model.

Definition at line 23 of file submodel.h.

References dismec::model::SubModelWrapper< T >::m_Original.

◆ num_features()

template<class T >
long dismec::model::SubModelWrapper< T >::num_features ( ) const
inlineoverridevirtual

How many weights are in each weight vector, i.e. how many features should the input have.

Implements dismec::model::Model.

Definition at line 22 of file submodel.h.

References dismec::model::SubModelWrapper< T >::m_Original.

◆ predict_scores_unchecked()

template<class T >
void dismec::model::SubModelWrapper< T >::predict_scores_unchecked ( const GenericInMatrix instances,
PredictionMatrixOut  target 
) const
inlineoverridevirtual

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 38 of file submodel.h.

◆ set_weights_for_label_unchecked()

template<class T >
void dismec::model::SubModelWrapper< T >::set_weights_for_label_unchecked ( label_id_t  label,
const GenericInVector weights 
)
inlineoverridevirtual

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 29 of file submodel.h.

References dismec::model::Model::labels_begin(), dismec::model::SubModelWrapper< T >::m_Original, and dismec::opaque_int_type< Tag, T >::to_index().

Member Data Documentation

◆ m_Original


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