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 <boost/ptr_container/ptr_vector.hpp>
23 
24 #include "layer_types.hpp"
25 
26 namespace mlpack {
27 namespace ann {
28 
40 template <
41  typename InputDataType = arma::mat,
42  typename OutputDataType = arma::mat,
43  typename... CustomLayers
44 >
45 class Concat
46 {
47  public:
54  Concat(const bool model = false,
55  const bool run = true);
56 
65  Concat(arma::Row<size_t>& inputSize,
66  const size_t axis,
67  const bool model = false,
68  const bool run = true);
69 
73  ~Concat();
74 
82  template<typename eT>
83  void Forward(arma::Mat<eT>&& input, arma::Mat<eT>&& output);
84 
94  template<typename eT>
95  void Backward(const arma::Mat<eT>&& /* input */,
96  arma::Mat<eT>&& gy,
97  arma::Mat<eT>&& g);
98 
108  template<typename eT>
109  void Backward(const arma::Mat<eT>&& /* input */,
110  arma::Mat<eT>&& gy,
111  arma::Mat<eT>&& g,
112  const size_t index);
113 
114  /*
115  * Calculate the gradient using the output delta and the input activation.
116  *
117  * @param input The input parameter used for calculating the gradient.
118  * @param error The calculated error.
119  * @param gradient The calculated gradient.
120  */
121  template<typename eT>
122  void Gradient(arma::Mat<eT>&& /* input */,
123  arma::Mat<eT>&& error,
124  arma::Mat<eT>&& /* gradient */);
125 
126  /*
127  * This is the overload of Gradient() that runs a specific layer with the
128  * given input.
129  *
130  * @param input The input parameter used for calculating the gradient.
131  * @param error The calculated error.
132  * @param gradient The calculated gradient.
133  * @param The index of the layer to run.
134  */
135  template<typename eT>
136  void Gradient(arma::Mat<eT>&& input,
137  arma::Mat<eT>&& error,
138  arma::Mat<eT>&& gradient,
139  const size_t index);
140 
141  /*
142  * Add a new module to the model.
143  *
144  * @param layer The Layer to be added to the model.
145  */
146  template<typename LayerType>
147  void Add(const LayerType& layer) { network.push_back(new LayerType(layer)); }
148 
149  /*
150  * Add a new module to the model.
151  *
152  * @param args The layer parameter.
153  */
154  template <class LayerType, class... Args>
155  void Add(Args... args) { network.push_back(new LayerType(args...)); }
156 
157  /*
158  * Add a new module to the model.
159  *
160  * @param layer The Layer to be added to the model.
161  */
162  void Add(LayerTypes<CustomLayers...> layer) { network.push_back(layer); }
163 
165  std::vector<LayerTypes<CustomLayers...> >& Model()
166  {
167  if (model)
168  {
169  return network;
170  }
171 
172  return empty;
173  }
174 
176  const arma::mat& Parameters() const { return parameters; }
178  arma::mat& Parameters() { return parameters; }
179 
181  bool Run() const { return run; }
183  bool& Run() { return run; }
184 
185  arma::mat const& InputParameter() const { return inputParameter; }
187  arma::mat& InputParameter() { return inputParameter; }
188 
190  arma::mat const& OutputParameter() const { return outputParameter; }
192  arma::mat& OutputParameter() { return outputParameter; }
193 
195  arma::mat const& Delta() const { return delta; }
197  arma::mat& Delta() { return delta; }
198 
200  arma::mat const& Gradient() const { return gradient; }
202  arma::mat& Gradient() { return gradient; }
203 
207  template<typename Archive>
208  void serialize(Archive& /* ar */, const unsigned int /* version */);
209 
210  private:
212  arma::Row<size_t> inputSize;
213 
215  size_t axis;
216 
218  bool useAxis;
219 
221  bool model;
222 
225  bool run;
226 
228  size_t channels;
229 
231  std::vector<LayerTypes<CustomLayers...> > network;
232 
234  arma::mat parameters;
235 
237  DeltaVisitor deltaVisitor;
238 
240  OutputParameterVisitor outputParameterVisitor;
241 
243  DeleteVisitor deleteVisitor;
244 
246  std::vector<LayerTypes<CustomLayers...> > empty;
247 
249  arma::mat delta;
250 
252  arma::mat inputParameter;
253 
255  arma::mat outputParameter;
256 
258  arma::mat gradient;
259 }; // class Concat
260 
261 } // namespace ann
262 } // namespace mlpack
263 
264 // Include implementation.
265 #include "concat_impl.hpp"
266 
267 #endif
DeleteVisitor executes the destructor of the instantiated object.
arma::mat const & InputParameter() const
Definition: concat.hpp:185
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:178
.hpp
Definition: add_to_po.hpp:21
The core includes that mlpack expects; standard C++ includes and Armadillo.
~Concat()
Destroy the layers held by the model.
void Add(const LayerType &layer)
Definition: concat.hpp:147
void Add(LayerTypes< CustomLayers... > layer)
Definition: concat.hpp:162
std::vector< LayerTypes< CustomLayers... > > & Model()
Return the model modules.
Definition: concat.hpp:165
boost::variant< Add< arma::mat, arma::mat > *, AddMerge< 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< RectifierFunction, arma::mat, arma::mat > *, BaseLayer< SoftplusFunction, arma::mat, arma::mat > *, BatchNorm< arma::mat, arma::mat > *, BilinearInterpolation< 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 > *, TransposedConvolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< FullConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > *, DropConnect< arma::mat, arma::mat > *, Dropout< arma::mat, arma::mat > *, AlphaDropout< arma::mat, arma::mat > *, ELU< arma::mat, arma::mat > *, FlexibleReLU< arma::mat, arma::mat > *, Glimpse< arma::mat, arma::mat > *, HardTanH< arma::mat, arma::mat > *, Highway< arma::mat, arma::mat > *, Join< arma::mat, arma::mat > *, LayerNorm< arma::mat, arma::mat > *, LeakyReLU< arma::mat, arma::mat > *, CReLU< 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 > *, GRU< arma::mat, arma::mat > *, FastLSTM< 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 > *, Padding< arma::mat, arma::mat > *, PReLU< arma::mat, arma::mat > *, MoreTypes, CustomLayers *... > LayerTypes
arma::mat & Gradient()
Modify the gradient.
Definition: concat.hpp:202
arma::mat const & Delta() const
Get the delta.e.
Definition: concat.hpp:195
arma::mat const & OutputParameter() const
Get the output parameter.
Definition: concat.hpp:190
arma::mat & InputParameter()
Modify the input parameter.
Definition: concat.hpp:187
Implementation of the Concat class.
Definition: concat.hpp:45
bool Run() const
Get the value of run parameter.
Definition: concat.hpp:181
OutputParameterVisitor exposes the output parameter of the given module.
arma::mat & OutputParameter()
Modify the output parameter.
Definition: concat.hpp:192
void Forward(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:183
void Backward(const arma::Mat< eT > &&, 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.
arma::mat & Delta()
Modify the delta.
Definition: concat.hpp:197
void serialize(Archive &, const unsigned int)
Serialize the layer.
DeltaVisitor exposes the delta parameter of the given module.
arma::mat const & Gradient() const
Get the gradient.
Definition: concat.hpp:200
const arma::mat & Parameters() const
Return the initial point for the optimization.
Definition: concat.hpp:176
void Add(Args... args)
Definition: concat.hpp:155