Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
generalized_loss.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
9
11#include "momentum/common/log.h"
12
13#include <cmath>
14
15namespace momentum {
16
17namespace {
18
19// L2 loss (alpha = 2):
20// f(s) = s / c^2
21// f'(s) = 1 / c^2
22// Note: the canonical Barron form has a 1/2 factor; it is intentionally omitted here so this
23// helper can be reused inside the other closed forms (L1/Cauchy/Welsch) without re-multiplying.
24template <typename T>
25T L2Loss(const T& sqrError, const T& invC2) {
26 return sqrError * invC2;
27}
28
29template <typename T>
30T derivL2Loss(const T& /* sqrError */, const T& invC2) {
31 return invC2;
32}
33
34// L1 / Pseudo-Huber loss (alpha = 1):
35// f(s) = sqrt(s/c^2 + 1) - 1
36// f'(s) = (1/2) * (1/c^2) / sqrt(s/c^2 + 1)
37// The "-1" offset is kept (rather than dropped as a constant) so that f(0) = 0 and the value
38// itself is interpretable as a loss magnitude.
39template <typename T>
40T L1Loss(const T& sqrError, const T& invC2) {
41 return std::sqrt(L2Loss(sqrError, invC2) + T(1)) - T(1);
42}
43
44template <typename T>
45T derivL1Loss(const T& sqrError, const T& invC2) {
46 return T(0.5) * invC2 / std::sqrt(L2Loss(sqrError, invC2) + T(1));
47}
48
49// Cauchy / Lorentzian loss (alpha = 0, the limit):
50// f(s) = log(1 + (1/2) * s/c^2)
51// f'(s) = 1 / (s + 2*c^2) = (1/c^2) / (s/c^2 + 2)
52// The argument to log is always >= 1 for sqrError >= 0, so log is well-defined.
53template <typename T>
54T CauchyLoss(const T& sqrError, const T& invC2) {
55 return std::log(T(0.5) * L2Loss(sqrError, invC2) + T(1));
56}
57
58template <typename T>
59T derivCauchyLoss(const T& sqrError, const T& invC2) {
60 return invC2 / (invC2 * sqrError + T(2));
61}
62
63// Welsch / Leclerc loss (alpha -> -inf, the limit):
64// f(s) = 1 - exp(-(1/2) * s/c^2)
65// f'(s) = (1/2) * (1/c^2) * exp(-(1/2) * s/c^2)
66// Bounded in [0, 1); derivative decays to 0 for large residuals — outliers contribute essentially
67// nothing to the gradient. exp argument is <= 0, so no overflow.
68template <typename T>
69T WelschLoss(const T& sqrError, const T& invC2) {
70 return T(1) - std::exp(T(-0.5) * L2Loss(sqrError, invC2));
71}
72
73template <typename T>
74T derivWelschLoss(const T& sqrError, const T& invC2) {
75 return T(0.5) * invC2 * std::exp(T(-0.5) * L2Loss(sqrError, invC2));
76}
77
78} // namespace
79
80template <typename T>
81GeneralizedLossT<T>::GeneralizedLossT(const T& a, const T& c) : alpha_(a), invC2_(T(1) / (c * c)) {
82 MT_CHECK(c > 0, "Parameter c should be positive but received {}", c);
83
84 // Snap alpha to a closed-form branch when it is within kEps of a special value, both for
85 // numerical stability (the General formula has a removable singularity at alpha=2 from the
86 // |alpha - 2| factor, and a 0/0 form at alpha=0) and for speed (closed forms avoid pow/log).
87 // The Welsch branch fires only for the exact kWelsch sentinel; finite negative alphas such as -2
88 // (Geman-McClure) fall through to LossType::General and are evaluated with the full Barron
89 // formula.
90 if (alpha_ >= kL2 - kEps && alpha_ <= kL2 + kEps) {
92 } else if (alpha_ >= kL1 - kEps && alpha_ <= kL1 + kEps) {
94 } else if (alpha_ >= kCauchy - kEps && alpha_ <= kCauchy + kEps) {
96 } else if (alpha_ == kWelsch) {
98 } else {
100 }
101}
102
103template <typename T>
104T GeneralizedLossT<T>::value(const T& sqrError) const {
105 switch (lossType_) {
106 case LossType::L2:
107 return L2Loss(sqrError, invC2_);
108 case LossType::L1:
109 return L1Loss(sqrError, invC2_);
110 case LossType::Cauchy:
111 return CauchyLoss(sqrError, invC2_);
112 case LossType::Welsch:
113 return WelschLoss(sqrError, invC2_);
115 // Barron's general form (eq. 1 of arXiv:1701.03077), valid for finite alpha not in
116 // {2, 0}; the |alpha - 2| / alpha factor recovers the L2/Cauchy/Welsch limits as
117 // alpha -> 2, 0, -inf respectively. Slower than the closed forms above due to pow().
118 // f(s) = (|alpha-2| / alpha) * ( (s/(c^2 * |alpha-2|) + 1)^(alpha/2) - 1 )
119 // Singularities at alpha == 2 and alpha == 0 are avoided by the kEps snap in the ctor.
120 return (std::pow(L2Loss(sqrError, invC2_) / std::abs(alpha_ - T(2)) + T(1), T(0.5) * alpha_) -
121 T(1)) *
122 std::abs(alpha_ - T(2)) / alpha_;
123 default: {
124 MT_LOGE("Invalid lossType_ ({})", static_cast<int>(lossType_));
125 return {};
126 }
127 }
128}
129
130template <typename T>
131T GeneralizedLossT<T>::deriv(const T& sqrError) const {
132 switch (lossType_) {
133 case LossType::L2:
134 return derivL2Loss(sqrError, invC2_);
135 case LossType::L1:
136 return derivL1Loss(sqrError, invC2_);
137 case LossType::Cauchy:
138 return derivCauchyLoss(sqrError, invC2_);
139 case LossType::Welsch:
140 return derivWelschLoss(sqrError, invC2_);
142 // Derivative of Barron's general form w.r.t. s (the squared error):
143 // f'(s) = (1 / (2 c^2)) * ( s / (c^2 * |alpha-2|) + 1 )^(alpha/2 - 1)
144 // The |alpha-2| / alpha prefactor in f(s) cancels under differentiation, so it does not
145 // appear here. Slower than the closed forms above due to pow().
146 return T(0.5) * invC2_ *
147 std::pow(
148 L2Loss(sqrError, invC2_) / std::abs(alpha_ - T(2)) + T(1), T(0.5) * alpha_ - T(1));
149 default: {
150 MT_LOGE("Invalid lossType_ ({})", static_cast<int>(lossType_));
151 return {};
152 }
153 }
154}
155
156template class GeneralizedLossT<float>;
157template class GeneralizedLossT<double>;
158
159} // namespace momentum
#define MT_CHECK(condition,...)
Runtime check macros with two interchangeable backends:
Definition checks.h:36
Implementation of "A General and Adaptive Robust Loss Function" (https://arxiv.org/abs/1701....
T value(const T &sqrError) const
Returns the loss value for a given squared error.
T deriv(const T &sqrError) const
Derivative of the loss with respect to the input squared error.
static constexpr T kWelsch
Sentinel for the Welsch (alpha -> -inf) limit.
static constexpr T kEps
Small threshold used to detect proximity to special-case alpha values.
GeneralizedLossT(const T &a=kL2, const T &c=T(1))
Constructs a generalized loss with shape a and scale c.
const T invC2_
Stored as 1/c^2 (instead of c) as a small optimization, since the loss formulas use this quantity dir...
static constexpr T kL2
Special-case alpha values that are either at a singularity in the general formula or simplify to a cl...
#define MT_LOGE(...)
Definition log.h:181