12 #ifndef MLPACK_METHODS_ANN_LOSS_FUNCTION_HUBER_LOSS_IMPL_HPP 13 #define MLPACK_METHODS_ANN_LOSS_FUNCTION_HUBER_LOSS_IMPL_HPP 21 template<
typename InputDataType,
typename OutputDataType>
31 template<
typename InputDataType,
typename OutputDataType>
32 template<
typename PredictionType,
typename TargetType>
33 typename PredictionType::elem_type
35 const PredictionType& prediction,
36 const TargetType& target)
38 typedef typename PredictionType::elem_type ElemType;
40 for (
size_t i = 0; i < prediction.n_elem; ++i)
42 const ElemType absError = std::abs(target[i] - prediction[i]);
43 loss += absError > delta ?
44 delta * (absError - 0.5 * delta) : 0.5 * std::pow(absError, 2);
46 return mean ? loss / prediction.n_elem : loss;
49 template<
typename InputDataType,
typename OutputDataType>
50 template<
typename PredictionType,
typename TargetType,
typename LossType>
52 const PredictionType& prediction,
53 const TargetType& target,
56 typedef typename PredictionType::elem_type ElemType;
58 loss.set_size(size(prediction));
59 for (
size_t i = 0; i < loss.n_elem; ++i)
61 const ElemType absError = std::abs(target[i] - prediction[i]);
62 loss[i] = absError > delta ?
63 -delta * (target[i] - prediction[i]) / absError :
64 prediction[i] - target[i];
66 loss[i] /= loss.n_elem;
70 template<
typename InputDataType,
typename OutputDataType>
71 template<
typename Archive>
76 ar(CEREAL_NVP(delta));
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
Definition: huber_loss_impl.hpp:72
HuberLoss(const double delta=1.0, const bool mean=true)
Create the HuberLoss object.
Definition: huber_loss_impl.hpp:22
void Backward(const PredictionType &prediction, const TargetType &target, LossType &loss)
Ordinary feed backward pass of a neural network.
Definition: huber_loss_impl.hpp:51
PredictionType::elem_type Forward(const PredictionType &prediction, const TargetType &target)
Computes the Huber Loss function.
Definition: huber_loss_impl.hpp:34