DiSMEC++
reg_sq_hinge.h
Go to the documentation of this file.
1 // Copyright (c) 2021, Aalto University, developed by Erik Schultheis
2 // All rights reserved.
3 //
4 // SPDX-License-Identifier: MIT
5 
6 #ifndef DISMEC_REG_SQ_HINGE_H
7 #define DISMEC_REG_SQ_HINGE_H
8 
9 #include "objective.h"
10 #include "matrix_types.h"
11 #include "utils/hash_vector.h"
12 #include "linear.h"
13 
14 namespace dismec::objective {
15 
21  class Regularized_SquaredHingeSVC : public LinearClassifierImpBase<Regularized_SquaredHingeSVC> {
23  public:
24  explicit Regularized_SquaredHingeSVC(std::shared_ptr<const GenericFeatureMatrix> X, std::unique_ptr<Objective> regularizer);
25 
26  [[nodiscard]] const features_t& features() const;
27 
28  void gradient_imp(const HashVector& location, Eigen::Ref<DenseRealVector> target);
29  void gradient_at_zero_imp(Eigen::Ref<DenseRealVector> target);
30 
31  void hessian_times_direction_imp(const HashVector& location,
32  const DenseRealVector &direction,
33  Eigen::Ref<DenseRealVector> target);
34 
35  void diag_preconditioner_imp(const HashVector& location, Eigen::Ref<DenseRealVector> target);
36 
37  void gradient_and_pre_conditioner_imp(const HashVector& location, Eigen::Ref<DenseRealVector> gradient,
38  Eigen::Ref<DenseRealVector> pre);
39 
40  template<typename OtherDerived>
41  static real_t value_from_xTw(const DenseRealVector& cost, const BinaryLabelVector& labels, const Eigen::DenseBase<OtherDerived>& xTw)
42  {
43  real_t f = 0;
44  assert(xTw.size() == labels.size());
45  for(Eigen::Index i = 0; i < labels.size(); ++i) {
46  real_t label = labels.coeff(i);
47  real_t d = std::max(real_t{0}, real_t{1} - label * xTw.coeff(i));
48  real_t factor = cost.coeff(i);
49  f += factor * d * d;
50  }
51 
52  return f;
53  }
54  private:
55  void invalidate_labels() override;
56  void margin_error(const HashVector& w);
57 
59  // do not write to these directly, only `margin_error` is allowed to do that
60  std::vector<int> m_MVPos;
61  std::vector<real_t> m_MVVal;
62 
63  template<class T, class U>
64  void gradient_and_pre_conditioner_tpl(const HashVector&location, T&& gradient, U&& pre); // __attribute__((hot));
65  };
66 }
67 
68 #endif //DISMEC_REG_SQ_HINGE_H
An Eigen vector with versioning information, to implement simple caching of results.
Definition: hash_vector.h:43
A unique identifier for a HashVector.
Definition: hash_vector.h:118
const BinaryLabelVector & labels() const
Definition: linear.cpp:89
Implementation helper for linear classifier derived classes.
Definition: linear.h:124
void gradient(const HashVector &location, Eigen::Ref< DenseRealVector > target)
Evaluate the gradient at location.
Definition: objective.cpp:96
static real_t value_from_xTw(const DenseRealVector &cost, const BinaryLabelVector &labels, const Eigen::DenseBase< OtherDerived > &xTw)
Definition: reg_sq_hinge.h:41
void gradient_imp(const HashVector &location, Eigen::Ref< DenseRealVector > target)
void hessian_times_direction_imp(const HashVector &location, const DenseRealVector &direction, Eigen::Ref< DenseRealVector > target)
void gradient_and_pre_conditioner_tpl(const HashVector &location, T &&gradient, U &&pre)
void diag_preconditioner_imp(const HashVector &location, Eigen::Ref< DenseRealVector > target)
void gradient_and_pre_conditioner_imp(const HashVector &location, Eigen::Ref< DenseRealVector > gradient, Eigen::Ref< DenseRealVector > pre)
Regularized_SquaredHingeSVC(std::shared_ptr< const GenericFeatureMatrix > X, std::unique_ptr< Objective > regularizer)
void gradient_at_zero_imp(Eigen::Ref< DenseRealVector > target)
types::DenseVector< std::int8_t > BinaryLabelVector
Dense vector for storing binary labels.
Definition: matrix_types.h:68
types::DenseVector< real_t > DenseRealVector
Any dense, real values vector.
Definition: matrix_types.h:40
types::SparseRowMajor< real_t > SparseFeatures
Sparse Feature Matrix in Row Major format.
Definition: matrix_types.h:50
float real_t
The default type for floating point values.
Definition: config.h:17