Momentum
A library for solving nonlinear optimization problems related to human body kinematics
Loading...
Searching...
No Matches
momentum::GeneralizedLossT< T > Class Template Reference

Implementation of "A General and Adaptive Robust Loss Function" (https://arxiv.org/abs/1701.03077). More...

#include <generalized_loss.h>

Inheritance diagram for momentum::GeneralizedLossT< T >:

Public Member Functions

 GeneralizedLossT (const T &a=kL2, const T &c=T(1))
 Constructs a generalized loss with shape a and scale c.
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.
bool isL2 () const
 Returns true if this loss is an L2 loss (alpha ~= 2).
T invC2 () const
 Returns the stored 1/c^2 value. For default c=1, this is 1.0.

Static Public Attributes

static constexpr T kL2 = T(2)
 Special-case alpha values that are either at a singularity in the general formula or simplify to a closed-form computation.
static constexpr T kL1 = T(1)
static constexpr T kCauchy = T(0)
static constexpr T kWelsch = std::numeric_limits<T>::lowest()
 Sentinel for the Welsch (alpha -> -inf) limit.

Protected Types

enum class  LossType {
  L1 , L2 , Cauchy , Welsch ,
  General
}

Protected Attributes

const T alpha_
const T invC2_
 Stored as 1/c^2 (instead of c) as a small optimization, since the loss formulas use this quantity directly.
LossType lossType_

Static Protected Attributes

static constexpr T kEps = 1e-9
 Small threshold used to detect proximity to special-case alpha values.

Detailed Description

template<typename T>
class momentum::GeneralizedLossT< T >

Implementation of "A General and Adaptive Robust Loss Function" (https://arxiv.org/abs/1701.03077).

This loss transforms a squared error value with parameters alpha and c. alpha can be approximately thought of as the norm: alpha=2 is the squared L2 norm loss; alpha=1 is a soft L1 norm or the L2-L1/pseudo-Huber loss. alpha controls the effect of "outliers" (i.e., "robust"). Smaller alpha reduces the contribution of large errors so they don't affect the result as much. c scales the gradient with respect to the squared error term inversely.

This does not implement solving for alpha. It assumes alpha and c are given as input.

Alpha value reference
  • alpha = 2.0 (kL2): L2 (quadratic). Standard least-squares. Fastest convergence, sensitive to outliers.
  • alpha = 1.0 (kL1): Pseudo-Huber. Smooth approximation to Huber. Robust to moderate outliers.
  • alpha = 0.0 (kCauchy): Cauchy (Lorentzian). Very robust to outliers. Much slower convergence.
  • alpha = -2.0: Geman-McClure. Extremely robust. Bounded influence — large outliers are essentially ignored.
  • alpha = kWelsch: Welsch. Negative-infinity limit case. Maximum robustness, slowest convergence.

The c parameter controls the transition scale: residuals << c behave like L2, residuals >> c are downweighted according to the loss shape.

In the Gauss-Newton solver, the loss function modifies both the residual and Jacobian via the chain rule. The SimdGeneralizedLoss class handles this vectorized computation for SIMD error functions. Generic error functions apply the loss via the JointErrorFunctionT base class automatically — subclasses implement evalFunction() with raw residuals.

Definition at line 46 of file generalized_loss.h.

Member Enumeration Documentation

◆ LossType

template<typename T>
enum class momentum::GeneralizedLossT::LossType
strongprotected
Enumerator
L1 
L2 
Cauchy 
Welsch 
General 

Definition at line 92 of file generalized_loss.h.

Constructor & Destructor Documentation

◆ GeneralizedLossT()

template<typename T>
momentum::GeneralizedLossT< T >::GeneralizedLossT ( const T & a = kL2,
const T & c = T(1) )

Constructs a generalized loss with shape a and scale c.

Parameters
aShape parameter alpha. Theoretically unbounded, but values with very large magnitude (e.g., > 100) are numerically unstable and not needed in practice. Use kWelsch to select the Welsch negative-infinity limit.
cScale parameter. Must be > 0. Very small values are unstable because the gradient scales with 1/c^2.
Note
Internal calculations involving log/exp are performed in double precision for numerical stability.

Definition at line 81 of file generalized_loss.cpp.

References alpha_, Cauchy, General, invC2_, kCauchy, kEps, kL1, kL2, kWelsch, L1, L2, lossType_, MT_CHECK, and Welsch.

Member Function Documentation

◆ deriv()

template<typename T>
T momentum::GeneralizedLossT< T >::deriv ( const T & sqrError) const
nodiscard

Derivative of the loss with respect to the input squared error.

This effectively scales the gradient of what's being squared.

Definition at line 131 of file generalized_loss.cpp.

References alpha_, Cauchy, General, invC2_, L1, L2, lossType_, MT_LOGE, and Welsch.

◆ invC2()

template<typename T>
T momentum::GeneralizedLossT< T >::invC2 ( ) const
inlinenodiscard

Returns the stored 1/c^2 value. For default c=1, this is 1.0.

Definition at line 87 of file generalized_loss.h.

◆ isL2()

template<typename T>
bool momentum::GeneralizedLossT< T >::isL2 ( ) const
inlinenodiscard

Returns true if this loss is an L2 loss (alpha ~= 2).

Useful for fast-path optimizations that bypass value()/deriv() calls.

Definition at line 82 of file generalized_loss.h.

◆ value()

template<typename T>
T momentum::GeneralizedLossT< T >::value ( const T & sqrError) const
nodiscard

Returns the loss value for a given squared error.

Parameters
sqrErrorNon-negative squared residual.

Definition at line 104 of file generalized_loss.cpp.

References alpha_, Cauchy, General, invC2_, L1, L2, lossType_, MT_LOGE, and Welsch.

Member Data Documentation

◆ alpha_

template<typename T>
const T momentum::GeneralizedLossT< T >::alpha_
protected

◆ invC2_

template<typename T>
const T momentum::GeneralizedLossT< T >::invC2_
protected

Stored as 1/c^2 (instead of c) as a small optimization, since the loss formulas use this quantity directly.

Definition at line 107 of file generalized_loss.h.

Referenced by deriv(), momentum::SimdGeneralizedLossT< T >::deriv(), GeneralizedLossT(), value(), and momentum::SimdGeneralizedLossT< T >::value().

◆ kCauchy

template<typename T>
T momentum::GeneralizedLossT< T >::kCauchy = T(0)
staticconstexpr

Definition at line 52 of file generalized_loss.h.

Referenced by GeneralizedLossT().

◆ kEps

template<typename T>
T momentum::GeneralizedLossT< T >::kEps = 1e-9
staticconstexprprotected

Small threshold used to detect proximity to special-case alpha values.

Definition at line 101 of file generalized_loss.h.

Referenced by GeneralizedLossT().

◆ kL1

template<typename T>
T momentum::GeneralizedLossT< T >::kL1 = T(1)
staticconstexpr

Definition at line 51 of file generalized_loss.h.

Referenced by GeneralizedLossT().

◆ kL2

template<typename T>
T momentum::GeneralizedLossT< T >::kL2 = T(2)
staticconstexpr

Special-case alpha values that are either at a singularity in the general formula or simplify to a closed-form computation.

Definition at line 50 of file generalized_loss.h.

Referenced by momentum::AimDirErrorFunctionT< float >::AimDirErrorFunctionT(), momentum::AimDirErrorFunctionT< float >::AimDirErrorFunctionT(), momentum::AimDistErrorFunctionT< float >::AimDistErrorFunctionT(), momentum::AimDistErrorFunctionT< float >::AimDistErrorFunctionT(), momentum::FixedAxisAngleErrorFunctionT< float >::FixedAxisAngleErrorFunctionT(), momentum::FixedAxisAngleErrorFunctionT< float >::FixedAxisAngleErrorFunctionT(), momentum::FixedAxisCosErrorFunctionT< float >::FixedAxisCosErrorFunctionT(), momentum::FixedAxisCosErrorFunctionT< float >::FixedAxisCosErrorFunctionT(), momentum::FixedAxisDiffErrorFunctionT< float >::FixedAxisDiffErrorFunctionT(), momentum::FixedAxisDiffErrorFunctionT< float >::FixedAxisDiffErrorFunctionT(), GeneralizedLossT(), momentum::JointErrorFunctionT< T, Data, FuncDim, NumVec, NumPos >::JointErrorFunctionT(), momentum::LimitErrorFunctionT< float >::LimitErrorFunctionT(), momentum::LimitErrorFunctionT< float >::LimitErrorFunctionT(), momentum::LimitErrorFunctionT< float >::LimitErrorFunctionT(), momentum::NormalErrorFunctionT< float >::NormalErrorFunctionT(), momentum::NormalErrorFunctionT< float >::NormalErrorFunctionT(), momentum::OrientationErrorFunctionT< float >::OrientationErrorFunctionT(), momentum::OrientationErrorFunctionT< float >::OrientationErrorFunctionT(), momentum::OrientationRotDiffErrorFunctionT< float >::OrientationRotDiffErrorFunctionT(), momentum::OrientationRotDiffErrorFunctionT< float >::OrientationRotDiffErrorFunctionT(), momentum::PlaneErrorFunctionT< float >::PlaneErrorFunctionT(), momentum::PlaneErrorFunctionT< float >::PlaneErrorFunctionT(), momentum::PositionErrorFunctionT< float >::PositionErrorFunctionT(), momentum::PositionErrorFunctionT< float >::PositionErrorFunctionT(), momentum::VertexErrorFunctionT< T, VertexNormalDataT< T >, 1 >::VertexErrorFunctionT(), momentum::VertexNormalErrorFunctionT< float >::VertexNormalErrorFunctionT(), momentum::VertexPlaneErrorFunctionT< float >::VertexPlaneErrorFunctionT(), momentum::VertexPositionErrorFunctionT< float >::VertexPositionErrorFunctionT(), and momentum::VertexProjectionErrorFunctionT< float >::VertexProjectionErrorFunctionT().

◆ kWelsch

template<typename T>
T momentum::GeneralizedLossT< T >::kWelsch = std::numeric_limits<T>::lowest()
staticconstexpr

Sentinel for the Welsch (alpha -> -inf) limit.

Pass this as alpha to opt into the Welsch closed form. This uses the finite lowest value instead of an infinity literal so fast-math builds that disable infinities can still compile the public header. Other finite negative alphas (e.g. -2 for Geman-McClure) are evaluated by the LossType::General branch and are NOT silently rerouted to Welsch.

Definition at line 58 of file generalized_loss.h.

Referenced by GeneralizedLossT().

◆ lossType_


The documentation for this class was generated from the following files: