mlpack  master
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 
38 template <
39  typename InputDataType = arma::mat,
40  typename OutputDataType = arma::mat
41 >
42 class Concat
43 {
44  public:
51  Concat(const bool model = true, const bool same = true);
52 
60  template<typename eT>
61  void Forward(arma::Mat<eT>&& input, arma::Mat<eT>&& output);
62 
72  template<typename eT>
73  void Backward(const arma::Mat<eT>&& /* input */,
74  arma::Mat<eT>&& gy,
75  arma::Mat<eT>&& g);
76 
77  /*
78  * Calculate the gradient using the output delta and the input activation.
79  *
80  * @param input The input parameter used for calculating the gradient.
81  * @param error The calculated error.
82  * @param gradient The calculated gradient.
83  */
84  template<typename eT>
85  void Gradient(arma::Mat<eT>&& /* input */,
86  arma::Mat<eT>&& error,
87  arma::Mat<eT>&& /* gradient */);
88 
89  /*
90  * Add a new module to the model.
91  *
92  * @param args The layer parameter.
93  */
94  template <class LayerType, class... Args>
95  void Add(Args... args) { network.push_back(new LayerType(args...)); }
96 
97  /*
98  * Add a new module to the model.
99  *
100  * @param layer The Layer to be added to the model.
101  */
102  void Add(LayerTypes layer) { network.push_back(layer); }
103 
105  std::vector<LayerTypes>& Model()
106  {
107  if (model)
108  {
109  return network;
110  }
111 
112  return empty;
113  }
114 
116  const arma::mat& Parameters() const { return parameters; }
118  arma::mat& Parameters() { return parameters; }
119 
120  arma::mat const& InputParameter() const { return inputParameter; }
122  arma::mat& InputParameter() { return inputParameter; }
123 
125  arma::mat const& OutputParameter() const { return outputParameter; }
127  arma::mat& OutputParameter() { return outputParameter; }
128 
130  arma::mat const& Delta() const { return delta; }
132  arma::mat& Delta() { return delta; }
133 
135  arma::mat const& Gradient() const { return gradient; }
137  arma::mat& Gradient() { return gradient; }
138 
142  template<typename Archive>
143  void Serialize(Archive& /* ar */, const unsigned int /* version */);
144 
145  private:
147  bool model;
148 
150  bool same;
151 
153  std::vector<LayerTypes> network;
154 
156  arma::mat parameters;
157 
160 
163 
166 
168  std::vector<LayerTypes> empty;
169 
171  arma::mat delta;
172 
174  arma::mat inputParameter;
175 
177  arma::mat outputParameter;
178 
180  arma::mat gradient;
181 }; // class Concat
182 
183 } // namespace ann
184 } // namespace mlpack
185 
186 // Include implementation.
187 #include "concat_impl.hpp"
188 
189 #endif
DeleteVisitor executes the destructor of the instantiated object.
void Serialize(Archive &, const unsigned int)
Serialize the layer.
const arma::mat & Parameters() const
Return the initial point for the optimization.
Definition: concat.hpp:116
arma::mat outputParameter
Locally-stored output parameter object.
Definition: concat.hpp:177
bool same
If true merge the error in the backward pass.
Definition: concat.hpp:150
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
arma::mat parameters
Locally-stored model parameters.
Definition: concat.hpp:156
DeltaVisitor deltaVisitor
Locally-stored delta visitor.
Definition: concat.hpp:159
DeleteVisitor deleteVisitor
Locally-stored delete visitor.
Definition: concat.hpp:165
The core includes that mlpack expects; standard C++ includes and Armadillo.
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.
void Add(LayerTypes layer)
Definition: concat.hpp:102
std::vector< LayerTypes > & Model()
Return the model modules.
Definition: concat.hpp:105
Concat(const bool model=true, const bool same=true)
Create the Concat object using the specified parameters.
Implementation of the Concat class.
Definition: concat.hpp:42
arma::mat & Delta()
Modify the delta.
Definition: concat.hpp:132
OutputParameterVisitor exposes the output parameter of the given module.
arma::mat & InputParameter()
Modify the input parameter.
Definition: concat.hpp:122
arma::mat gradient
Locally-stored gradient object.
Definition: concat.hpp:180
arma::mat const & InputParameter() const
Definition: concat.hpp:120
boost::variant< Add< arma::mat, arma::mat > *, AddMerge< 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 > *, 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 > *, DropConnect< arma::mat, arma::mat > *, Dropout< arma::mat, arma::mat > *, Glimpse< arma::mat, arma::mat > *, HardTanH< arma::mat, arma::mat > *, Join< 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 > *, MaxPooling< arma::mat, arma::mat > *, MeanPooling< arma::mat, arma::mat > *, MeanSquaredError< arma::mat, arma::mat > *, MultiplyConstant< 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 > * > LayerTypes
arma::mat inputParameter
Locally-stored input parameter object.
Definition: concat.hpp:174
arma::mat const & OutputParameter() const
Get the output parameter.
Definition: concat.hpp:125
bool model
Parameter which indicates if the modules should be exposed.
Definition: concat.hpp:147
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 Add(Args...args)
Definition: concat.hpp:95
DeltaVisitor exposes the delta parameter of the given module.
arma::mat & OutputParameter()
Modify the output parameter.
Definition: concat.hpp:127
arma::mat const & Delta() const
Get the delta.e.
Definition: concat.hpp:130
std::vector< LayerTypes > empty
Locally-stored empty list of modules.
Definition: concat.hpp:168
std::vector< LayerTypes > network
Locally-stored network modules.
Definition: concat.hpp:153
arma::mat & Parameters()
Modify the initial point for the optimization.
Definition: concat.hpp:118
arma::mat & Gradient()
Modify the gradient.
Definition: concat.hpp:137
arma::mat const & Gradient() const
Get the gradient.
Definition: concat.hpp:135
arma::mat delta
Locally-stored delta object.
Definition: concat.hpp:171
OutputParameterVisitor outputParameterVisitor
Locally-stored output parameter visitor.
Definition: concat.hpp:162