concat.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_ANN_LAYER_CONCAT_HPP
13 #define MLPACK_METHODS_ANN_LAYER_CONCAT_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 
17 #include "../visitor/delete_visitor.hpp"
18 #include "../visitor/delta_visitor.hpp"
19 #include "../visitor/output_parameter_visitor.hpp"
20 
21 #include <boost/ptr_container/ptr_vector.hpp>
22 
23 #include "layer_types.hpp"
24 
25 namespace mlpack {
26 namespace ann {
27 
39 template <
40  typename InputDataType = arma::mat,
41  typename OutputDataType = arma::mat,
42  typename... CustomLayers
43 >
44 class Concat
45 {
46  public:
53  Concat(const bool model = false,
54  const bool run = true);
55 
59  ~Concat();
60 
68  template<typename eT>
69  void Forward(arma::Mat<eT>&& input, arma::Mat<eT>&& output);
70 
80  template<typename eT>
81  void Backward(const arma::Mat<eT>&& /* input */,
82  arma::Mat<eT>&& gy,
83  arma::Mat<eT>&& g);
84 
94  template<typename eT>
95  void Backward(const arma::Mat<eT>&& /* input */,
96  arma::Mat<eT>&& gy,
97  arma::Mat<eT>&& g,
98  const size_t index);
99 
100  /*
101  * Calculate the gradient using the output delta and the input activation.
102  *
103  * @param input The input parameter used for calculating the gradient.
104  * @param error The calculated error.
105  * @param gradient The calculated gradient.
106  */
107  template<typename eT>
108  void Gradient(arma::Mat<eT>&& /* input */,
109  arma::Mat<eT>&& error,
110  arma::Mat<eT>&& /* gradient */);
111 
112  /*
113  * This is the overload of Gradient() that runs a specific layer with the
114  * given input.
115  *
116  * @param input The input parameter used for calculating the gradient.
117  * @param error The calculated error.
118  * @param gradient The calculated gradient.
119  * @param The index of the layer to run.
120  */
121  template<typename eT>
122  void Gradient(arma::Mat<eT>&& input,
123  arma::Mat<eT>&& error,
124  arma::Mat<eT>&& gradient,
125  const size_t index);
126 
127  /*
128  * Add a new module to the model.
129  *
130  * @param layer The Layer to be added to the model.
131  */
132  template<typename LayerType>
133  void Add(const LayerType& layer) { network.push_back(new LayerType(layer)); }
134 
135  /*
136  * Add a new module to the model.
137  *
138  * @param args The layer parameter.
139  */
140  template <class LayerType, class... Args>
141  void Add(Args... args) { network.push_back(new LayerType(args...)); }
142 
143  /*
144  * Add a new module to the model.
145  *
146  * @param layer The Layer to be added to the model.
147  */
148  void Add(LayerTypes<CustomLayers...> layer) { network.push_back(layer); }
149 
151  std::vector<LayerTypes<CustomLayers...> >& Model()
152  {
153  if (model)
154  {
155  return network;
156  }
157 
158  return empty;
159  }
160 
162  const arma::mat& Parameters() const { return parameters; }
164  arma::mat& Parameters() { return parameters; }
165 
167  bool Run() const { return run; }
169  bool& Run() { return run; }
170 
171  arma::mat const& InputParameter() const { return inputParameter; }
173  arma::mat& InputParameter() { return inputParameter; }
174 
176  arma::mat const& OutputParameter() const { return outputParameter; }
178  arma::mat& OutputParameter() { return outputParameter; }
179 
181  arma::mat const& Delta() const { return delta; }
183  arma::mat& Delta() { return delta; }
184 
186  arma::mat const& Gradient() const { return gradient; }
188  arma::mat& Gradient() { return gradient; }
189 
193  template<typename Archive>
194  void serialize(Archive& /* ar */, const unsigned int /* version */);
195 
196  private:
198  bool model;
199 
202  bool run;
203 
205  std::vector<LayerTypes<CustomLayers...> > network;
206 
208  arma::mat parameters;
209 
211  DeltaVisitor deltaVisitor;
212 
214  OutputParameterVisitor outputParameterVisitor;
215 
217  DeleteVisitor deleteVisitor;
218 
220  std::vector<LayerTypes<CustomLayers...> > empty;
221 
223  arma::mat delta;
224 
226  arma::mat inputParameter;
227 
229  arma::mat outputParameter;
230 
232  arma::mat gradient;
233 }; // class Concat
234 
235 } // namespace ann
236 } // namespace mlpack
237 
238 // Include implementation.
239 #include "concat_impl.hpp"
240 
241 #endif
DeleteVisitor executes the destructor of the instantiated object.
arma::mat const & InputParameter() const
Definition: concat.hpp:171
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:164
.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:133
void Add(LayerTypes< CustomLayers... > layer)
Definition: concat.hpp:148
std::vector< LayerTypes< CustomLayers... > > & Model()
Return the model modules.
Definition: concat.hpp:151
arma::mat & Gradient()
Modify the gradient.
Definition: concat.hpp:188
arma::mat const & Delta() const
Get the delta.e.
Definition: concat.hpp:181
arma::mat const & OutputParameter() const
Get the output parameter.
Definition: concat.hpp:176
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 > *, Join< arma::mat, arma::mat > *, LayerNorm< arma::mat, arma::mat > *, LeakyReLU< arma::mat, arma::mat > *, Linear< arma::mat, arma::mat > *, LinearNoBias< arma::mat, arma::mat > *, 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 > *, MultiplyConstant< arma::mat, arma::mat > *, MultiplyMerge< arma::mat, arma::mat > *, NegativeLogLikelihood< arma::mat, arma::mat > *, PReLU< arma::mat, arma::mat > *, Recurrent< arma::mat, arma::mat > *, RecurrentAttention< arma::mat, arma::mat > *, ReinforceNormal< arma::mat, arma::mat > *, Reparametrization< arma::mat, arma::mat > *, Select< arma::mat, arma::mat > *, Sequential< arma::mat, arma::mat, false > *, Sequential< arma::mat, arma::mat, true > *, Subview< arma::mat, arma::mat > *, VRClassReward< arma::mat, arma::mat > *, CustomLayers *... > LayerTypes
arma::mat & InputParameter()
Modify the input parameter.
Definition: concat.hpp:173
Implementation of the Concat class.
Definition: concat.hpp:44
bool Run() const
Get the value of run parameter.
Definition: concat.hpp:167
OutputParameterVisitor exposes the output parameter of the given module.
arma::mat & OutputParameter()
Modify the output parameter.
Definition: concat.hpp:178
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:169
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:183
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:186
const arma::mat & Parameters() const
Return the initial point for the optimization.
Definition: concat.hpp:162
void Add(Args... args)
Definition: concat.hpp:141