6 #ifndef DISMEC_GENERIC_LINEAR_H
7 #define DISMEC_GENERIC_LINEAR_H
38 Eigen::Ref<DenseRealVector> target)
override;
41 Eigen::Ref<DenseRealVector> target)
override;
44 Eigen::Ref<DenseRealVector>
gradient,
45 Eigen::Ref<DenseRealVector> pre)
override;
119 template<
class MarginFunction>
122 std::unique_ptr<Objective> regularizer,
124 Phi(std::move(phi)) {
131 assert(scores.size() ==
labels.size());
132 for(
int i = 0; i < scores.size(); ++i) {
134 out.coeffRef(i) =
Phi.value(margin);
141 assert(scores.size() ==
labels.size());
142 for(
int i = 0; i < scores.size(); ++i) {
144 real_t margin = scores.coeff(i) * label;
145 out.coeffRef(i) =
Phi.grad(margin) * label;
152 assert(scores.size() ==
labels.size());
153 for(
int i = 0; i < scores.size(); ++i) {
155 out.coeffRef(i) =
Phi.quad(margin);
163 std::unique_ptr<GenericLinearClassifier>
make_squared_hinge(std::shared_ptr<const GenericFeatureMatrix> X,
164 std::unique_ptr<Objective> regularizer);
166 std::unique_ptr<GenericLinearClassifier>
make_logistic_loss(std::shared_ptr<const GenericFeatureMatrix> X,
167 std::unique_ptr<Objective> regularizer);
169 std::unique_ptr<GenericLinearClassifier>
make_huber_hinge(std::shared_ptr<const GenericFeatureMatrix> X,
170 std::unique_ptr<Objective> regularizer,
real_t epsilon);
An Eigen vector with versioning information, to implement simple caching of results.
This is a non-templated, runtime-polymorphic generic implementation of the linear classifier objectiv...
std::unique_ptr< Objective > m_Regularizer
Pointer to the regularizer.
void invalidate_labels() override
void project_to_line_unchecked(const HashVector &location, const DenseRealVector &direction) override
void gradient_unchecked(const HashVector &location, Eigen::Ref< DenseRealVector > target) override
virtual void calculate_loss(const DenseRealVector &scores, const BinaryLabelVector &labels, DenseRealVector &out) const =0
Calculates the loss for each instance.
void hessian_times_direction_unchecked(const HashVector &location, const DenseRealVector &direction, Eigen::Ref< DenseRealVector > target) override
CacheHelper m_DerivativeBuffer
Cached value of the last calculation of the 2nd derivative. Needs to be invalidated when the labels c...
const DenseRealVector & cached_derivative(const HashVector &location)
Gets the derivative vector for the current location.
GenericLinearClassifier(std::shared_ptr< const GenericFeatureMatrix > X, std::unique_ptr< Objective > regularizer)
real_t value_from_xTw(const DenseRealVector &xTw)
void diag_preconditioner_unchecked(const HashVector &location, 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().
virtual void calculate_2nd_derivative(const DenseRealVector &scores, const BinaryLabelVector &labels, DenseRealVector &out) const =0
Calculates the 2nd derivative of the loss with respect to the scores for each instance.
real_t value_unchecked(const HashVector &location) override
void gradient_at_zero_unchecked(Eigen::Ref< DenseRealVector > target) override
DenseRealVector m_GenericOutBuffer
DenseRealVector m_GenericInBuffer
virtual void calculate_derivative(const DenseRealVector &scores, const BinaryLabelVector &labels, DenseRealVector &out) const =0
Calculates the derivative of the loss with respect to the scores for each instance.
CacheHelper m_SecondDerivativeBuffer
Cached value of the last calculation of the loss derivative. Needs to be invalidated when the labels ...
void gradient_and_pre_conditioner_unchecked(const HashVector &location, Eigen::Ref< DenseRealVector > gradient, Eigen::Ref< DenseRealVector > pre) override
const DenseRealVector & cached_2nd_derivative(const HashVector &location)
Gets the 2nd derivative vector for the current location.
Base class for objectives that use a linear classifier.
const BinaryLabelVector & labels() const
void gradient(const HashVector &location, Eigen::Ref< DenseRealVector > target)
Evaluate the gradient at location.
std::unique_ptr< GenericLinearClassifier > make_huber_hinge(std::shared_ptr< const GenericFeatureMatrix > X, std::unique_ptr< Objective > regularizer, real_t epsilon)
std::unique_ptr< GenericLinearClassifier > make_logistic_loss(std::shared_ptr< const GenericFeatureMatrix > X, std::unique_ptr< Objective > regularizer)
std::unique_ptr< GenericLinearClassifier > make_squared_hinge(std::shared_ptr< const GenericFeatureMatrix > X, std::unique_ptr< Objective > regularizer)
types::DenseVector< std::int8_t > BinaryLabelVector
Dense vector for storing binary labels.
types::DenseVector< real_t > DenseRealVector
Any dense, real values vector.
float real_t
The default type for floating point values.
A utility class template that, when instatiated with a MarginFunction, produces the corresponding lin...
void calculate_loss(const DenseRealVector &scores, const BinaryLabelVector &labels, DenseRealVector &out) const override
Calculates the loss for each instance.
void calculate_2nd_derivative(const DenseRealVector &scores, const BinaryLabelVector &labels, DenseRealVector &out) const override
Calculates the 2nd derivative of the loss with respect to the scores for each instance.
GenericMarginClassifier(std::shared_ptr< const GenericFeatureMatrix > X, std::unique_ptr< Objective > regularizer, MarginFunction phi)
void calculate_derivative(const DenseRealVector &scores, const BinaryLabelVector &labels, DenseRealVector &out) const override
Calculates the derivative of the loss with respect to the scores for each instance.