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 = true, const bool same = true);
54 
58  ~Concat();
59 
67  template<typename eT>
68  void Forward(arma::Mat<eT>&& input, arma::Mat<eT>&& output);
69 
79  template<typename eT>
80  void Backward(const arma::Mat<eT>&& /* input */,
81  arma::Mat<eT>&& gy,
82  arma::Mat<eT>&& g);
83 
84  /*
85  * Calculate the gradient using the output delta and the input activation.
86  *
87  * @param input The input parameter used for calculating the gradient.
88  * @param error The calculated error.
89  * @param gradient The calculated gradient.
90  */
91  template<typename eT>
92  void Gradient(arma::Mat<eT>&& /* input */,
93  arma::Mat<eT>&& error,
94  arma::Mat<eT>&& /* gradient */);
95 
96  /*
97  * Add a new module to the model.
98  *
99  * @param layer The Layer to be added to the model.
100  */
101  template<typename LayerType>
102  void Add(const LayerType& layer) { network.push_back(new LayerType(layer)); }
103 
104  /*
105  * Add a new module to the model.
106  *
107  * @param args The layer parameter.
108  */
109  template <class LayerType, class... Args>
110  void Add(Args... args) { network.push_back(new LayerType(args...)); }
111 
112  /*
113  * Add a new module to the model.
114  *
115  * @param layer The Layer to be added to the model.
116  */
117  void Add(LayerTypes<CustomLayers...> layer) { network.push_back(layer); }
118 
120  std::vector<LayerTypes<CustomLayers...> >& Model()
121  {
122  if (model)
123  {
124  return network;
125  }
126 
127  return empty;
128  }
129 
131  const arma::mat& Parameters() const { return parameters; }
133  arma::mat& Parameters() { return parameters; }
134 
135  arma::mat const& InputParameter() const { return inputParameter; }
137  arma::mat& InputParameter() { return inputParameter; }
138 
140  arma::mat const& OutputParameter() const { return outputParameter; }
142  arma::mat& OutputParameter() { return outputParameter; }
143 
145  arma::mat const& Delta() const { return delta; }
147  arma::mat& Delta() { return delta; }
148 
150  arma::mat const& Gradient() const { return gradient; }
152  arma::mat& Gradient() { return gradient; }
153 
157  template<typename Archive>
158  void serialize(Archive& /* ar */, const unsigned int /* version */);
159 
160  private:
162  bool model;
163 
165  bool same;
166 
168  std::vector<LayerTypes<CustomLayers...> > network;
169 
171  arma::mat parameters;
172 
174  DeltaVisitor deltaVisitor;
175 
177  OutputParameterVisitor outputParameterVisitor;
178 
180  DeleteVisitor deleteVisitor;
181 
183  std::vector<LayerTypes<CustomLayers...> > empty;
184 
186  arma::mat delta;
187 
189  arma::mat inputParameter;
190 
192  arma::mat outputParameter;
193 
195  arma::mat gradient;
196 }; // class Concat
197 
198 } // namespace ann
199 } // namespace mlpack
200 
201 // Include implementation.
202 #include "concat_impl.hpp"
203 
204 #endif
DeleteVisitor executes the destructor of the instantiated object.
arma::mat const & InputParameter() const
Definition: concat.hpp:135
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 > *, BatchNorm< arma::mat, arma::mat > *, BilinearInterpolation< arma::mat, arma::mat > *, Concat< 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 > *, Select< arma::mat, arma::mat > *, Sequential< arma::mat, arma::mat > *, VRClassReward< arma::mat, arma::mat > *, CustomLayers *... > LayerTypes
arma::mat & Parameters()
Modify the initial point for the optimization.
Definition: concat.hpp:133
.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:102
void Add(LayerTypes< CustomLayers... > layer)
Definition: concat.hpp:117
std::vector< LayerTypes< CustomLayers... > > & Model()
Return the model modules.
Definition: concat.hpp:120
arma::mat & Gradient()
Modify the gradient.
Definition: concat.hpp:152
arma::mat const & Delta() const
Get the delta.e.
Definition: concat.hpp:145
arma::mat const & OutputParameter() const
Get the output parameter.
Definition: concat.hpp:140
arma::mat & InputParameter()
Modify the input parameter.
Definition: concat.hpp:137
Implementation of the Concat class.
Definition: concat.hpp:44
OutputParameterVisitor exposes the output parameter of the given module.
arma::mat & OutputParameter()
Modify the output parameter.
Definition: concat.hpp:142
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...
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:147
void serialize(Archive &, const unsigned int)
Serialize the layer.
DeltaVisitor exposes the delta parameter of the given module.
Concat(const bool model=true, const bool same=true)
Create the Concat object using the specified parameters.
arma::mat const & Gradient() const
Get the gradient.
Definition: concat.hpp:150
const arma::mat & Parameters() const
Return the initial point for the optimization.
Definition: concat.hpp:131
void Add(Args... args)
Definition: concat.hpp:110