6 #ifndef DISMEC_LINEAR_H
7 #define DISMEC_LINEAR_H
52 template<
class Derived>
123 template<
class Derived>
130 return static_cast<const Derived&
>(*this);
134 return static_cast<Derived&
>(*this);
154 derived().gradient_imp(location, target);
160 derived().gradient_at_zero_imp(target);
165 Eigen::Ref<DenseRealVector> target)
override {
166 m_Regularizer->hessian_times_direction(location, direction, target);
167 derived().hessian_times_direction_imp(location, direction, target);
172 derived().diag_preconditioner_imp(location, target);
176 Eigen::Ref<DenseRealVector> pre)
override {
An Eigen vector with versioning information, to implement simple caching of results.
VectorHash hash() const
Gets the unique id of this vector.
A unique identifier for a HashVector.
Base class for objectives that use a linear classifier.
const SparseFeatures & sparse_features() const
std::shared_ptr< const GenericFeatureMatrix > m_FeatureMatrix
DenseRealVector m_X_times_w
cache for the last result of x_times_w() corresponding to m_Last_W.
long num_instances() const noexcept
VectorHash m_Last_W
cache for the last argument to x_times_w().
long num_variables() const noexcept override
virtual void invalidate_labels()=0
LinearClassifierBase(std::shared_ptr< const GenericFeatureMatrix > X)
const DenseRealVector & costs() const
const DenseFeatures & dense_features() const
void update_xtw_cache(const HashVector &new_weight, const Eigen::MatrixBase< Derived > &new_result)
Updates the cached value for x_times_w.
DenseRealVector m_Costs
Label-Dependent costs.
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.
DenseRealVector m_LsCache_xTw
cache for line search implementation: feature times weights
BinaryLabelVector m_Y
Label vector – use a vector of ints here. We encode label present == 1, absent == -1.
void update_costs(real_t positive, real_t negative)
auto line_interpolation(real_t t) const
void project_linear_to_line(const HashVector &location, const DenseRealVector &direction)
Prepares the cache variables for line projection.
DenseRealVector m_LsCache_xTd
cache for line search implementation: feature times direction
BinaryLabelVector & get_label_ref()
const BinaryLabelVector & labels() const
const GenericFeatureMatrix & generic_features() const
const DenseRealVector & x_times_w(const HashVector &w)
Calculates the vector of feature matrix times weights w
Implementation helper for linear classifier derived classes.
void gradient_unchecked(const HashVector &location, Eigen::Ref< DenseRealVector > target) override
void diag_preconditioner_unchecked(const HashVector &location, Eigen::Ref< DenseRealVector > target) override
real_t value_unchecked(const HashVector &location) override
void hessian_times_direction_unchecked(const HashVector &location, const DenseRealVector &direction, Eigen::Ref< DenseRealVector > target) override
std::unique_ptr< Objective > m_Regularizer
Pointer to the regularizer.
LinearClassifierImpBase(std::shared_ptr< const GenericFeatureMatrix > X, std::unique_ptr< Objective > regularizer)
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
void gradient_at_zero_unchecked(Eigen::Ref< DenseRealVector > target) 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().
const Derived & derived() const
Class that models an optimization objective.
void gradient(const HashVector &location, Eigen::Ref< DenseRealVector > target)
Evaluate the gradient at location.
real_t value_from_xTw(const DenseRealVector &cost, const BinaryLabelVector &labels, const Eigen::DenseBase< Derived > &xTw)
types::DenseRowMajor< real_t > DenseFeatures
Dense Feature Matrix in Row Major format.
types::DenseVector< std::int8_t > BinaryLabelVector
Dense vector for storing binary labels.
types::DenseVector< real_t > DenseRealVector
Any dense, real values vector.
types::SparseRowMajor< real_t > SparseFeatures
Sparse Feature Matrix in Row Major format.
float real_t
The default type for floating point values.