DiSMEC++
dismec::objective::DenseAndSparseLinearBase Class Referenceabstract

Base class for implementationa of an objective that combines dense features and sparse features. More...

#include <dense_and_sparse.h>

Inheritance diagram for dismec::objective::DenseAndSparseLinearBase:
dismec::objective::Objective dismec::stats::Tracked dismec::objective::DenseAndSparseMargin< MarginFunction, SparseRegFunction, DenseRegFunction >

Public Member Functions

 DenseAndSparseLinearBase (std::shared_ptr< const GenericFeatureMatrix > dense_features, std::shared_ptr< const GenericFeatureMatrix > sparse_features)
 
long num_instances () const noexcept
 
long num_variables () const noexcept override
 
BinaryLabelVectorget_label_ref ()
 
void update_costs (real_t positive, real_t negative)
 
void update_features (const DenseFeatures &dense, const SparseFeatures &sparse)
 
- Public Member Functions inherited from dismec::objective::Objective
 Objective ()
 
virtual ~Objective () noexcept=default
 
real_t value (const HashVector &location)
 Evaluate the objective at the given location. More...
 
void diag_preconditioner (const HashVector &location, Eigen::Ref< DenseRealVector > target)
 Get precondition to be used in CG optimization. More...
 
void project_to_line (const HashVector &location, const DenseRealVector &direction)
 creates a function g such that g(a) = objective(location + a * direction) Use lookup_on_line() to evaluate g. More...
 
void gradient_at_zero (Eigen::Ref< DenseRealVector > target)
 Gets the gradient for location zero. More...
 
void gradient (const HashVector &location, Eigen::Ref< DenseRealVector > target)
 Evaluate the gradient at location. More...
 
void hessian_times_direction (const HashVector &location, const DenseRealVector &direction, Eigen::Ref< DenseRealVector > target)
 Calculates the product of the Hessian matrix at location with direction. More...
 
void gradient_and_pre_conditioner (const HashVector &location, Eigen::Ref< DenseRealVector > gradient, Eigen::Ref< DenseRealVector > pre)
 Combines the calculation of gradient and pre-conditioner, which may be more efficient in some cases. More...
 
- Public Member Functions inherited from dismec::stats::Tracked
 Tracked ()
 Default constructor, creates the internal stats::StatisticsCollection. More...
 
void register_stat (const std::string &name, std::unique_ptr< Statistics > stat)
 Registers a tracker for the statistics name. More...
 
std::shared_ptr< StatisticsCollectionget_stats () const
 Gets an ownership-sharing reference to the StatisticsCollection. More...
 

Protected Member Functions

long get_num_variables () const noexcept
 actual implementation of num_variables(). We need this non-virtual function to be called during the constructor More...
 
const DenseRealVectorx_times_w (const HashVector &w)
 Calculates the vector of feature matrix times weights w More...
 
template<class Derived >
void update_xtw_cache (const HashVector &new_weight, const Eigen::MatrixBase< Derived > &new_result)
 Updates the cached value for x_times_w. More...
 
void project_linear_to_line (const HashVector &location, const DenseRealVector &direction)
 Prepares the cache variables for line projection. More...
 
auto line_interpolation (real_t t) const
 
void declare_vector_on_last_line (const HashVector &location, real_t t) override
 State that the given vector corresponds to a certain position on the line of the last line search. More...
 
const DenseFeaturesdense_features () const
 
const SparseFeaturessparse_features () const
 
const DenseRealVectorcosts () const
 
const BinaryLabelVectorlabels () const
 
- Protected Member Functions inherited from dismec::stats::Tracked
 ~Tracked ()
 Non-virtual destructor. Declared protected, so we don't accidentally try to do a polymorphic delete. More...
 
template<class T >
void record (stat_id_t stat, T &&value)
 Record statistics. This function just forwards all its arguments to the internal StatisticsCollection. More...
 
void declare_stat (stat_id_t index, StatisticMetaData meta)
 Declares a new statistics. This function just forwards all its arguments to the internal StatisticsCollection. More...
 
void declare_tag (tag_id_t index, std::string name)
 Declares a new tag. This function just forwards all its arguments to the internal StatisticsCollection. More...
 
template<class... Args>
void set_tag (tag_id_t tag, long value)
 Set value of tag. This function just forwards all its arguments to the internal StatisticsCollection. More...
 
template<class... Args>
auto make_timer (stat_id_t id, Args... args)
 Creates a new ScopeTimer using stats::record_scope_time. More...
 

Private Member Functions

real_t value_unchecked (const HashVector &location) override
 
real_t lookup_on_line (real_t position) override
 Looks up the value of the objective on the line defined by the last call to project_to_line(). More...
 
void gradient_unchecked (const HashVector &location, Eigen::Ref< DenseRealVector > target) override
 
void gradient_at_zero_unchecked (Eigen::Ref< DenseRealVector > target) override
 
void hessian_times_direction_unchecked (const HashVector &location, const DenseRealVector &direction, Eigen::Ref< DenseRealVector > target) override
 
void diag_preconditioner_unchecked (const HashVector &location, Eigen::Ref< DenseRealVector > target) override
 
void gradient_and_pre_conditioner_unchecked (const HashVector &location, Eigen::Ref< DenseRealVector > gradient, Eigen::Ref< DenseRealVector > pre) override
 
void project_to_line_unchecked (const HashVector &location, const DenseRealVector &direction) override
 
real_t value_from_xTw (const DenseRealVector &xTw)
 
virtual void calculate_loss (const DenseRealVector &scores, const BinaryLabelVector &labels, DenseRealVector &out) const =0
 
virtual void calculate_derivative (const DenseRealVector &scores, const BinaryLabelVector &labels, DenseRealVector &out) const =0
 
virtual void calculate_2nd_derivative (const DenseRealVector &scores, const BinaryLabelVector &labels, DenseRealVector &out) const =0
 
const DenseRealVectorcached_derivative (const HashVector &location)
 
const DenseRealVectorcached_2nd_derivative (const HashVector &location)
 
virtual real_t regularization_value (const DenseRealVector &weights) const =0
 
virtual void regularization_gradient (const DenseRealVector &weights, Eigen::Ref< DenseRealVector > gradient) const =0
 
virtual void regularization_gradient_at_zero (Eigen::Ref< DenseRealVector > gradient) const =0
 
virtual void regularization_preconditioner (const DenseRealVector &weights, Eigen::Ref< DenseRealVector > pre_cond) const =0
 
virtual void regularization_hessian (const DenseRealVector &weights, const DenseRealVector &direction, Eigen::Ref< DenseRealVector > pre_cond) const =0
 
void invalidate_labels ()
 

Private Attributes

std::shared_ptr< const GenericFeatureMatrixm_DenseFeatures
 Pointer to the dense part of the feature matrix. More...
 
std::shared_ptr< const GenericFeatureMatrixm_SparseFeatures
 pointer to the sparse part of the feature matrix More...
 
VectorHash m_Last_W {}
 cache for the last argument to x_times_w(). More...
 
DenseRealVector m_X_times_w
 cache for the last result of x_times_w() corresponding to m_Last_W. More...
 
DenseRealVector m_LsCache_xTd
 cache for line search implementation: feature times direction More...
 
DenseRealVector m_LsCache_xTw
 cache for line search implementation: feature times weights More...
 
DenseRealVector m_Costs
 Label-Dependent costs. More...
 
BinaryLabelVector m_Y
 Label vector – use a vector of ints here. We encode label present == 1, absent == -1. More...
 
CacheHelper m_DerivativeBuffer
 
CacheHelper m_SecondDerivativeBuffer
 
DenseRealVector m_LineStart
 
DenseRealVector m_LineDirection
 
DenseRealVector m_LineCache
 
DenseRealVector m_GenericInBuffer
 
DenseRealVector m_GenericOutBuffer
 

Detailed Description

Base class for implementationa of an objective that combines dense features and sparse features.

This is the shared code for all combined sparse/dense feature linear objectives.

Note
Unfortunately, I don't think that this can be implemented as a subclass of the Linear base, even though it models a linear classifier.

Definition at line 20 of file dense_and_sparse.h.

Constructor & Destructor Documentation

◆ DenseAndSparseLinearBase()

DenseAndSparseLinearBase::DenseAndSparseLinearBase ( std::shared_ptr< const GenericFeatureMatrix dense_features,
std::shared_ptr< const GenericFeatureMatrix sparse_features 
)

Member Function Documentation

◆ cached_2nd_derivative()

◆ cached_derivative()

const DenseRealVector & DenseAndSparseLinearBase::cached_derivative ( const HashVector location)
private

◆ calculate_2nd_derivative()

virtual void dismec::objective::DenseAndSparseLinearBase::calculate_2nd_derivative ( const DenseRealVector scores,
const BinaryLabelVector labels,
DenseRealVector out 
) const
privatepure virtual

◆ calculate_derivative()

virtual void dismec::objective::DenseAndSparseLinearBase::calculate_derivative ( const DenseRealVector scores,
const BinaryLabelVector labels,
DenseRealVector out 
) const
privatepure virtual

◆ calculate_loss()

virtual void dismec::objective::DenseAndSparseLinearBase::calculate_loss ( const DenseRealVector scores,
const BinaryLabelVector labels,
DenseRealVector out 
) const
privatepure virtual

◆ costs()

const DenseRealVector & DenseAndSparseLinearBase::costs ( ) const
protected

◆ declare_vector_on_last_line()

void dismec::objective::DenseAndSparseLinearBase::declare_vector_on_last_line ( const HashVector location,
real_t  t 
)
inlineoverrideprotectedvirtual

State that the given vector corresponds to a certain position on the line of the last line search.

This function is a pure optimization hint. It is used in the following scenario: If several computations need the product of weight vector w and feature matrix X, then we can compute this product only once and use a cached value for all later invocations. This can be done by comparing the vector hashes. However, as soon as a vector is modified, these hashes are invalidated. To do an efficient line search over ‘w’ = w + t d, we also cache the value ofX d, so thatX w' = X w + t X d. This function then declares that the vector given inlocationcorresponds tow + t d, wherewandd are the arguments passed to the last call ofproject_to_line()`.

Todo:
improve this interface, together with project_to_line, to be less error prone!

Reimplemented from dismec::objective::Objective.

Definition at line 75 of file dense_and_sparse.h.

References m_LsCache_xTd, m_LsCache_xTw, and update_xtw_cache().

◆ dense_features()

◆ diag_preconditioner_unchecked()

void DenseAndSparseLinearBase::diag_preconditioner_unchecked ( const HashVector location,
Eigen::Ref< DenseRealVector target 
)
overrideprivatevirtual

The function that does the actual computation. This is called in diag_preconditioner() after the arguments have been validated. The default implementation returns ones.

Reimplemented from dismec::objective::Objective.

Definition at line 189 of file dense_and_sparse.cpp.

References cached_2nd_derivative(), dense_features(), DENSE_PART, dismec::HashVector::get(), regularization_preconditioner(), sparse_features(), and SPARSE_PART.

◆ get_label_ref()

BinaryLabelVector & DenseAndSparseLinearBase::get_label_ref ( )

Definition at line 81 of file dense_and_sparse.cpp.

References invalidate_labels(), and m_Y.

◆ get_num_variables()

long DenseAndSparseLinearBase::get_num_variables ( ) const
protectednoexcept

actual implementation of num_variables(). We need this non-virtual function to be called during the constructor

Definition at line 47 of file dense_and_sparse.cpp.

References m_DenseFeatures, and m_SparseFeatures.

Referenced by num_variables().

◆ gradient_and_pre_conditioner_unchecked()

void DenseAndSparseLinearBase::gradient_and_pre_conditioner_unchecked ( const HashVector location,
Eigen::Ref< DenseRealVector gradient,
Eigen::Ref< DenseRealVector pre 
)
overrideprivatevirtual

The function that does the actual computation. This is called in gradient_and_pre_conditioner() after the arguments have been validated. The default implementation sucessively calls gradient() and diag_preconditioner().

Reimplemented from dismec::objective::Objective.

Definition at line 141 of file dense_and_sparse.cpp.

References cached_2nd_derivative(), cached_derivative(), dense_features(), DENSE_PART, dismec::HashVector::get(), dismec::objective::Objective::gradient(), regularization_gradient(), regularization_preconditioner(), sparse_features(), and SPARSE_PART.

◆ gradient_at_zero_unchecked()

void DenseAndSparseLinearBase::gradient_at_zero_unchecked ( Eigen::Ref< DenseRealVector target)
overrideprivatevirtual

The function that does the actual computation. This is called in gradient_at_zero() after the argument has been validated. The default implementation is rather inefficient and creates a new temporary zero vector.

Reimplemented from dismec::objective::Objective.

Definition at line 174 of file dense_and_sparse.cpp.

References calculate_derivative(), costs(), dense_features(), DENSE_PART, labels(), m_GenericInBuffer, m_GenericOutBuffer, regularization_gradient_at_zero(), sparse_features(), and SPARSE_PART.

◆ gradient_unchecked()

void DenseAndSparseLinearBase::gradient_unchecked ( const HashVector location,
Eigen::Ref< DenseRealVector target 
)
overrideprivatevirtual

The function that does the actual gradient computation. This is called in gradient() after the arguments have been validated.

Implements dismec::objective::Objective.

Definition at line 162 of file dense_and_sparse.cpp.

References cached_derivative(), dense_features(), DENSE_PART, dismec::HashVector::get(), regularization_gradient(), sparse_features(), and SPARSE_PART.

◆ hessian_times_direction_unchecked()

void DenseAndSparseLinearBase::hessian_times_direction_unchecked ( const HashVector location,
const DenseRealVector direction,
Eigen::Ref< DenseRealVector target 
)
overrideprivatevirtual

The function that does the actual computation. This is called in hessian_times_direction() after the arguments have been validated.

Implements dismec::objective::Objective.

Definition at line 126 of file dense_and_sparse.cpp.

References cached_2nd_derivative(), dense_features(), DENSE_PART, dismec::HashVector::get(), regularization_hessian(), sparse_features(), and SPARSE_PART.

◆ invalidate_labels()

void DenseAndSparseLinearBase::invalidate_labels ( )
private

This function will be called whenever m_Y changes so that derived classes can invalidate their caches.

Definition at line 217 of file dense_and_sparse.cpp.

References dismec::CacheHelper::invalidate(), m_DerivativeBuffer, and m_SecondDerivativeBuffer.

Referenced by get_label_ref().

◆ labels()

◆ line_interpolation()

auto dismec::objective::DenseAndSparseLinearBase::line_interpolation ( real_t  t) const
inlineprotected

Definition at line 71 of file dense_and_sparse.h.

References m_LsCache_xTd, and m_LsCache_xTw.

Referenced by lookup_on_line().

◆ lookup_on_line()

real_t DenseAndSparseLinearBase::lookup_on_line ( real_t  position)
overrideprivatevirtual

Looks up the value of the objective on the line defined by the last call to project_to_line().

Parameters
positionThe location where the objective is calculated.
Returns
The value of objective(location + position * direction), where location and direction are the vectors passed to the last call of project_to_line().
Attention
This function may use results cached project_to_line(), so it has to be called after a call to that function. A new call to project_to_line() will change the line which is evaluated.

Implements dismec::objective::Objective.

Definition at line 111 of file dense_and_sparse.cpp.

References line_interpolation(), m_GenericInBuffer, m_LineCache, m_LineDirection, m_LineStart, regularization_value(), and value_from_xTw().

◆ num_instances()

long DenseAndSparseLinearBase::num_instances ( ) const
noexcept

Definition at line 39 of file dense_and_sparse.cpp.

References m_DenseFeatures.

◆ num_variables()

long DenseAndSparseLinearBase::num_variables ( ) const
overridevirtualnoexcept

Gets the number of variables this objective expects. May return -1 if the objective is agnostic to the number of variables, e.g. for regularizers.

Implements dismec::objective::Objective.

Definition at line 43 of file dense_and_sparse.cpp.

References get_num_variables().

◆ project_linear_to_line()

void DenseAndSparseLinearBase::project_linear_to_line ( const HashVector location,
const DenseRealVector direction 
)
protected

Prepares the cache variables for line projection.

This function precomputes \( x^T d\) and \(x^T w\), so that for a line search parameter t we can get \( x^T (w + td) \) by simple linear combination, i.e. we can skip the matrix multiplication. This can then be used in the implementation of the line search lookup by calling the line_interpolation() function.

Parameters
locationThe origin point on the line.
directionThe direction of the line.

Definition at line 73 of file dense_and_sparse.cpp.

References dense_features(), DENSE_PART, dismec::HashVector::get(), m_LineDirection, m_LineStart, m_LsCache_xTd, m_LsCache_xTw, sparse_features(), SPARSE_PART, and x_times_w().

Referenced by project_to_line_unchecked().

◆ project_to_line_unchecked()

void DenseAndSparseLinearBase::project_to_line_unchecked ( const HashVector location,
const DenseRealVector direction 
)
overrideprivatevirtual

The function that does the actual computation. This is called in project_to_line() after the arguments have been validated.

Implements dismec::objective::Objective.

Definition at line 222 of file dense_and_sparse.cpp.

References project_linear_to_line().

◆ regularization_gradient()

virtual void dismec::objective::DenseAndSparseLinearBase::regularization_gradient ( const DenseRealVector weights,
Eigen::Ref< DenseRealVector gradient 
) const
privatepure virtual

◆ regularization_gradient_at_zero()

virtual void dismec::objective::DenseAndSparseLinearBase::regularization_gradient_at_zero ( Eigen::Ref< DenseRealVector gradient) const
privatepure virtual

◆ regularization_hessian()

virtual void dismec::objective::DenseAndSparseLinearBase::regularization_hessian ( const DenseRealVector weights,
const DenseRealVector direction,
Eigen::Ref< DenseRealVector pre_cond 
) const
privatepure virtual

◆ regularization_preconditioner()

virtual void dismec::objective::DenseAndSparseLinearBase::regularization_preconditioner ( const DenseRealVector weights,
Eigen::Ref< DenseRealVector pre_cond 
) const
privatepure virtual

◆ regularization_value()

virtual real_t dismec::objective::DenseAndSparseLinearBase::regularization_value ( const DenseRealVector weights) const
privatepure virtual

◆ sparse_features()

◆ update_costs()

void DenseAndSparseLinearBase::update_costs ( real_t  positive,
real_t  negative 
)

Definition at line 87 of file dense_and_sparse.cpp.

References labels(), m_Costs, and m_Y.

◆ update_features()

void DenseAndSparseLinearBase::update_features ( const DenseFeatures dense,
const SparseFeatures sparse 
)

Definition at line 226 of file dense_and_sparse.cpp.

References m_DenseFeatures, and m_SparseFeatures.

◆ update_xtw_cache()

template<class Derived >
void dismec::objective::DenseAndSparseLinearBase::update_xtw_cache ( const HashVector new_weight,
const Eigen::MatrixBase< Derived > &  new_result 
)
inlineprotected

Updates the cached value for x_times_w.

Parameters
new_weightThe new value of w.
new_resultThe value of x^T w.

Definition at line 52 of file dense_and_sparse.h.

References dismec::HashVector::hash(), m_Last_W, and m_X_times_w.

Referenced by declare_vector_on_last_line().

◆ value_from_xTw()

real_t DenseAndSparseLinearBase::value_from_xTw ( const DenseRealVector xTw)
private

Definition at line 118 of file dense_and_sparse.cpp.

References calculate_loss(), costs(), labels(), and m_GenericOutBuffer.

Referenced by lookup_on_line(), and value_unchecked().

◆ value_unchecked()

real_t DenseAndSparseLinearBase::value_unchecked ( const HashVector location)
overrideprivatevirtual

The function that does the actual value computation. This is called in value() after the argument has been validated.

Implements dismec::objective::Objective.

Definition at line 106 of file dense_and_sparse.cpp.

References dismec::HashVector::get(), regularization_value(), value_from_xTw(), and x_times_w().

◆ x_times_w()

const DenseRealVector & DenseAndSparseLinearBase::x_times_w ( const HashVector w)
protected

Calculates the vector of feature matrix times weights w

Parameters
wThe weight vector with which to multiply.

Consecutive calls to this function with the same argument w will return a reference to a cached result. However, calling with another value in between will invalidate the cache.

Returns
A reference to the cached result vector.

Definition at line 62 of file dense_and_sparse.cpp.

References dense_features(), DENSE_PART, dismec::HashVector::get(), dismec::HashVector::hash(), m_Last_W, m_X_times_w, dismec::stats::Tracked::make_timer(), sparse_features(), SPARSE_PART, and anonymous_namespace{dense_and_sparse.cpp}::STAT_PERF_MATMUL.

Referenced by cached_2nd_derivative(), cached_derivative(), project_linear_to_line(), and value_unchecked().

Member Data Documentation

◆ m_Costs

DenseRealVector dismec::objective::DenseAndSparseLinearBase::m_Costs
private

Label-Dependent costs.

Definition at line 144 of file dense_and_sparse.h.

Referenced by costs(), DenseAndSparseLinearBase(), and update_costs().

◆ m_DenseFeatures

std::shared_ptr<const GenericFeatureMatrix> dismec::objective::DenseAndSparseLinearBase::m_DenseFeatures
private

Pointer to the dense part of the feature matrix.

Definition at line 129 of file dense_and_sparse.h.

Referenced by dense_features(), DenseAndSparseLinearBase(), get_num_variables(), num_instances(), and update_features().

◆ m_DerivativeBuffer

CacheHelper dismec::objective::DenseAndSparseLinearBase::m_DerivativeBuffer
private

Definition at line 152 of file dense_and_sparse.h.

Referenced by cached_derivative(), and invalidate_labels().

◆ m_GenericInBuffer

DenseRealVector dismec::objective::DenseAndSparseLinearBase::m_GenericInBuffer
private

Definition at line 159 of file dense_and_sparse.h.

Referenced by gradient_at_zero_unchecked(), and lookup_on_line().

◆ m_GenericOutBuffer

DenseRealVector dismec::objective::DenseAndSparseLinearBase::m_GenericOutBuffer
private

Definition at line 160 of file dense_and_sparse.h.

Referenced by gradient_at_zero_unchecked(), and value_from_xTw().

◆ m_Last_W

VectorHash dismec::objective::DenseAndSparseLinearBase::m_Last_W {}
private

cache for the last argument to x_times_w().

Definition at line 134 of file dense_and_sparse.h.

Referenced by update_xtw_cache(), and x_times_w().

◆ m_LineCache

DenseRealVector dismec::objective::DenseAndSparseLinearBase::m_LineCache
private

Definition at line 157 of file dense_and_sparse.h.

Referenced by lookup_on_line().

◆ m_LineDirection

DenseRealVector dismec::objective::DenseAndSparseLinearBase::m_LineDirection
private

Definition at line 156 of file dense_and_sparse.h.

Referenced by lookup_on_line(), and project_linear_to_line().

◆ m_LineStart

DenseRealVector dismec::objective::DenseAndSparseLinearBase::m_LineStart
private

Definition at line 155 of file dense_and_sparse.h.

Referenced by lookup_on_line(), and project_linear_to_line().

◆ m_LsCache_xTd

DenseRealVector dismec::objective::DenseAndSparseLinearBase::m_LsCache_xTd
private

cache for line search implementation: feature times direction

Definition at line 139 of file dense_and_sparse.h.

Referenced by declare_vector_on_last_line(), line_interpolation(), and project_linear_to_line().

◆ m_LsCache_xTw

DenseRealVector dismec::objective::DenseAndSparseLinearBase::m_LsCache_xTw
private

cache for line search implementation: feature times weights

Definition at line 141 of file dense_and_sparse.h.

Referenced by declare_vector_on_last_line(), line_interpolation(), and project_linear_to_line().

◆ m_SecondDerivativeBuffer

CacheHelper dismec::objective::DenseAndSparseLinearBase::m_SecondDerivativeBuffer
private

Definition at line 153 of file dense_and_sparse.h.

Referenced by cached_2nd_derivative(), and invalidate_labels().

◆ m_SparseFeatures

std::shared_ptr<const GenericFeatureMatrix> dismec::objective::DenseAndSparseLinearBase::m_SparseFeatures
private

pointer to the sparse part of the feature matrix

Definition at line 131 of file dense_and_sparse.h.

Referenced by DenseAndSparseLinearBase(), get_num_variables(), sparse_features(), and update_features().

◆ m_X_times_w

DenseRealVector dismec::objective::DenseAndSparseLinearBase::m_X_times_w
private

cache for the last result of x_times_w() corresponding to m_Last_W.

Definition at line 136 of file dense_and_sparse.h.

Referenced by update_xtw_cache(), and x_times_w().

◆ m_Y

BinaryLabelVector dismec::objective::DenseAndSparseLinearBase::m_Y
private

Label vector – use a vector of ints here. We encode label present == 1, absent == -1.

Definition at line 147 of file dense_and_sparse.h.

Referenced by get_label_ref(), labels(), and update_costs().


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