21 std::shared_ptr<const GenericFeatureMatrix> sparse_features) :
22 m_DenseFeatures( std::move(dense_features) ),
23 m_SparseFeatures( std::move(sparse_features) ),
24 m_X_times_w( m_DenseFeatures->rows() ),
25 m_LsCache_xTd( m_DenseFeatures->rows() ),
26 m_LsCache_xTw( m_DenseFeatures->rows() ),
27 m_Costs( m_DenseFeatures->rows() ),
28 m_Y( m_DenseFeatures->rows() ),
29 m_DerivativeBuffer(m_DenseFeatures->rows()), m_SecondDerivativeBuffer(m_DenseFeatures->rows()),
30 m_LineStart( get_num_variables() ), m_LineDirection( get_num_variables() ),
31 m_LineCache( get_num_variables() ), m_GenericInBuffer(m_DenseFeatures->rows()), m_GenericOutBuffer(m_DenseFeatures->rows())
59 #define DENSE_PART(source) source.head(dense_features().cols())
60 #define SPARSE_PART(source) source.tail(sparse_features().cols())
89 for(
int i = 0; i <
m_Costs.size(); ++i) {
90 if(
m_Y.coeff(i) == 1) {
127 Eigen::Ref<DenseRealVector> target) {
131 for (
int pos = 0; pos < hessian.size(); ++pos) {
132 if(
real_t h = hessian.coeff(pos); h != 0) {
142 Eigen::Ref<DenseRealVector> gradient,
143 Eigen::Ref<DenseRealVector> pre) {
149 for (
int pos = 0; pos < derivative.size(); ++pos) {
150 if(
real_t d = derivative.coeff(pos); d != 0) {
154 if(
real_t h = hessian.coeff(pos); h != 0) {
166 for (
int pos = 0; pos < derivative.size(); ++pos) {
167 if(
real_t d = derivative.coeff(pos); d != 0) {
180 const auto& cost_vector =
costs();
193 for (
int pos = 0; pos < hessian.size(); ++pos) {
194 if(
real_t h = hessian.coeff(pos); h != 0) {
203 out.resize(
labels().size());
205 out.array() *=
costs().array();
211 out.resize(
labels().size());
213 out.array() *=
costs().array();
234 return weight * weight;
238 return real_t{2} * weight;
248 std::shared_ptr<const GenericFeatureMatrix> dense_features,
249 real_t dense_reg_strength,
250 std::shared_ptr<const GenericFeatureMatrix> sparse_features,
251 real_t sparse_reg_strength) {
252 return std::make_unique<objective::DenseAndSparseMargin<SquaredHingePhi, L2Regularizer, L2Regularizer>>(
253 std::move(dense_features),
254 std::move(sparse_features),
255 SquaredHingePhi{}, L2Regularizer{}, dense_reg_strength, L2Regularizer{}, sparse_reg_strength);
282 std::make_shared<const GenericFeatureMatrix>(zero_dense),
283 std::make_shared<const GenericFeatureMatrix>(zero_sparse),
284 ZeroPhi{}, L2Regularizer{}, 1.0, L2Regularizer{}, 1.0);
289 CHECK(goal.value(hv) == 1.0 + 4.0 + 1.0 + 1.0 + 4.0 + 25.0);
293 goal.gradient(hv, out_grad);
294 CHECK(expected_grad == out_grad);
const DenseRealVector & update(const HashVector &input, F &&function)
An Eigen vector with versioning information, to implement simple caching of results.
VectorHash hash() const
Gets the unique id of this vector.
const DenseRealVector & get() const
Gets a constant reference to the data of this vector.
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
virtual void calculate_2nd_derivative(const DenseRealVector &scores, const BinaryLabelVector &labels, DenseRealVector &out) const =0
void update_features(const DenseFeatures &dense, const SparseFeatures &sparse)
BinaryLabelVector m_Y
Label vector – use a vector of ints here. We encode label present == 1, absent == -1.
virtual void calculate_derivative(const DenseRealVector &scores, const BinaryLabelVector &labels, DenseRealVector &out) const =0
const DenseRealVector & costs() const
std::shared_ptr< const GenericFeatureMatrix > m_DenseFeatures
Pointer to the dense part of the feature matrix.
void diag_preconditioner_unchecked(const HashVector &location, Eigen::Ref< DenseRealVector > target) override
BinaryLabelVector & get_label_ref()
void hessian_times_direction_unchecked(const HashVector &location, const DenseRealVector &direction, Eigen::Ref< DenseRealVector > target) override
real_t value_from_xTw(const DenseRealVector &xTw)
const DenseRealVector & cached_derivative(const HashVector &location)
void gradient_at_zero_unchecked(Eigen::Ref< DenseRealVector > target) override
const SparseFeatures & sparse_features() const
virtual void regularization_gradient(const DenseRealVector &weights, Eigen::Ref< DenseRealVector > gradient) const =0
virtual void regularization_preconditioner(const DenseRealVector &weights, Eigen::Ref< DenseRealVector > pre_cond) const =0
real_t value_unchecked(const HashVector &location) override
const DenseRealVector & cached_2nd_derivative(const HashVector &location)
DenseRealVector m_GenericInBuffer
void update_costs(real_t positive, real_t negative)
DenseRealVector m_LsCache_xTw
cache for line search implementation: feature times weights
virtual void regularization_gradient_at_zero(Eigen::Ref< DenseRealVector > gradient) const =0
CacheHelper m_DerivativeBuffer
const BinaryLabelVector & labels() const
virtual void calculate_loss(const DenseRealVector &scores, const BinaryLabelVector &labels, DenseRealVector &out) const =0
DenseRealVector m_LsCache_xTd
cache for line search implementation: feature times direction
long num_instances() const noexcept
std::shared_ptr< const GenericFeatureMatrix > m_SparseFeatures
pointer to the sparse part of the feature matrix
DenseRealVector m_Costs
Label-Dependent costs.
VectorHash m_Last_W
cache for the last argument to x_times_w().
virtual void regularization_hessian(const DenseRealVector &weights, const DenseRealVector &direction, Eigen::Ref< DenseRealVector > pre_cond) const =0
void project_linear_to_line(const HashVector &location, const DenseRealVector &direction)
Prepares the cache variables for line projection.
auto line_interpolation(real_t t) const
const DenseFeatures & dense_features() const
virtual real_t regularization_value(const DenseRealVector &weights) const =0
DenseRealVector m_LineStart
DenseRealVector m_LineDirection
const DenseRealVector & x_times_w(const HashVector &w)
Calculates the vector of feature matrix times weights w
long get_num_variables() const noexcept
actual implementation of num_variables(). We need this non-virtual function to be called during the c...
DenseRealVector m_LineCache
DenseAndSparseLinearBase(std::shared_ptr< const GenericFeatureMatrix > dense_features, std::shared_ptr< const GenericFeatureMatrix > sparse_features)
void gradient_unchecked(const HashVector &location, Eigen::Ref< DenseRealVector > target) override
DenseRealVector m_X_times_w
cache for the last result of x_times_w() corresponding to m_Last_W.
DenseRealVector m_GenericOutBuffer
long num_variables() const noexcept 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().
CacheHelper m_SecondDerivativeBuffer
void gradient(const HashVector &location, Eigen::Ref< DenseRealVector > target)
Evaluate the gradient at location.
auto make_timer(stat_id_t id, Args... args)
Creates a new ScopeTimer using stats::record_scope_time.
void declare_stat(stat_id_t index, StatisticMetaData meta)
Declares a new statistics. This function just forwards all its arguments to the internal StatisticsCo...
#define SPARSE_PART(source)
TEST_CASE("pure-regularization")
#define DENSE_PART(source)
constexpr const stat_id_t STAT_PERF_MATMUL
std::unique_ptr< DenseAndSparseLinearBase > make_sp_dense_squared_hinge(std::shared_ptr< const GenericFeatureMatrix > dense_features, real_t dense_reg_strength, std::shared_ptr< const GenericFeatureMatrix > sparse_features, real_t sparse_reg_strength)
opaque_int_type< detail::stat_id_tag > stat_id_t
An opaque int-like type that is used to identify a statistic in a StatisticsCollection.
Main namespace in which all types, classes, and functions are defined.
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.
real_t grad(real_t weight) const
real_t quad(real_t weight) const
real_t value(real_t weight) const
real_t grad(real_t margin) const
real_t value(real_t margin) const
real_t quad(real_t margin) const
#define ALWAYS_ASSERT_EQUAL(x, y, msg)