tinyopt 700
Tinyopt, a lightweight, header only optimization library
Loading...
Searching...
No Matches
tinyopt::losses Namespace Reference

Classes

struct  HuberTag
struct  CauchyTag
struct  TukeyTag
struct  RobustResidual
struct  has_rows
struct  has_rows< T, std::void_t< decltype(std::declval< const T & >().rows())> >

Functions

template<typename T>
const T & UnwrapResidual (const T &residual)
template<typename ResidualT, typename LossTag>
const ResidualT & UnwrapResidual (const RobustResidual< ResidualT, LossTag > &residual)
double HuberWeight (double n2, double delta)
double HuberLossValue (double n2, double delta)
template<typename T>
auto Huber (const T &residual, double delta) -> std::enable_if_t<!std::is_arithmetic_v< std::decay_t< T > > &&!traits::is_pair_v< T > &&!traits::is_jet_type_v< std::decay_t< T > > &&(traits::is_matrix_or_array_v< std::decay_t< T > >||traits::is_robust_residual_v< std::decay_t< T > >), RobustResidual< std::decay_t< T >, HuberTag > >
template<typename T>
auto Cauchy (const T &residual, double delta) -> std::enable_if_t<!std::is_arithmetic_v< std::decay_t< T > > &&!traits::is_pair_v< T > &&!traits::is_jet_type_v< std::decay_t< T > > &&(traits::is_matrix_or_array_v< std::decay_t< T > >||traits::is_robust_residual_v< std::decay_t< T > >), RobustResidual< std::decay_t< T >, CauchyTag > >
template<typename T>
auto Tukey (const T &residual, double delta) -> std::enable_if_t<!std::is_arithmetic_v< std::decay_t< T > > &&!traits::is_pair_v< T > &&!traits::is_jet_type_v< std::decay_t< T > > &&(traits::is_matrix_or_array_v< std::decay_t< T > >||traits::is_robust_residual_v< std::decay_t< T > >), RobustResidual< std::decay_t< T >, TukeyTag > >
Activation losses
 DefineLoss (Sigmoid, 1.0/(1.0+exp(-x)), l.unaryExpr([](auto x) -> Scalar { return x *(Scalar(1.0) - x);}).reshaped().asDiagonal())
 Sigmoid = 1/(1+e^-x), derivative = Sigmoid(x) * (1 - Sigmoid(x)).
 DefineLoss (Tanh,(exp(x) - exp(-x))/(exp(x)+exp(-x)), l.unaryExpr([](auto x) -> Scalar { return Scalar(1.0) - x *x;}).reshaped().asDiagonal())
 Tanh = (e^x-e^-x)/(e^x+e^-x), derivative = 1 - Tanh(x)^2.
 DefineLoss (ReLU, x > 0 ? x :Scalar(0.0), x.unaryExpr([](auto x) { return x > 0 ? Scalar(1.0) :Scalar(0.0);}).reshaped().asDiagonal())
 ReLU = max(0, x), derivative = {x>0:1, x<=0:0}.
 DefineLoss2 (LeakyReLU, x > 0 ? x :a *x, x.unaryExpr([a](auto x) { return x > 0 ? 1 :a;}).reshaped().asDiagonal())
 LeakyReLU = {x>0:x, x<=0:a*x}, derivative = {x>0:1, x<=0:a}.
Classification losses
template<typename T, typename ExportJ = std::nullptr_t>
auto Softmax (const T &x, const ExportJ &Jx_or_bool=nullptr)
 Softmax = e^xi / sum(e^x), jacobian = {i=j: si(x)*(1-si(x)) , i!=j: -si(x)*sj(x)}.
template<typename T, typename ExportJ = std::nullptr_t>
auto SafeSoftmax (const T &x, const ExportJ &Jx_or_bool=nullptr)
 Safe Softmax = e^(xi-max(xi)) / sum(e^(x-max(xi)).
Mahalanobis Distances
template<typename T, typename Cov_t, typename ExportJ = std::nullptr_t>
auto MahaSquaredNorm (const T &x, const Cov_t &cov_or_var, const ExportJ &Jx_or_bool=nullptr, bool add_scale=true)
 Compute the Squared Mahalanobis distance of ´x´ with a covariance cov: n(x) = ||x||Σ²
template<typename T, typename Cov_t, typename ExportJ = std::nullptr_t>
auto MahaNorm (const T &x, const Cov_t &cov_or_var, const ExportJ &Jx_or_bool=nullptr)
 Compute the Mahalanobis distance of ´x´ with a covariance cov: n(x) = ||x||Σ
template<typename Derived, typename Cov_t, typename ExportJ = std::nullptr_t>
auto MahaWhitened (const MatrixBase< Derived > &res, const Cov_t &cov_stevs, const ExportJ &Jx_or_bool=nullptr)
template<typename Derived, typename DerivedC, typename ExportJ = std::nullptr_t>
auto MahaWhitenedInfoU (const MatrixBase< Derived > &res, const MatrixBase< DerivedC > &U, const ExportJ &Jx_or_bool=nullptr)
Classical Norms
template<typename T>
auto SquaredL2 (const T &x)
 Return the squared L2 norm of a vector or scalar (a.k.a Sum of Squares).
template<typename T, typename ExportJ>
auto SquaredL2 (const T &x, const ExportJ &Jx_or_bool, bool add_scale=true)
 Return the squared L2 norm of a vector or scalar and its jacobian (x.t()).
template<typename T>
auto L2 (const T &x)
 Return L2 norm of a vector or scalar.
template<typename T, typename ExportJ>
auto L2 (const T &x, const ExportJ &Jx_or_bool)
 Return L2 norm of a vector or scalar and its jacobian (x.t() / ||x||).
template<typename T>
auto L1 (const T &x)
 Return L1 norm of a vector or scalar.
template<typename T, typename ExportJ>
auto L1 (const T &x, const ExportJ &Jx_or_bool)
 Return L1 norm of a vector or scalar and its jacobian.
template<typename T>
auto Linf (const T &x)
 Return L infinity norm of a vector or scalar.
template<typename T, typename ExportJ>
auto Linf (const T &x, const ExportJ &Jx_or_bool)
 Return L infinity norm of a vector or scalar.
M-Estimators / Robust Losses

These functions return a loss and a scale to apply on the jacobian or the scaled jacobian

The loss's gradient is then s*res.t(), with 's' the scale returned by the robust function (e.g. Truncated, huber, Artcan, etc.), typically 1 if the ||res|| < threshold. The scale can then be used to solve the lossal equations: JtJ * dx = Jt*res*s Ex: Huber : s = {1, th/||x||} so that we thereby clip the gradient to a max of theshold 'th' while keeping the direction. grdaient = {inlier:Jt * res; outlier:Jt * res * th / ||res||}

template<typename T, typename ExportJ = std::nullptr_t>
auto Truncated (const T &n2, typename traits::params_trait< T >::Scalar th2, const ExportJ &Jx_or_bool=nullptr)
 Hard Truncation: Clip the given loss n to a max of th, also return the scale {0 1} or scaled jacobian if given as input Jx_or_bool.
template<typename T, typename ExportJ = std::nullptr_t>
auto TruncatedLoss (const T &x, typename traits::params_trait< T >::Scalar th2, const ExportJ &Jx_or_bool=nullptr)
 Hard Truncated L2 Loss: Clip the L2 loss of x to a max of th, also return the scale {0, 1} or scaled jacobian if given as input Jx_or_bool.
template<typename T, typename ExportJ = std::nullptr_t, std::enable_if_t<!traits::is_matrix_or_array_v< T > &&!traits::is_robust_residual_v< T >, int > = 0>
auto Huber (const T &n2, typename traits::params_trait< T >::Scalar th2, const ExportJ &Jx_or_bool=nullptr)
 Huber: Return a scaled loss $ n'= n if n < th, else n'= \sqrt(2.0 * th * n - th²) $, also return the scale {1, th/n} or scaled jacobian if given as input Jx_or_bool.
template<typename T, typename ExportJ = std::nullptr_t>
auto HuberLoss (const T &x, typename traits::params_trait< T >::Scalar th2, const ExportJ &Jx_or_bool=nullptr)
 Huber: Return a scaled loss of 'x' such that $ n'² = ||x||² if ||x||² < th2, else n'² = 2.0 * th * n - th² $, also return the scale {1, th/n} or scaled jacobian if given as input Jx_or_bool.
template<typename T, typename ExportJ = std::nullptr_t, std::enable_if_t<!traits::is_matrix_or_array_v< T > &&!traits::is_robust_residual_v< T >, int > = 0>
auto Tukey (const T &n2, typename traits::params_trait< T >::Scalar th2, const ExportJ &Jx_or_bool=nullptr)
 Tukey: return a scaled loss (without the /6) $ n'² = n² if n < th, else n'² = 2.0 * h * n - th² $, also return the scale or scaled jacobian if given as input Jx_or_bool.
template<typename T, typename ExportJ = std::nullptr_t>
auto TukeyLoss (const T &x, typename traits::params_trait< T >::Scalar th2, const ExportJ &Jx_or_bool=nullptr)
 Tukey: Return a scaled loss of 'x' such that $ n'² = ||x||² if ||x|| < th, else n'² = 2.0 * h * n - th² $, also return the scale or scaled jacobian if given as input Jx_or_bool.
template<typename T, typename ExportJ = std::nullptr_t>
auto Arctan (const T &n2, typename traits::params_trait< T >::Scalar th2, const ExportJ &Jx_or_bool=nullptr)
 Arctan: Return a scaled loss $ n'² = th * \atan(n² / th) $, also return the scale or scaled jacobian if given as input Jx_or_bool.
template<typename T, typename ExportJ = std::nullptr_t>
auto ArctanLoss (const T &x, typename traits::params_trait< T >::Scalar th2, const ExportJ &Jx_or_bool=nullptr)
 Arctan: Return a scaled loss of 'x' such that $ n'² = th * \atan(||x||² / th) $, also return the scale or scaled jacobian if given as input Jx_or_bool.
template<typename T, typename ExportJ = std::nullptr_t, std::enable_if_t<!traits::is_matrix_or_array_v< T > &&!traits::is_robust_residual_v< T >, int > = 0>
auto Cauchy (const T &n2, typename traits::params_trait< T >::Scalar th2, const ExportJ &Jx_or_bool=nullptr)
 Cauchy: Return a scaled loss $ n'² = th² * \log(1 + n² / th²) $, also return the scale or scaled jacobian if given as input Jx_or_bool.
template<typename T, typename ExportJ = std::nullptr_t>
auto CauchyLoss (const T &x, typename traits::params_trait< T >::Scalar th2, const ExportJ &Jx_or_bool=nullptr)
 Cauchy: Return a scaled loss of 'x' such that $ n'² = th² * \log(1 + ||x||² / th²) $, also return the scale or scaled jacobian if given as input Jx_or_bool.
template<typename T, typename ExportJ = std::nullptr_t>
auto GemanMcClure (const T &n2, typename traits::params_trait< T >::Scalar th2, const ExportJ &Jx_or_bool=nullptr)
 GemanMcClure: Return a scaled loss $ n'² = n² / (||x||² + th²) $, also return the scale or scaled jacobian if given as input Jx_or_bool.
template<typename T, typename ExportJ = std::nullptr_t>
auto GemanMcClureLoss (const T &x, typename traits::params_trait< T >::Scalar th2, const ExportJ &Jx_or_bool=nullptr)
 GemanMcClure: Return a scaled loss of 'x' such that $ n' = ||x|| / \sqrt(||x||² + th²) $, also return the scale or scaled jacobian if given as input Jx_or_bool.
template<typename T, typename ExportJ = std::nullptr_t>
auto BlakeZisserman (const T &n2, typename traits::params_trait< T >::Scalar th2, const ExportJ &Jx_or_bool=nullptr)
 BlakeZisserman: Return a scaled loss n' such that $ n'² = -\log(\exp(-n²) + \exp(-th²))  $, also return the scale or scaled jacobian if given as input Jx_or_bool.
template<typename T, typename ExportJ = std::nullptr_t>
auto BlakeZissermanLoss (const T &x, typename traits::params_trait< T >::Scalar th2, const ExportJ &Jx_or_bool=nullptr)
 BlakeZisserman: Return a scaled loss of 'x' such that $ n'² = -\log(\exp(-||x||²) + \exp(-th²))  $, also return the scale or scaled jacobian if given as input Jx_or_bool.

Detailed Description

If you land here, put your shades on because those fat functions are still hurting my eyes as of today.

Function Documentation

◆ HuberLoss()

template<typename T, typename ExportJ = std::nullptr_t>
auto tinyopt::losses::HuberLoss ( const T & x,
typename traits::params_trait< T >::Scalar th2,
const ExportJ & Jx_or_bool = nullptr )

Huber: Return a scaled loss of 'x' such that $ n'² = ||x||² if ||x||² < th2, else n'² = 2.0 * th * n - th² $, also return the scale {1, th/n} or scaled jacobian if given as input Jx_or_bool.

Note
The gradient is then {inlier:x.t(), outlier:x.t() * s}, with $s = th / ||x||$.

◆ MahaWhitened()

template<typename Derived, typename Cov_t, typename ExportJ = std::nullptr_t>
auto tinyopt::losses::MahaWhitened ( const MatrixBase< Derived > & res,
const Cov_t & cov_stevs,
const ExportJ & Jx_or_bool = nullptr )

Return the Whitened/Sphered/Decorrelated residuals (and its jacobian as a option) when applying a Mahalanobis norm when given residuals and a covariance matrix (Upper filled at least)

◆ MahaWhitenedInfoU()

template<typename Derived, typename DerivedC, typename ExportJ = std::nullptr_t>
auto tinyopt::losses::MahaWhitenedInfoU ( const MatrixBase< Derived > & res,
const MatrixBase< DerivedC > & U,
const ExportJ & Jx_or_bool = nullptr )

Return the Whitened/Sphered/Decorrelated residuals (and its jacobian as a option) when applying a Mahalanobis norm when given residuals and a Upper Information matrix