concat.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_ANN_LAYER_CONCAT_HPP
14 #define MLPACK_METHODS_ANN_LAYER_CONCAT_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 
18 #include "../visitor/delete_visitor.hpp"
19 #include "../visitor/delta_visitor.hpp"
20 #include "../visitor/output_parameter_visitor.hpp"
21 
22 #include "layer_types.hpp"
23 
24 namespace mlpack {
25 namespace ann {
26 
38 template <
39  typename InputDataType = arma::mat,
40  typename OutputDataType = arma::mat,
41  typename... CustomLayers
42 >
43 class Concat
44 {
45  public:
52  Concat(const bool model = false,
53  const bool run = true);
54 
63  Concat(arma::Row<size_t>& inputSize,
64  const size_t axis,
65  const bool model = false,
66  const bool run = true);
67 
71  ~Concat();
72 
80  template<typename eT>
81  void Forward(const arma::Mat<eT>& input, arma::Mat<eT>& output);
82 
92  template<typename eT>
93  void Backward(const arma::Mat<eT>& /* input */,
94  const arma::Mat<eT>& gy,
95  arma::Mat<eT>& g);
96 
106  template<typename eT>
107  void Backward(const arma::Mat<eT>& /* input */,
108  const arma::Mat<eT>& gy,
109  arma::Mat<eT>& g,
110  const size_t index);
111 
112  /*
113  * Calculate the gradient using the output delta and the input activation.
114  *
115  * @param input The input parameter used for calculating the gradient.
116  * @param error The calculated error.
117  * @param gradient The calculated gradient.
118  */
119  template<typename eT>
120  void Gradient(const arma::Mat<eT>& /* input */,
121  const arma::Mat<eT>& error,
122  arma::Mat<eT>& /* gradient */);
123 
124  /*
125  * This is the overload of Gradient() that runs a specific layer with the
126  * given input.
127  *
128  * @param input The input parameter used for calculating the gradient.
129  * @param error The calculated error.
130  * @param gradient The calculated gradient.
131  * @param The index of the layer to run.
132  */
133  template<typename eT>
134  void Gradient(const arma::Mat<eT>& input,
135  const arma::Mat<eT>& error,
136  arma::Mat<eT>& gradient,
137  const size_t index);
138 
139  /*
140  * Add a new module to the model.
141  *
142  * @param args The layer parameter.
143  */
144  template <class LayerType, class... Args>
145  void Add(Args... args) { network.push_back(new LayerType(args...)); }
146 
147  /*
148  * Add a new module to the model.
149  *
150  * @param layer The Layer to be added to the model.
151  */
152  void Add(LayerTypes<CustomLayers...> layer) { network.push_back(layer); }
153 
155  std::vector<LayerTypes<CustomLayers...> >& Model()
156  {
157  if (model)
158  {
159  return network;
160  }
161 
162  return empty;
163  }
164 
166  const arma::mat& Parameters() const { return weights; }
168  arma::mat& Parameters() { return weights; }
169 
171  bool Run() const { return run; }
173  bool& Run() { return run; }
174 
175  arma::mat const& InputParameter() const { return inputParameter; }
177  arma::mat& InputParameter() { return inputParameter; }
178 
180  arma::mat const& OutputParameter() const { return outputParameter; }
182  arma::mat& OutputParameter() { return outputParameter; }
183 
185  arma::mat const& Delta() const { return delta; }
187  arma::mat& Delta() { return delta; }
188 
190  arma::mat const& Gradient() const { return gradient; }
192  arma::mat& Gradient() { return gradient; }
193 
195  size_t const& ConcatAxis() const { return axis; }
196 
198  size_t WeightSize() const { return 0; }
199 
203  template<typename Archive>
204  void serialize(Archive& ar, const uint32_t /* version */);
205 
206  private:
208  arma::Row<size_t> inputSize;
209 
211  size_t axis;
212 
214  bool useAxis;
215 
217  bool model;
218 
221  bool run;
222 
224  size_t channels;
225 
227  std::vector<LayerTypes<CustomLayers...> > network;
228 
230  OutputDataType weights;
231 
233  DeltaVisitor deltaVisitor;
234 
236  OutputParameterVisitor outputParameterVisitor;
237 
239  DeleteVisitor deleteVisitor;
240 
242  std::vector<LayerTypes<CustomLayers...> > empty;
243 
245  arma::mat delta;
246 
248  arma::mat inputParameter;
249 
251  arma::mat outputParameter;
252 
254  arma::mat gradient;
255 }; // class Concat
256 
257 } // namespace ann
258 } // namespace mlpack
259 
260 // Include implementation.
261 #include "concat_impl.hpp"
262 
263 #endif
DeleteVisitor executes the destructor of the instantiated object.
arma::mat const & InputParameter() const
Definition: concat.hpp:175
void Backward(const arma::Mat< eT > &, const arma::Mat< eT > &gy, arma::Mat< eT > &g)
Ordinary feed backward pass of a neural network, using 3rd-order tensors as input, calculating the function f(x) by propagating x backwards through f.
Concat(const bool model=false, const bool run=true)
Create the Concat object using the specified parameters.
arma::mat & Parameters()
Modify the initial point for the optimization.
Definition: concat.hpp:168
Linear algebra utility functions, generally performed on matrices or vectors.
size_t const & ConcatAxis() const
Get the axis of concatenation.
Definition: concat.hpp:195
The core includes that mlpack expects; standard C++ includes and Armadillo.
~Concat()
Destroy the layers held by the model.
void Add(LayerTypes< CustomLayers... > layer)
Definition: concat.hpp:152
std::vector< LayerTypes< CustomLayers... > > & Model()
Return the model modules.
Definition: concat.hpp:155
arma::mat & Gradient()
Modify the gradient.
Definition: concat.hpp:192
arma::mat const & Delta() const
Get the delta.e.
Definition: concat.hpp:185
arma::mat const & OutputParameter() const
Get the output parameter.
Definition: concat.hpp:180
arma::mat & InputParameter()
Modify the input parameter.
Definition: concat.hpp:177
Implementation of the Concat class.
Definition: concat.hpp:43
bool Run() const
Get the value of run parameter.
Definition: concat.hpp:171
OutputParameterVisitor exposes the output parameter of the given module.
arma::mat & OutputParameter()
Modify the output parameter.
Definition: concat.hpp:182
void Forward(const arma::Mat< eT > &input, arma::Mat< eT > &output)
Ordinary feed forward pass of a neural network, evaluating the function f(x) by propagating the activ...
bool & Run()
Modify the value of run parameter.
Definition: concat.hpp:173
arma::mat & Delta()
Modify the delta.
Definition: concat.hpp:187
DeltaVisitor exposes the delta parameter of the given module.
boost::variant< AdaptiveMaxPooling< arma::mat, arma::mat > *, AdaptiveMeanPooling< arma::mat, arma::mat > *, Add< arma::mat, arma::mat > *, AddMerge< arma::mat, arma::mat > *, AlphaDropout< arma::mat, arma::mat > *, AtrousConvolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< FullConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > *, BaseLayer< LogisticFunction, arma::mat, arma::mat > *, BaseLayer< IdentityFunction, arma::mat, arma::mat > *, BaseLayer< TanhFunction, arma::mat, arma::mat > *, BaseLayer< SoftplusFunction, arma::mat, arma::mat > *, BaseLayer< RectifierFunction, arma::mat, arma::mat > *, BatchNorm< arma::mat, arma::mat > *, BilinearInterpolation< arma::mat, arma::mat > *, CELU< arma::mat, arma::mat > *, Concat< arma::mat, arma::mat > *, Concatenate< arma::mat, arma::mat > *, ConcatPerformance< NegativeLogLikelihood< arma::mat, arma::mat >, arma::mat, arma::mat > *, Constant< arma::mat, arma::mat > *, Convolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< FullConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > *, CReLU< arma::mat, arma::mat > *, DropConnect< arma::mat, arma::mat > *, Dropout< arma::mat, arma::mat > *, ELU< arma::mat, arma::mat > *, FastLSTM< arma::mat, arma::mat > *, GRU< arma::mat, arma::mat > *, HardTanH< arma::mat, arma::mat > *, Join< arma::mat, arma::mat > *, LayerNorm< arma::mat, arma::mat > *, LeakyReLU< arma::mat, arma::mat > *, Linear< arma::mat, arma::mat, NoRegularizer > *, LinearNoBias< arma::mat, arma::mat, NoRegularizer > *, LogSoftMax< arma::mat, arma::mat > *, Lookup< arma::mat, arma::mat > *, LSTM< arma::mat, arma::mat > *, MaxPooling< arma::mat, arma::mat > *, MeanPooling< arma::mat, arma::mat > *, MiniBatchDiscrimination< arma::mat, arma::mat > *, MultiplyConstant< arma::mat, arma::mat > *, MultiplyMerge< arma::mat, arma::mat > *, NegativeLogLikelihood< arma::mat, arma::mat > *, NoisyLinear< arma::mat, arma::mat > *, Padding< arma::mat, arma::mat > *, PReLU< arma::mat, arma::mat > *, Sequential< arma::mat, arma::mat, false > *, Sequential< arma::mat, arma::mat, true > *, Softmax< arma::mat, arma::mat > *, TransposedConvolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > *, WeightNorm< arma::mat, arma::mat > *, MoreTypes, CustomLayers *... > LayerTypes
size_t WeightSize() const
Get the size of the weight matrix.
Definition: concat.hpp:198
arma::mat const & Gradient() const
Get the gradient.
Definition: concat.hpp:190
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
const arma::mat & Parameters() const
Return the initial point for the optimization.
Definition: concat.hpp:166
void Add(Args... args)
Definition: concat.hpp:145