DiSMEC++
|
#include <sparse.h>
Public Member Functions | |
SparseModel (long num_features, long num_labels) | |
SparseModel (long num_features, PartialModelSpec partial) | |
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 | predict_scores_unchecked (const FeatureMatrixIn &instances, PredictionMatrixOut target) const override |
Unchecked version of predict_scores(). 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 WeightVectorIn &weights) override |
Unchecked version of set_weights_for_label(). 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 Attributes | |
std::vector< SparseRealVector > | m_Weights |
long | m_NumFeatures |
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 |
SparseModel::SparseModel | ( | long | num_features, |
long | num_labels | ||
) |
Definition at line 24 of file sparse.cpp.
SparseModel::SparseModel | ( | long | num_features, |
PartialModelSpec | partial | ||
) |
Definition at line 29 of file sparse.cpp.
References m_Weights, and num_features().
|
overridevirtual |
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 84 of file sparse.cpp.
References m_Weights, and dismec::opaque_int_type< Tag, T >::to_index().
|
inlineoverridevirtual |
whether this model stores the weights in a sparse format, or a dense format.
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 39 of file sparse.cpp.
References m_NumFeatures.
Referenced by SparseModel().
|
overridevirtual |
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 79 of file sparse.cpp.
References m_Weights, and dismec::types::visit().
|
overridevirtual |
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 113 of file sparse.cpp.
References m_Weights, dismec::opaque_int_type< Tag, T >::to_index(), and dismec::types::visit().
|
private |
Definition at line 29 of file sparse.h.
Referenced by num_features().
|
private |
Definition at line 28 of file sparse.h.
Referenced by get_weights_for_label_unchecked(), predict_scores_unchecked(), set_weights_for_label_unchecked(), and SparseModel().