shark::AbstractLoss< LabelT, OutputT > Class Template Referenceabstract

Loss function interface. More...

#include <shark/ObjectiveFunctions/Loss/AbstractLoss.h>

+ Inheritance diagram for shark::AbstractLoss< LabelT, OutputT >:

Public Types

typedef OutputT OutputType
 
typedef LabelT LabelType
 
typedef RealMatrix MatrixType
 
typedef Batch< OutputType >::type BatchOutputType
 
typedef Batch< LabelType >::type BatchLabelType
 
typedef ConstProxyReference< LabelType const >::type ConstLabelReference
 Const references to LabelType. More...
 
typedef ConstProxyReference< OutputType const >::type ConstOutputReference
 Const references to OutputType. More...
 
- Public Types inherited from shark::AbstractCost< LabelT, OutputT >
enum  Feature { HAS_FIRST_DERIVATIVE = 1, HAS_SECOND_DERIVATIVE = 2, IS_LOSS_FUNCTION = 4 }
 list of features a cost function can have More...
 
typedef OutputT OutputType
 
typedef LabelT LabelType
 
typedef Batch< OutputType >::type BatchOutputType
 
typedef Batch< LabelType >::type BatchLabelType
 
typedef TypedFlags< FeatureFeatures
 
typedef TypedFeatureNotAvailableException< FeatureFeatureNotAvailableException
 

Public Member Functions

 AbstractLoss ()
 
virtual double eval (BatchLabelType const &target, BatchOutputType const &prediction) const =0
 evaluate the loss for a batch of targets and a prediction More...
 
virtual double eval (ConstLabelReference target, ConstOutputReference prediction) const
 evaluate the loss for a target and a prediction More...
 
virtual double evalDerivative (ConstLabelReference target, ConstOutputReference prediction, OutputType &gradient) const
 evaluate the loss and its derivative for a target and a prediction More...
 
virtual double evalDerivative (ConstLabelReference target, ConstOutputReference prediction, OutputType &gradient, MatrixType &hessian) const
 evaluate the loss and its first and second derivative for a target and a prediction More...
 
virtual double evalDerivative (BatchLabelType const &target, BatchOutputType const &prediction, BatchOutputType &gradient) const
 evaluate the loss and the derivative w.r.t. the prediction More...
 
double eval (Data< LabelType > const &targets, Data< OutputType > const &predictions) const
 
double operator() (LabelType const &target, OutputType const &prediction) const
 evaluate the loss for a target and a prediction More...
 
double operator() (BatchLabelType const &target, BatchOutputType const &prediction) const
 
- Public Member Functions inherited from shark::AbstractCost< LabelT, OutputT >
virtual ~AbstractCost ()
 
const Featuresfeatures () const
 
virtual void updateFeatures ()
 
bool hasFirstDerivative () const
 returns true when the first parameter derivative is implemented More...
 
bool isLossFunction () const
 returns true when the cost function is in fact a loss function More...
 
double operator() (Data< LabelType > const &targets, Data< OutputType > const &predictions) const
 
- Public Member Functions inherited from shark::INameable
virtual ~INameable ()
 
virtual std::string name () const
 returns the name of the object More...
 

Additional Inherited Members

- Protected Attributes inherited from shark::AbstractCost< LabelT, OutputT >
Features m_features
 

Detailed Description

template<class LabelT, class OutputT = LabelT>
class shark::AbstractLoss< LabelT, OutputT >

Loss function interface.

In statistics and machine learning, a loss function encodes the severity of getting a label wrong. This is am important special case of a cost function (see AbstractCost), where the cost is computed as the average loss over a set, also known as (empirical) risk.
It is generally agreed that loss values are non-negative, and that the loss of correct prediction is zero. This rule is not formally checked, but instead left to the various sub-classes.

Definition at line 59 of file AbstractLoss.h.

Member Typedef Documentation

◆ BatchLabelType

template<class LabelT, class OutputT = LabelT>
typedef Batch<LabelType>::type shark::AbstractLoss< LabelT, OutputT >::BatchLabelType

Definition at line 68 of file AbstractLoss.h.

◆ BatchOutputType

template<class LabelT, class OutputT = LabelT>
typedef Batch<OutputType>::type shark::AbstractLoss< LabelT, OutputT >::BatchOutputType

Definition at line 67 of file AbstractLoss.h.

◆ ConstLabelReference

template<class LabelT, class OutputT = LabelT>
typedef ConstProxyReference<LabelType const>::type shark::AbstractLoss< LabelT, OutputT >::ConstLabelReference

Const references to LabelType.

Definition at line 71 of file AbstractLoss.h.

◆ ConstOutputReference

template<class LabelT, class OutputT = LabelT>
typedef ConstProxyReference<OutputType const>::type shark::AbstractLoss< LabelT, OutputT >::ConstOutputReference

Const references to OutputType.

Definition at line 73 of file AbstractLoss.h.

◆ LabelType

template<class LabelT, class OutputT = LabelT>
typedef LabelT shark::AbstractLoss< LabelT, OutputT >::LabelType

Definition at line 64 of file AbstractLoss.h.

◆ MatrixType

template<class LabelT, class OutputT = LabelT>
typedef RealMatrix shark::AbstractLoss< LabelT, OutputT >::MatrixType

Definition at line 65 of file AbstractLoss.h.

◆ OutputType

template<class LabelT, class OutputT = LabelT>
typedef OutputT shark::AbstractLoss< LabelT, OutputT >::OutputType

Definition at line 63 of file AbstractLoss.h.

Constructor & Destructor Documentation

◆ AbstractLoss()

template<class LabelT, class OutputT = LabelT>
shark::AbstractLoss< LabelT, OutputT >::AbstractLoss ( )
inline

Definition at line 75 of file AbstractLoss.h.

Member Function Documentation

◆ eval() [1/3]

◆ eval() [2/3]

template<class LabelT, class OutputT = LabelT>
virtual double shark::AbstractLoss< LabelT, OutputT >::eval ( ConstLabelReference  target,
ConstOutputReference  prediction 
) const
inlinevirtual

evaluate the loss for a target and a prediction

Parameters
targettarget value
predictionprediction, typically made by a model

Reimplemented in shark::CrossEntropy.

Definition at line 89 of file AbstractLoss.h.

◆ eval() [3/3]

template<class LabelT, class OutputT = LabelT>
double shark::AbstractLoss< LabelT, OutputT >::eval ( Data< LabelType > const &  targets,
Data< OutputType > const &  predictions 
) const
inlinevirtual

from AbstractCost

Parameters
targetstarget values
predictionspredictions, typically made by a model

Implements shark::AbstractCost< LabelT, OutputT >.

Definition at line 166 of file AbstractLoss.h.

◆ evalDerivative() [1/3]

template<class LabelT, class OutputT = LabelT>
virtual double shark::AbstractLoss< LabelT, OutputT >::evalDerivative ( ConstLabelReference  target,
ConstOutputReference  prediction,
OutputType gradient 
) const
inlinevirtual

evaluate the loss and its derivative for a target and a prediction

Parameters
targettarget value
predictionprediction, typically made by a model
gradientthe gradient of the loss function with respect to the prediction

Reimplemented in shark::CrossEntropy.

Definition at line 102 of file AbstractLoss.h.

Referenced by shark::AbstractLoss< unsigned int, RealVector >::evalDerivative().

◆ evalDerivative() [2/3]

template<class LabelT, class OutputT = LabelT>
virtual double shark::AbstractLoss< LabelT, OutputT >::evalDerivative ( ConstLabelReference  target,
ConstOutputReference  prediction,
OutputType gradient,
MatrixType hessian 
) const
inlinevirtual

evaluate the loss and its first and second derivative for a target and a prediction

Parameters
targettarget value
predictionprediction, typically made by a model
gradientthe gradient of the loss function with respect to the prediction
hessianthe hessian of the loss function with respect to the prediction

Reimplemented in shark::CrossEntropy.

Definition at line 119 of file AbstractLoss.h.

◆ evalDerivative() [3/3]

template<class LabelT, class OutputT = LabelT>
virtual double shark::AbstractLoss< LabelT, OutputT >::evalDerivative ( BatchLabelType const &  target,
BatchOutputType const &  prediction,
BatchOutputType gradient 
) const
inlinevirtual

evaluate the loss and the derivative w.r.t. the prediction

The default implementations throws an exception. If you overwrite this method, don't forget to set the flag HAS_FIRST_DERIVATIVE.
Parameters
targettarget value
predictionprediction, typically made by a model
gradientthe gradient of the loss function with respect to the prediction

Reimplemented in shark::SquaredLoss< Sequence, Sequence >, shark::SquaredLoss< OutputType, unsigned int >, shark::HingeLoss, shark::SquaredHingeLoss, shark::TukeyBiweightLoss, shark::HuberLoss, shark::SquaredLoss< OutputType, LabelType >, shark::SquaredEpsilonHingeLoss, and shark::EpsilonHingeLoss.

Definition at line 136 of file AbstractLoss.h.

◆ operator()() [1/2]

template<class LabelT, class OutputT = LabelT>
double shark::AbstractLoss< LabelT, OutputT >::operator() ( LabelType const &  target,
OutputType const &  prediction 
) const
inline

evaluate the loss for a target and a prediction

convenience operator
Parameters
targettarget value
predictionprediction, typically made by a model

Definition at line 187 of file AbstractLoss.h.

◆ operator()() [2/2]

template<class LabelT, class OutputT = LabelT>
double shark::AbstractLoss< LabelT, OutputT >::operator() ( BatchLabelType const &  target,
BatchOutputType const &  prediction 
) const
inline

Definition at line 190 of file AbstractLoss.h.


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