add_merge.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_ANN_LAYER_ADD_MERGE_HPP
14 #define MLPACK_METHODS_ANN_LAYER_ADD_MERGE_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 
37 template<
38  typename InputDataType = arma::mat,
39  typename OutputDataType = arma::mat,
40  typename... CustomLayers
41 >
42 class AddMerge
43 {
44  public:
50  AddMerge(const bool model = false);
51 
53  ~AddMerge();
54 
62  template<typename InputType, typename OutputType>
63  void Forward(const InputType&& /* input */, OutputType&& output);
64 
74  template<typename eT>
75  void Backward(const arma::Mat<eT>&& /* input */,
76  arma::Mat<eT>&& gy,
77  arma::Mat<eT>&& g);
78 
79  /*
80  * Add a new module to the model.
81  *
82  * @param layer The Layer to be added to the model.
83  */
84  void Add(LayerTypes<CustomLayers...> layer) { network.push_back(layer); }
85 
86  /*
87  * Add a new module to the model.
88  *
89  * @param layer The Layer to be added to the model.
90  */
91  template<typename LayerType>
92  void Add(const LayerType& layer) { network.push_back(new LayerType(layer)); }
93 
94  /*
95  * Add a new module to the model.
96  *
97  * @param args The layer parameter.
98  */
99  template <class LayerType, class... Args>
100  void Add(Args... args) { network.push_back(new LayerType(args...)); }
101 
103  InputDataType const& InputParameter() const { return inputParameter; }
105  InputDataType& InputParameter() { return inputParameter; }
106 
108  OutputDataType const& OutputParameter() const { return outputParameter; }
110  OutputDataType& OutputParameter() { return outputParameter; }
111 
113  OutputDataType const& Delta() const { return delta; }
115  OutputDataType& Delta() { return delta; }
116 
118  std::vector<LayerTypes<CustomLayers...> >& Model()
119  {
120  if (model)
121  {
122  return network;
123  }
124 
125  return empty;
126  }
127 
131  template<typename Archive>
132  void serialize(Archive& ar, const unsigned int /* version */);
133 
134  private:
136  bool model;
137 
139  bool ownsLayer;
140 
142  std::vector<LayerTypes<CustomLayers...> > network;
143 
145  std::vector<LayerTypes<CustomLayers...> > empty;
146 
148  DeleteVisitor deleteVisitor;
149 
151  OutputParameterVisitor outputParameterVisitor;
152 
154  DeltaVisitor deltaVisitor;
155 
157  OutputDataType delta;
158 
160  InputDataType inputParameter;
161 
163  OutputDataType outputParameter;
164 }; // class AddMerge
165 
166 } // namespace ann
167 } // namespace mlpack
168 
169 // Include implementation.
170 #include "add_merge_impl.hpp"
171 
172 #endif
DeleteVisitor executes the destructor of the instantiated object.
InputDataType const & InputParameter() const
Get the input parameter.
Definition: add_merge.hpp:103
Implementation of the AddMerge module class.
Definition: add_merge.hpp:42
.hpp
Definition: add_to_po.hpp:21
The core includes that mlpack expects; standard C++ includes and Armadillo.
OutputDataType const & Delta() const
Get the delta.
Definition: add_merge.hpp:113
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
AddMerge(const bool model=false)
Create the AddMerge object using the specified parameters.
void Forward(const InputType &&, OutputType &&output)
Ordinary feed forward pass of a neural network, evaluating the function f(x) by propagating the activ...
void Add(Args... args)
Definition: add_merge.hpp:100
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: add_merge.hpp:110
OutputDataType & Delta()
Modify the delta.
Definition: add_merge.hpp:115
OutputParameterVisitor exposes the output parameter of the given module.
void Add(const LayerType &layer)
Definition: add_merge.hpp:92
InputDataType & InputParameter()
Modify the input parameter.
Definition: add_merge.hpp:105
DeltaVisitor exposes the delta parameter of the given module.
~AddMerge()
Destructor to release allocated memory.
std::vector< LayerTypes< CustomLayers... > > & Model()
Return the model modules.
Definition: add_merge.hpp:118
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: add_merge.hpp:108
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 > *, 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 > *, CrossEntropyError< arma::mat, arma::mat > *, DropConnect< arma::mat, arma::mat > *, Dropout< arma::mat, arma::mat > *, ELU< 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 > *, GRU< arma::mat, arma::mat > *, FastLSTM< 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 > *, SigmoidCrossEntropyError< arma::mat, arma::mat > *, Select< arma::mat, arma::mat > *, Sequential< arma::mat, arma::mat > *, VRClassReward< arma::mat, arma::mat > *, CustomLayers *... > LayerTypes
void Add(LayerTypes< CustomLayers... > layer)
Definition: add_merge.hpp:84
void Backward(const arma::Mat< eT > &&, arma::Mat< eT > &&gy, arma::Mat< eT > &&g)
Ordinary feed backward pass of a neural network, calculating the function f(x) by propagating x backw...