31 real_t ll = location->coeff(location->size() - 1);
32 real_t ld = direction.coeff(direction.size() - 1);
68 if(x > m_Epsilon)
return x - m_Epsilon/2;
69 if(x < -m_Epsilon)
return -x - m_Epsilon/2;
70 return real_t{0.5} * x*x / m_Epsilon;
76 }
else if(x < -m_Epsilon) {
84 if(x > m_Epsilon)
return real_t{1.0} / x;
85 if(x < -m_Epsilon)
return -
real_t{1.0} / x;
86 return real_t{0.5} / m_Epsilon;
90 :
PointWiseRegularizer(scale, ignore_bias), m_Epsilon(epsilon), m_L1_Factor(1 - interp), m_L2_Factor(interp)
96 if(interp < 0 || interp > 1) {
97 THROW_EXCEPTION(std::invalid_argument,
"Interpolation needs to be in [0, 1]. Got {}", interp);
103 if(x > m_Epsilon)
return m_L1_Factor*(x - m_Epsilon/2) +
real_t{0.5} * m_L2_Factor * x2;
104 if(x < -m_Epsilon)
return m_L1_Factor*(-x - m_Epsilon/2) +
real_t{0.5} * m_L2_Factor * x2;
105 return real_t{0.5} * (m_L1_Factor / m_Epsilon + m_L2_Factor) * x2;
110 return m_L1_Factor + m_L2_Factor * x;
111 }
else if(x < -m_Epsilon) {
112 return -m_L1_Factor + m_L2_Factor * x;
114 return m_L1_Factor * x / m_Epsilon + m_L2_Factor * x;
119 if(x > m_Epsilon)
return m_L1_Factor / x + m_L2_Factor;
120 if(x < -m_Epsilon)
return -m_L1_Factor / x + m_L2_Factor;
121 return real_t{0.5} / m_Epsilon * m_L1_Factor + m_L2_Factor;
143 #ifndef DOCTEST_CONFIG_DISABLE
149 auto it = begin(values);
150 for(
int i = 0; i <
ssize(values); ++i) {
151 vec.coeffRef(i) = *it;
163 for(
real_t t : {-1.2f, 0.1f, 0.5f, 0.8f, 2.5f}) {
166 CHECK(predict == doctest::Approx(actual));
181 CHECK(no_bias.
value(hl) == doctest::Approx(reference));
186 full.
gradient(short_loc, short_target);
188 for(
int i = 0; i < 3; ++i) {
189 CHECK(target.coeff(i) == short_target.coeff(i));
191 CHECK(target.coeff(3) == 0);
194 target = DenseRealVector::Random(4);
196 for(
int i = 0; i < 3; ++i) {
197 CHECK(target.coeff(i) == short_target.coeff(i));
199 CHECK(target.coeff(3) == 0);
209 CHECK(reg.
value(hl) == 0.5*(1+4+9));
214 for(
int i = 0; i < target.size(); ++i) {
215 CHECK(loc.coeff(i) == target.coeff(i));
219 CHECK(target.squaredNorm() == 0);
224 for(
int i = 0; i < target.size(); ++i) {
225 CHECK(probe.coeff(i) == target.coeff(i));
230 for(
int i = 0; i < target.size(); ++i) {
231 CHECK(target.coeff(i) == 1.0);
236 bool ignore_bias =
false;
238 SUBCASE(
"ignore bias") {
242 SUBCASE(
"full weights") {
259 loc << 1, 5.0, -3.0, 0.0;
261 CHECK(absreg.
value(hl) == 9.0 - 1.5);
266 CHECK(grad.coeff(0) == 1.0);
267 CHECK(grad.coeff(1) == 1.0);
268 CHECK(grad.coeff(2) == -1.0);
269 CHECK(grad.coeff(3) == 0.0);
272 CHECK(grad.squaredNorm() == 0);
276 bool ignore_bias =
false;
278 SUBCASE(
"ignore bias") {
282 SUBCASE(
"full weights") {
296 #include "spdlog/spdlog.h"
303 loc << 1, 0.5, -3.0, 0.0;
306 auto check_vector_valued = [&](
auto&& f) {
313 CHECK(elastic.coeff(0) == doctest::Approx(l1.coeff(0) + l2.coeff(0)));
314 CHECK(elastic.coeff(1) == doctest::Approx(l1.coeff(1) + l2.coeff(1)));
315 CHECK(elastic.coeff(2) == doctest::Approx(l1.coeff(2) + l2.coeff(2)));
316 CHECK(elastic.coeff(3) == doctest::Approx(l1.coeff(3) + l2.coeff(3)));
323 SUBCASE(
"gradient") {
324 check_vector_valued([&](
auto&& ref,
auto&& vec){
325 ref.gradient(hl, vec);
329 SUBCASE(
"gradient_at_zero") {
330 check_vector_valued([&](
auto&& ref,
auto&& vec){
331 ref.gradient_at_zero(vec);
335 SUBCASE(
"hessian_times_direction") {
337 check_vector_valued([&](
auto&& ref,
auto&& vec){
338 ref.hessian_times_direction(hl, dir, vec);
349 bool ignore_bias =
false;
351 SUBCASE(
"ignore bias") {
355 SUBCASE(
"full weights") {
An Eigen vector with versioning information, to implement simple caching of results.
real_t point_wise_grad(real_t x) const
ElasticNetRegularizer(real_t epsilon, real_t scale, real_t interp, bool ignore_bias=false)
Constructor for a ElasticNet regularizer objective.
real_t point_wise_value(real_t x) const
real_t point_wise_quad(real_t x) const
This class implements a huber regularizer.
HuberRegularizer(real_t epsilon, real_t scale=1.0, bool ignore_bias=false)
Constructor for a Huber regularizer objective.
real_t point_wise_value(real_t x) const
real_t point_wise_quad(real_t x) const
real_t point_wise_grad(real_t x) const
Class that models an optimization objective.
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.
void gradient_at_zero(Eigen::Ref< DenseRealVector > target)
Gets the gradient for location zero.
void gradient(const HashVector &location, Eigen::Ref< DenseRealVector > target)
Evaluate the gradient at location.
virtual real_t lookup_on_line(real_t position)=0
Looks up the value of the objective on the line defined by the last call to project_to_line().
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 eva...
void diag_preconditioner(const HashVector &location, Eigen::Ref< DenseRealVector > target)
Get precondition to be used in CG optimization.
real_t value(const HashVector &location)
Evaluate the objective at the given location.
Base class for pointwise regularization functions.
bool dont_regularize_bias() const
real_t value_unchecked(const HashVector &location) override
real_t scale() const
Returns the common scale factor for the entire regularizer.
This class implements a squared norm (L2) regularizer. Thus f(x) = 0.5 |x|^2.
static real_t point_wise_quad(real_t x)
real_t lookup_on_line(real_t a) override
Looks up the value of the objective on the line defined by the last call to project_to_line().
static real_t point_wise_grad(real_t x)
SquaredNormRegularizer(real_t scale=1, bool ignore_bias=false)
static real_t point_wise_value(real_t x)
real_t value_unchecked(const HashVector &location) override
void project_to_line_unchecked(const HashVector &location, const DenseRealVector &direction) override
void verify_bias(objective::Objective &full, objective::Objective &no_bias)
void verify_line_search(objective::Objective ®)
DenseRealVector make_vec(std::initializer_list< real_t > values)
std::unique_ptr< Objective > make_regularizer(const SquaredNormConfig &config)
Main namespace in which all types, classes, and functions are defined.
constexpr auto ssize(const C &c) -> std::common_type_t< std::ptrdiff_t, std::make_signed_t< decltype(c.size())>>
signed size free function. Taken from https://en.cppreference.com/w/cpp/iterator/size
types::DenseVector< real_t > DenseRealVector
Any dense, real values vector.
float real_t
The default type for floating point values.
#define THROW_EXCEPTION(exception_type,...)