DiSMEC++
|
Base class for implementationa of an objective that combines dense features and sparse features. More...
#include <dense_and_sparse.h>
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 |
BinaryLabelVector & | get_label_ref () |
void | update_costs (real_t positive, real_t negative) |
void | update_features (const DenseFeatures &dense, const SparseFeatures &sparse) |
![]() | |
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... | |
![]() | |
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< StatisticsCollection > | get_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 DenseRealVector & | x_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 DenseFeatures & | dense_features () const |
const SparseFeatures & | sparse_features () const |
const DenseRealVector & | costs () const |
const BinaryLabelVector & | labels () const |
![]() | |
~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 Attributes | |
std::shared_ptr< const GenericFeatureMatrix > | m_DenseFeatures |
Pointer to the dense part of the feature matrix. More... | |
std::shared_ptr< const GenericFeatureMatrix > | m_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 |
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.
Linear
base, even though it models a linear classifier. Definition at line 20 of file dense_and_sparse.h.
DenseAndSparseLinearBase::DenseAndSparseLinearBase | ( | std::shared_ptr< const GenericFeatureMatrix > | dense_features, |
std::shared_ptr< const GenericFeatureMatrix > | sparse_features | ||
) |
Definition at line 20 of file dense_and_sparse.cpp.
References ALWAYS_ASSERT_EQUAL, dismec::stats::Tracked::declare_stat(), m_Costs, m_DenseFeatures, m_SparseFeatures, and anonymous_namespace{dense_and_sparse.cpp}::STAT_PERF_MATMUL.
|
private |
Definition at line 209 of file dense_and_sparse.cpp.
References calculate_2nd_derivative(), costs(), labels(), m_SecondDerivativeBuffer, dismec::CacheHelper::update(), and x_times_w().
Referenced by diag_preconditioner_unchecked(), gradient_and_pre_conditioner_unchecked(), and hessian_times_direction_unchecked().
|
private |
Definition at line 201 of file dense_and_sparse.cpp.
References calculate_derivative(), costs(), labels(), m_DerivativeBuffer, dismec::CacheHelper::update(), and x_times_w().
Referenced by gradient_and_pre_conditioner_unchecked(), and gradient_unchecked().
|
privatepure virtual |
Implemented in dismec::objective::DenseAndSparseMargin< MarginFunction, SparseRegFunction, DenseRegFunction >.
Referenced by cached_2nd_derivative().
|
privatepure virtual |
Implemented in dismec::objective::DenseAndSparseMargin< MarginFunction, SparseRegFunction, DenseRegFunction >.
Referenced by cached_derivative(), and gradient_at_zero_unchecked().
|
privatepure virtual |
Implemented in dismec::objective::DenseAndSparseMargin< MarginFunction, SparseRegFunction, DenseRegFunction >.
Referenced by value_from_xTw().
|
protected |
Definition at line 98 of file dense_and_sparse.cpp.
References m_Costs.
Referenced by cached_2nd_derivative(), cached_derivative(), gradient_at_zero_unchecked(), and value_from_xTw().
|
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 of
X d, so that
X w' = X w + t X d. This function then declares that the vector given in
locationcorresponds to
w + t d, where
wand
d are the arguments passed to the last call of
project_to_line()`.
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().
|
protected |
Definition at line 51 of file dense_and_sparse.cpp.
References m_DenseFeatures.
Referenced by diag_preconditioner_unchecked(), gradient_and_pre_conditioner_unchecked(), gradient_at_zero_unchecked(), gradient_unchecked(), hessian_times_direction_unchecked(), project_linear_to_line(), dismec::objective::DenseAndSparseMargin< MarginFunction, SparseRegFunction, DenseRegFunction >::regularization_gradient(), dismec::objective::DenseAndSparseMargin< MarginFunction, SparseRegFunction, DenseRegFunction >::regularization_gradient_at_zero(), dismec::objective::DenseAndSparseMargin< MarginFunction, SparseRegFunction, DenseRegFunction >::regularization_hessian(), dismec::objective::DenseAndSparseMargin< MarginFunction, SparseRegFunction, DenseRegFunction >::regularization_preconditioner(), dismec::objective::DenseAndSparseMargin< MarginFunction, SparseRegFunction, DenseRegFunction >::regularization_value(), and x_times_w().
|
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.
BinaryLabelVector & DenseAndSparseLinearBase::get_label_ref | ( | ) |
Definition at line 81 of file dense_and_sparse.cpp.
References invalidate_labels(), and m_Y.
|
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().
|
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.
|
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.
|
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.
|
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.
|
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().
|
protected |
Definition at line 102 of file dense_and_sparse.cpp.
References m_Y.
Referenced by cached_2nd_derivative(), cached_derivative(), dismec::objective::DenseAndSparseMargin< MarginFunction, SparseRegFunction, DenseRegFunction >::calculate_2nd_derivative(), dismec::objective::DenseAndSparseMargin< MarginFunction, SparseRegFunction, DenseRegFunction >::calculate_derivative(), dismec::objective::DenseAndSparseMargin< MarginFunction, SparseRegFunction, DenseRegFunction >::calculate_loss(), gradient_at_zero_unchecked(), update_costs(), and value_from_xTw().
|
inlineprotected |
Definition at line 71 of file dense_and_sparse.h.
References m_LsCache_xTd, and m_LsCache_xTw.
Referenced by lookup_on_line().
Looks up the value of the objective on the line defined by the last call to project_to_line()
.
position | The location where the objective is calculated. |
objective(location + position * direction)
, where location
and direction
are the vectors passed to the last call of project_to_line()
. 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().
|
noexcept |
Definition at line 39 of file dense_and_sparse.cpp.
References m_DenseFeatures.
|
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().
|
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.
location | The origin point on the line. |
direction | The 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().
|
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().
|
privatepure virtual |
|
privatepure virtual |
Implemented in dismec::objective::DenseAndSparseMargin< MarginFunction, SparseRegFunction, DenseRegFunction >.
Referenced by gradient_at_zero_unchecked().
|
privatepure virtual |
|
privatepure virtual |
|
privatepure virtual |
Implemented in dismec::objective::DenseAndSparseMargin< MarginFunction, SparseRegFunction, DenseRegFunction >.
Referenced by lookup_on_line(), and value_unchecked().
|
protected |
Definition at line 55 of file dense_and_sparse.cpp.
References m_SparseFeatures.
Referenced by diag_preconditioner_unchecked(), gradient_and_pre_conditioner_unchecked(), gradient_at_zero_unchecked(), gradient_unchecked(), hessian_times_direction_unchecked(), project_linear_to_line(), dismec::objective::DenseAndSparseMargin< MarginFunction, SparseRegFunction, DenseRegFunction >::regularization_gradient(), dismec::objective::DenseAndSparseMargin< MarginFunction, SparseRegFunction, DenseRegFunction >::regularization_gradient_at_zero(), dismec::objective::DenseAndSparseMargin< MarginFunction, SparseRegFunction, DenseRegFunction >::regularization_hessian(), dismec::objective::DenseAndSparseMargin< MarginFunction, SparseRegFunction, DenseRegFunction >::regularization_preconditioner(), dismec::objective::DenseAndSparseMargin< MarginFunction, SparseRegFunction, DenseRegFunction >::regularization_value(), and x_times_w().
Definition at line 87 of file dense_and_sparse.cpp.
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.
|
inlineprotected |
Updates the cached value for x_times_w.
new_weight | The new value of w. |
new_result | The 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().
|
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().
|
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().
|
protected |
Calculates the vector of feature matrix times weights w
w | The 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.
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().
|
private |
Label-Dependent costs.
Definition at line 144 of file dense_and_sparse.h.
Referenced by costs(), DenseAndSparseLinearBase(), and update_costs().
|
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().
|
private |
Definition at line 152 of file dense_and_sparse.h.
Referenced by cached_derivative(), and invalidate_labels().
|
private |
Definition at line 159 of file dense_and_sparse.h.
Referenced by gradient_at_zero_unchecked(), and lookup_on_line().
|
private |
Definition at line 160 of file dense_and_sparse.h.
Referenced by gradient_at_zero_unchecked(), and value_from_xTw().
|
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().
|
private |
Definition at line 157 of file dense_and_sparse.h.
Referenced by lookup_on_line().
|
private |
Definition at line 156 of file dense_and_sparse.h.
Referenced by lookup_on_line(), and project_linear_to_line().
|
private |
Definition at line 155 of file dense_and_sparse.h.
Referenced by lookup_on_line(), and project_linear_to_line().
|
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().
|
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().
|
private |
Definition at line 153 of file dense_and_sparse.h.
Referenced by cached_2nd_derivative(), and invalidate_labels().
|
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().
|
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().
|
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().