mlpack
binary_cross_entropy_loss_impl.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_ANN_LOSS_FUNCTIONS_CROSS_ENTROPY_ERROR_IMPL_HPP
13 #define MLPACK_METHODS_ANN_LOSS_FUNCTIONS_CROSS_ENTROPY_ERROR_IMPL_HPP
14 
15 // In case it hasn't yet been included.
16 #include "binary_cross_entropy_loss.hpp"
17 
18 namespace mlpack {
19 namespace ann {
20 
21 template<typename InputDataType, typename OutputDataType>
23  const double eps, const bool reduction) : eps(eps), reduction(reduction)
24 {
25  // Nothing to do here.
26 }
27 
28 template<typename InputDataType, typename OutputDataType>
29 template<typename PredictionType, typename TargetType>
30 typename PredictionType::elem_type
32  const PredictionType& prediction,
33  const TargetType& target)
34 {
35  typedef typename PredictionType::elem_type ElemType;
36 
37  ElemType loss = -arma::accu(target % arma::log(prediction + eps) +
38  (1. - target) % arma::log(1. - prediction + eps));
39  if (reduction)
40  loss /= prediction.n_elem;
41  return loss;
42 }
43 
44 template<typename InputDataType, typename OutputDataType>
45 template<typename PredictionType, typename TargetType, typename LossType>
47  const PredictionType& prediction,
48  const TargetType& target,
49  LossType& loss)
50 {
51  loss = (1. - target) / (1. - prediction + eps) - target / (prediction + eps);
52  if (reduction)
53  loss /= prediction.n_elem;
54 }
55 
56 template<typename InputDataType, typename OutputDataType>
57 template<typename Archive>
59  Archive& ar,
60  const uint32_t /* version */)
61 {
62  ar(CEREAL_NVP(eps));
63 }
64 
65 } // namespace ann
66 } // namespace mlpack
67 
68 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
PredictionType::elem_type Forward(const PredictionType &prediction, const TargetType &target)
Computes the cross-entropy function.
Definition: binary_cross_entropy_loss_impl.hpp:31
BCELoss(const double eps=1e-10, const bool reduction=true)
Create the BinaryCrossEntropyLoss object.
Definition: binary_cross_entropy_loss_impl.hpp:22
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
Definition: binary_cross_entropy_loss_impl.hpp:58
void Backward(const PredictionType &prediction, const TargetType &target, LossType &loss)
Ordinary feed backward pass of a neural network.
Definition: binary_cross_entropy_loss_impl.hpp:46