mlpack
negative_log_likelihood_impl.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_ANN_LAYER_NEGATIVE_LOG_LIKELIHOOD_IMPL_HPP
13 #define MLPACK_METHODS_ANN_LAYER_NEGATIVE_LOG_LIKELIHOOD_IMPL_HPP
14 
15 // In case it hasn't yet been included.
17 
18 namespace mlpack {
19 namespace ann {
20 
21 template<typename InputDataType, typename OutputDataType>
23 {
24  // Nothing to do here.
25 }
26 
27 template<typename InputDataType, typename OutputDataType>
28 template<typename PredictionType, typename TargetType>
29 typename PredictionType::elem_type
31  const PredictionType& prediction,
32  const TargetType& target)
33 {
34  typedef typename PredictionType::elem_type ElemType;
35  ElemType output = 0;
36  for (size_t i = 0; i < prediction.n_cols; ++i)
37  {
38  Log::Assert(target(i) >= 0 && target(i) < prediction.n_rows,
39  "Target class out of range.");
40 
41  output -= prediction(target(i), i);
42  }
43 
44  return output;
45 }
46 
47 template<typename InputDataType, typename OutputDataType>
48 template<typename PredictionType, typename TargetType, typename LossType>
50  const PredictionType& prediction,
51  const TargetType& target,
52  LossType& loss)
53 {
54  loss = arma::zeros<LossType>(prediction.n_rows, prediction.n_cols);
55  for (size_t i = 0; i < prediction.n_cols; ++i)
56  {
57  Log::Assert(target(i) >= 0 && target(i) < prediction.n_rows,
58  "Target class out of range.");
59 
60  loss(target(i), i) = -1;
61  }
62 }
63 
64 template<typename InputDataType, typename OutputDataType>
65 template<typename Archive>
67  Archive& /* ar */, const uint32_t /* version */)
68 {
69  // Nothing to do here.
70 }
71 
72 } // namespace ann
73 } // namespace mlpack
74 
75 #endif
void Backward(const PredictionType &prediction, const TargetType &target, LossType &loss)
Ordinary feed backward pass of a neural network.
Definition: negative_log_likelihood_impl.hpp:49
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
NegativeLogLikelihood()
Create the NegativeLogLikelihoodLayer object.
Definition: negative_log_likelihood_impl.hpp:22
void serialize(Archive &, const uint32_t)
Serialize the layer.
Definition: negative_log_likelihood_impl.hpp:66
PredictionType::elem_type Forward(const PredictionType &prediction, const TargetType &target)
Computes the Negative log likelihood.
Definition: negative_log_likelihood_impl.hpp:30
static void Assert(bool condition, const std::string &message="Assert Failed.")
Checks if the specified condition is true.
Definition: log.cpp:38