multi_layer.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_ANN_LAYER_MULTI_LAYER_HPP
13 #define MLPACK_METHODS_ANN_LAYER_MULTI_LAYER_HPP
14 
15 #include "../make_alias.hpp"
16 #include "layer.hpp"
17 
18 namespace mlpack {
19 namespace ann {
20 
33 template<typename MatType>
34 class MultiLayer : public Layer<MatType>
35 {
36  public:
41  MultiLayer();
42 
44  MultiLayer(const MultiLayer& other);
46  MultiLayer(MultiLayer&& other);
48  MultiLayer& operator=(const MultiLayer& other);
51 
53  virtual ~MultiLayer()
54  {
55  for (size_t i = 0; i < network.size(); ++i)
56  delete network[i];
57  }
58 
60  virtual MultiLayer* Clone() const { return new MultiLayer(*this); }
61 
70  virtual void Forward(const MatType& input, MatType& output);
71 
83  void Forward(const MatType& input,
84  MatType& output,
85  const size_t start,
86  const size_t end);
87 
106  virtual void Backward(const MatType& input,
107  const MatType& gy,
108  MatType& g);
109 
124  virtual void Gradient(const MatType& input,
125  const MatType& error,
126  MatType& gradient);
127 
131  virtual void SetWeights(typename MatType::elem_type* weightsPtr);
132 
139  virtual void CustomInitialize(
140  MatType& W,
141  const size_t elements);
142 
147  virtual size_t WeightSize() const;
148 
154  virtual void ComputeOutputDimensions();
155 
159  virtual double Loss() const;
160 
166  template <typename LayerType, typename... Args>
167  void Add(Args... args)
168  {
169  network.push_back(new LayerType(args...));
170  layerOutputs.push_back(MatType());
171  layerDeltas.push_back(MatType());
172  layerGradients.push_back(MatType());
173  }
174 
180  void Add(Layer<MatType>* layer)
181  {
182  network.push_back(layer);
183  layerOutputs.push_back(MatType());
184  layerDeltas.push_back(MatType());
185  layerGradients.push_back(MatType());
186  }
187 
189  const std::vector<Layer<MatType>*>& Network() const
190  {
191  return network;
192  }
195  std::vector<Layer<MatType>*>& Network() { return network; }
196 
198  template<typename Archive>
199  void serialize(Archive& ar, const uint32_t /* version */);
200 
201  protected:
209  void InitializeForwardPassMemory(const size_t batchSize);
210 
218  void InitializeBackwardPassMemory(const size_t batchSize);
219 
226  void InitializeGradientPassMemory(MatType& gradient);
227 
229  std::vector<Layer<MatType>*> network;
230 
231  // Total number of elements in the input, cached for convenience.
232  size_t inSize;
233  // Total number of input elements for *every* layer.
235  // Total number of output elements for *every* layer.
237 
242  std::vector<MatType> layerOutputs;
243 
248  std::vector<MatType> layerDeltas;
249 
253  std::vector<MatType> layerGradients;
254 };
255 
256 } // namespace ann
257 } // namespace mlpack
258 
259 // Include implementation.
260 #include "multi_layer_impl.hpp"
261 
262 #endif
std::vector< Layer< MatType > * > & Network()
Modify the network (series of layers) held by this MultiLayer.
void InitializeBackwardPassMemory(const size_t batchSize)
Initialize memory that will be used by each layer for the backwards pass, assuming that the input wil...
std::vector< MatType > layerOutputs
These are aliases of layerOutputMatrix for each layer.
std::vector< MatType > layerGradients
Gradient aliases for each layer.
Linear algebra utility functions, generally performed on matrices or vectors.
MultiLayer()
Create an empty MultiLayer that holds no layers of its own.
virtual void ComputeOutputDimensions()
Compute the output dimensions of the MultiLayer using InputDimensions().
void Add(Layer< MatType > *layer)
Add a new module to the model.
virtual void SetWeights(typename MatType::elem_type *weightsPtr)
Set the weights of the layer to use the memory given as weightsPtr.
const std::vector< Layer< MatType > * > & Network() const
Get the network (series of layers) held by this MultiLayer.
virtual void Backward(const MatType &input, const MatType &gy, MatType &g)
Perform a backward pass with the given data.
virtual MultiLayer * Clone() const
Create a copy of the MultiLayer (this is safe for polymorphic use).
Definition: multi_layer.hpp:60
virtual void Forward(const MatType &input, MatType &output)
Perform a forward pass with the given input data.
virtual ~MultiLayer()
Virtual destructor: delete all held layers.
Definition: multi_layer.hpp:53
A "multi-layer" is a layer that is a wrapper around other layers.
Definition: multi_layer.hpp:34
void Add(Args... args)
Add a new module to the model.
void InitializeForwardPassMemory(const size_t batchSize)
Initialize memory that will be used by each layer for the forward pass, assuming that the input will ...
virtual void Gradient(const MatType &input, const MatType &error, MatType &gradient)
Compute the gradients of each layer.
virtual double Loss() const
Compute the loss that should be added to the objective.
virtual size_t WeightSize() const
Return the number of weights in the MultiLayer.
virtual void CustomInitialize(MatType &W, const size_t elements)
Initialize the weight matrix of the layer.
A layer is an abstract class implementing common neural networks operations, such as convolution...
Definition: layer.hpp:52
MatType layerOutputMatrix
This matrix stores all of the outputs of each layer when Forward() is called.
std::vector< Layer< MatType > * > network
The internally-held network.
std::vector< MatType > layerDeltas
These are aliases of layerDeltaMatrix for each layer.
void InitializeGradientPassMemory(MatType &gradient)
Initialize memory for the gradient pass.
void serialize(Archive &ar, const uint32_t)
Serialize the MultiLayer.
MultiLayer & operator=(const MultiLayer &other)
Copy the given MultiLayer.
MatType layerDeltaMatrix
This matrix stores all of the backwards pass results of each layer when Backward() is called...