mlpack
cosine_embedding_loss_impl.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_ANN_LOSS_FUNCTION_COSINE_EMBEDDING_IMPL_HPP
13 #define MLPACK_METHODS_ANN_LOSS_FUNCTION_COSINE_EMBEDDING_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  const double margin, const bool similarity, const bool takeMean):
24  margin(margin), similarity(similarity), takeMean(takeMean)
25 {
26  // Nothing to do here.
27 }
28 
29 template<typename InputDataType, typename OutputDataType>
30 template<typename PredictionType, typename TargetType>
31 typename PredictionType::elem_type
33  const PredictionType& prediction,
34  const TargetType& target)
35 {
36  typedef typename PredictionType::elem_type ElemType;
37 
38  const size_t cols = prediction.n_cols;
39  const size_t batchSize = prediction.n_elem / cols;
40  if (arma::size(prediction) != arma::size(target))
41  Log::Fatal << "Input Tensors must have same dimensions." << std::endl;
42 
43  arma::colvec inputTemp1 = arma::vectorise(prediction);
44  arma::colvec inputTemp2 = arma::vectorise(target);
45  ElemType loss = 0.0;
46 
47  for (size_t i = 0; i < inputTemp1.n_elem; i += cols)
48  {
49  const ElemType cosDist = kernel::CosineDistance::Evaluate(
50  inputTemp1(arma::span(i, i + cols - 1)), inputTemp2(arma::span(i,
51  i + cols - 1)));
52  if (similarity)
53  loss += 1 - cosDist;
54  else
55  {
56  const ElemType currentLoss = cosDist - margin;
57  loss += currentLoss > 0 ? currentLoss : 0;
58  }
59  }
60 
61  if (takeMean)
62  loss = (ElemType) loss / batchSize;
63 
64  return loss;
65 }
66 
67 template<typename InputDataType, typename OutputDataType>
68 template<typename PredictionType, typename TargetType, typename LossType>
70  const PredictionType& prediction,
71  const TargetType& target,
72  LossType& loss)
73 {
74  typedef typename PredictionType::elem_type ElemType;
75 
76  const size_t cols = prediction.n_cols;
77  if (arma::size(prediction) != arma::size(target))
78  Log::Fatal << "Input Tensors must have same dimensions." << std::endl;
79 
80  arma::colvec inputTemp1 = arma::vectorise(prediction);
81  arma::colvec inputTemp2 = arma::vectorise(target);
82  loss.set_size(arma::size(inputTemp1));
83 
84  arma::colvec outputTemp(loss.memptr(), inputTemp1.n_elem,
85  false, false);
86  for (size_t i = 0; i < inputTemp1.n_elem; i += cols)
87  {
88  const ElemType cosDist = kernel::CosineDistance::Evaluate(inputTemp1(
89  arma::span(i, i + cols -1)), inputTemp2(arma::span(i, i + cols -1)));
90 
91  if (cosDist < margin && !similarity)
92  outputTemp(arma::span(i, i + cols - 1)).zeros();
93  else
94  {
95  const int multiplier = similarity ? 1 : -1;
96  outputTemp(arma::span(i, i + cols -1)) = -1 * multiplier *
97  (arma::normalise(inputTemp2(arma::span(i, i + cols - 1))) -
98  cosDist * arma::normalise(inputTemp1(arma::span(i, i + cols -
99  1)))) / std::sqrt(arma::accu(arma::pow(inputTemp1(arma::span(i, i +
100  cols - 1)), 2)));
101  }
102  }
103 }
104 
105 template<typename InputDataType, typename OutputDataType>
106 template<typename Archive>
108  Archive& ar, const uint32_t /* version */)
109 {
110  ar(CEREAL_NVP(margin));
111  ar(CEREAL_NVP(similarity));
112  ar(CEREAL_NVP(takeMean));
113 }
114 
115 } // namespace ann
116 } // namespace mlpack
117 
118 #endif
CosineEmbeddingLoss(const double margin=0.0, const bool similarity=true, const bool takeMean=false)
Create the CosineEmbeddingLoss object.
Definition: cosine_embedding_loss_impl.hpp:22
static MLPACK_EXPORT util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
Definition: log.hpp:90
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
void Backward(const PredictionType &prediction, const TargetType &target, LossType &loss)
Ordinary feed backward pass of a neural network.
Definition: cosine_embedding_loss_impl.hpp:69
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
Definition: cosine_embedding_loss_impl.hpp:107
static double Evaluate(const VecTypeA &a, const VecTypeB &b)
Computes the cosine distance between two points.
Definition: cosine_distance_impl.hpp:21
PredictionType::elem_type Forward(const PredictionType &prediction, const TargetType &target)
Ordinary feed forward pass of a neural network.
Definition: cosine_embedding_loss_impl.hpp:32