combined_normalization.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_CF_NORMALIZATION_COMBINED_NORMALIZATION_HPP
14 #define MLPACK_METHODS_CF_NORMALIZATION_COMBINED_NORMALIZATION_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 
18 namespace mlpack {
19 namespace cf {
20 
43 template<typename... NormalizationTypes>
45 {
46  public:
47  using TupleType = std::tuple<NormalizationTypes...>;
48 
49  // Empty constructor.
51 
57  template<typename MatType>
58  void Normalize(MatType& data)
59  {
60  SequenceNormalize<0>(data);
61  }
62 
72  double Denormalize(const size_t user,
73  const size_t item,
74  const double rating) const
75  {
76  return SequenceDenormalize<0>(user, item, rating);
77  }
78 
87  void Denormalize(const arma::Mat<size_t>& combinations,
88  arma::vec& predictions) const
89  {
90  SequenceDenormalize<0>(combinations, predictions);
91  }
92 
96  const TupleType& Normalizations() const
97  {
98  return normalizations;
99  }
100 
104  template<typename Archive>
105  void serialize(Archive& ar, const uint32_t version)
106  {
107  SequenceSerialize<0, Archive>(ar, version);
108  }
109 
110  private:
112  TupleType normalizations;
113 
115  template<
116  int I, /* Which normalization in tuple to use */
117  typename MatType,
119  void SequenceNormalize(MatType& data)
120  {
121  std::get<I>(normalizations).Normalize(data);
122  SequenceNormalize<I + 1>(data);
123  }
124 
126  template<
127  int I, /* Which normalization in tuple to use */
128  typename MatType,
129  typename = std::enable_if_t<(I >= std::tuple_size<TupleType>::value)>,
130  typename = void>
131  void SequenceNormalize(MatType& /* data */) { }
132 
134  template<
135  int I, /* Which normalization in tuple to use */
137  double SequenceDenormalize(const size_t user,
138  const size_t item,
139  const double rating) const
140  {
141  // The order of denormalization should be the reversed order
142  // of normalization.
143  double realRating = SequenceDenormalize<I + 1>(user, item, rating);
144  realRating =
145  std::get<I>(normalizations).Denormalize(user, item, realRating);
146  return realRating;
147  }
148 
150  template<
151  int I, /* Which normalization in tuple to use */
152  typename = std::enable_if_t<(I >= std::tuple_size<TupleType>::value)>,
153  typename = void>
154  double SequenceDenormalize(const size_t /* user */,
155  const size_t /* item */,
156  const double rating) const
157  {
158  return rating;
159  }
160 
162  template<
163  int I, /* Which normalization in tuple to use */
165  void SequenceDenormalize(const arma::Mat<size_t>& combinations,
166  arma::vec& predictions) const
167  {
168  // The order of denormalization should be the reversed order
169  // of normalization.
170  SequenceDenormalize<I+1>(combinations, predictions);
171  std::get<I>(normalizations).Denormalize(combinations, predictions);
172  }
173 
175  template<
176  int I, /* Which normalization in tuple to use */
177  typename = std::enable_if_t<(I >= std::tuple_size<TupleType>::value)>,
178  typename = void>
179  void SequenceDenormalize(const arma::Mat<size_t>& /* combinations */,
180  arma::vec& /* predictions */) const { }
181 
183  template<
184  int I, /* Which normalization in tuple to serialize */
185  typename Archive,
187  void SequenceSerialize(Archive& ar, const uint32_t version)
188  {
189  std::string tagName = "normalization_";
190  tagName += std::to_string(I);
191  ar(cereal::make_nvp(
192  tagName.c_str(), std::get<I>(normalizations)));
193  SequenceSerialize<I + 1, Archive>(ar, version);
194  }
195 
197  template<
198  int I, /* Which normalization in tuple to serialize */
199  typename Archive,
200  typename = std::enable_if_t<(I >= std::tuple_size<TupleType>::value)>,
201  typename = void>
202  void SequenceSerialize(Archive& /* ar */, const uint32_t /* version */)
203  { }
204 };
205 
206 } // namespace cf
207 } // namespace mlpack
208 
209 #endif
typename enable_if< B, T >::type enable_if_t
Definition: prereqs.hpp:70
Linear algebra utility functions, generally performed on matrices or vectors.
void serialize(Archive &ar, const uint32_t version)
Serialization.
This normalization class performs a sequence of normalization methods on raw ratings.
The core includes that mlpack expects; standard C++ includes and Armadillo.
double Denormalize(const size_t user, const size_t item, const double rating) const
Denormalize rating by calling Denormalize() in each normalization object.
const TupleType & Normalizations() const
Return normalizations tuple.
void Normalize(MatType &data)
Normalize the data by calling Normalize() in each normalization object.
std::tuple< NormalizationTypes... > TupleType
void Denormalize(const arma::Mat< size_t > &combinations, arma::vec &predictions) const
Denormalize rating by calling Denormalize() in each normalization object.