layer.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_ANN_LAYER_LAYER_HPP
13 #define MLPACK_METHODS_ANN_LAYER_LAYER_HPP
14 
15 namespace mlpack {
16 namespace ann {
17 
51 template<typename MatType = arma::mat>
52 class Layer
53 {
54  public:
56  Layer() :
57  validOutputDimensions(false),
58  training(false)
59  { /* Nothing to do here */ }
60 
62  virtual ~Layer() { /* Nothing to do here */ }
63 
65  Layer(const Layer& layer) :
69  training(layer.training)
70  { /* Nothing to do here */ }
71 
73  virtual Layer* Clone() const = 0;
74 
76  Layer(Layer&& layer) :
80  training(std::move(layer.training))
81  { /* Nothing to do here */ }
82 
84  virtual Layer& operator=(const Layer& layer)
85  {
86  if (&layer != this)
87  {
91  training = layer.training;
92  }
93 
94  return *this;
95  }
96 
98  virtual Layer& operator=(Layer&& layer)
99  {
100  if (&layer != this)
101  {
102  inputDimensions = std::move(layer.inputDimensions);
103  outputDimensions = std::move(layer.outputDimensions);
104  validOutputDimensions = std::move(layer.validOutputDimensions);
105  training = std::move(layer.training);
106  }
107 
108  return *this;
109  }
110 
120  virtual void Forward(const MatType& /* input */,
121  MatType& /* output */)
122  { /* Nothing to do here */ }
123 
133  virtual void Forward(const MatType& /* input */,
134  const MatType& /* output */)
135  { /* Nothing to do here */ }
136 
158  virtual void Backward(const MatType& /* input */,
159  const MatType& /* gy */,
160  MatType& /* g */)
161  { /* Nothing to do here */ }
162 
175  virtual void Gradient(const MatType& /* input */,
176  const MatType& /* error */,
177  MatType& /* gradient */)
178  { /* Nothing to do here */ }
179 
192  virtual void SetWeights(typename MatType::elem_type* /* weightsPtr */) { }
193 
197  virtual size_t WeightSize() const { return 0; }
198 
207  virtual bool const& Training() const { return training; }
208 
217  virtual bool& Training() { return training; }
218 
221  virtual double Loss() { return 0; }
222 
224  const std::vector<size_t>& InputDimensions() const { return inputDimensions; }
226  std::vector<size_t>& InputDimensions()
227  {
228  validOutputDimensions = false;
229  return inputDimensions;
230  }
231 
233  const std::vector<size_t>& OutputDimensions()
234  {
236  {
237  this->ComputeOutputDimensions();
238  validOutputDimensions = true;
239  }
240 
241  return outputDimensions;
242  }
243 
245  virtual const MatType& Parameters() const
246  {
247  throw std::invalid_argument("Layer::Parameters(): cannot access parameters "
248  "of a layer with no weights!");
249  }
251  virtual MatType& Parameters()
252  {
253  throw std::invalid_argument("Layer::Parameters(): cannot modify parameters "
254  "of a layer with no weights!");
255  }
256 
267  virtual void CustomInitialize(
268  MatType& /* W */,
269  const size_t /* elements */)
270  { /* Nothing to do here */ }
271 
275  virtual void ComputeOutputDimensions()
276  {
277  // The default implementation is to assume that the output size is the same
278  // as the input.
280  }
281 
284  virtual size_t OutputSize() final
285  {
287  {
288  this->ComputeOutputDimensions();
289  validOutputDimensions = true;
290  }
291 
292  size_t outputSize = 1;
293  for (size_t i = 0; i < this->outputDimensions.size(); ++i)
294  outputSize *= this->outputDimensions[i];
295  return outputSize;
296  }
297 
299  template<typename Archive>
300  void serialize(Archive& ar, const uint32_t /* version */)
301  {
302  ar(CEREAL_NVP(inputDimensions));
303  ar(CEREAL_NVP(outputDimensions));
304  ar(CEREAL_NVP(validOutputDimensions));
305  ar(CEREAL_NVP(training));
306 
307  // Note that layer weights are serialized by the FFN!
308  }
309 
310  protected:
319  std::vector<size_t> inputDimensions;
320 
327  std::vector<size_t> outputDimensions;
328 
332 
334  bool training;
335 };
336 
337 } // namespace ann
338 } // namespace mlpack
339 
340 #endif
virtual bool & Training()
Modify whether the layer is currently in training mode.
Definition: layer.hpp:217
virtual MatType & Parameters()
Set the parameters.
Definition: layer.hpp:251
std::vector< size_t > inputDimensions
Logical input dimensions of each point.
Definition: layer.hpp:319
virtual const MatType & Parameters() const
Get the parameters.
Definition: layer.hpp:245
Linear algebra utility functions, generally performed on matrices or vectors.
virtual void Gradient(const MatType &, const MatType &, MatType &)
Computing the gradient of the layer with respect to its own input.
Definition: layer.hpp:175
const std::vector< size_t > & OutputDimensions()
Get the output dimensions.
Definition: layer.hpp:233
virtual ~Layer()
Default deconstructor.
Definition: layer.hpp:62
virtual void SetWeights(typename MatType::elem_type *)
Reset the layer parameter.
Definition: layer.hpp:192
Layer()
Default constructor.
Definition: layer.hpp:56
std::vector< size_t > outputDimensions
Logical output dimensions of each point.
Definition: layer.hpp:327
virtual Layer * Clone() const =0
Make a copy of the object.
virtual Layer & operator=(Layer &&layer)
Move assignment operator. This is not responsible for moving weights!
Definition: layer.hpp:98
virtual Layer & operator=(const Layer &layer)
Copy assignment operator. This is not responsible for copying weights!
Definition: layer.hpp:84
const std::vector< size_t > & InputDimensions() const
Get the input dimensions.
Definition: layer.hpp:224
virtual double Loss()
Get the layer loss.
Definition: layer.hpp:221
virtual void CustomInitialize(MatType &, const size_t)
Override the weight matrix of the layer.
Definition: layer.hpp:267
virtual void ComputeOutputDimensions()
Compute the output dimensions.
Definition: layer.hpp:275
virtual void Backward(const MatType &, const MatType &, MatType &)
Performs a backpropagation step through the layer, with respect to the given input.
Definition: layer.hpp:158
bool training
If true, the layer is in training mode; otherwise, it is in testing mode.
Definition: layer.hpp:334
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
Definition: layer.hpp:300
A layer is an abstract class implementing common neural networks operations, such as convolution...
Definition: layer.hpp:52
bool validOutputDimensions
This is true if ComputeOutputDimensions() has been called, and outputDimensions can be considered to ...
Definition: layer.hpp:331
virtual size_t OutputSize() final
Get the number of elements in the output from this layer.
Definition: layer.hpp:284
Layer(const Layer &layer)
Copy constructor. This is not responsible for copying weights!
Definition: layer.hpp:65
virtual void Forward(const MatType &, MatType &)
Takes an input object, and computes the corresponding output of the layer.
Definition: layer.hpp:120
auto move(Range &&rng, OutputIt &&it) -> enable_if_t< is_range< Range >::value, decay_t< OutputIt > >
Definition: algorithm.hpp:736
std::vector< size_t > & InputDimensions()
Modify the input dimensions.
Definition: layer.hpp:226
Layer(Layer &&layer)
Move constructor. This is not responsible for moving weights!
Definition: layer.hpp:76
virtual void Forward(const MatType &, const MatType &)
Takes an input and output object, and computes the corresponding loss of the layer.
Definition: layer.hpp:133
virtual bool const & Training() const
Get whether the layer is currently in training mode.
Definition: layer.hpp:207
virtual size_t WeightSize() const
Get the total number of trainable weights in the layer.
Definition: layer.hpp:197