multiply_merge.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_ANN_LAYER_MULTIPLY_MERGE_HPP
14 #define MLPACK_METHODS_ANN_LAYER_MULTIPLY_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 MultiplyMerge
43 {
44  public:
51  MultiplyMerge(const bool model = false, const bool run = true);
52 
55 
63  template<typename InputType, typename OutputType>
64  void Forward(InputType&& /* input */, OutputType&& output);
65 
75  template<typename eT>
76  void Backward(const arma::Mat<eT>&& /* input */,
77  arma::Mat<eT>&& gy,
78  arma::Mat<eT>&& g);
79 
80  /*
81  * Calculate the gradient using the output delta and the input activation.
82  *
83  * @param input The input parameter used for calculating the gradient.
84  * @param error The calculated error.
85  * @param gradient The calculated gradient.
86  */
87  template<typename eT>
88  void Gradient(arma::Mat<eT>&& input,
89  arma::Mat<eT>&& error,
90  arma::Mat<eT>&& gradient);
91 
92  /*
93  * Add a new module to the model.
94  *
95  * @param args The layer parameter.
96  */
97  template <class LayerType, class... Args>
98  void Add(Args... args) { network.push_back(new LayerType(args...)); }
99 
100  /*
101  * Add a new module to the model.
102  *
103  * @param layer The Layer to be added to the model.
104  */
105  void Add(LayerTypes<CustomLayers...> layer) { network.push_back(layer); }
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  OutputDataType const& Gradient() const { return gradient; }
120  OutputDataType& Gradient() { return gradient; }
121 
123  std::vector<LayerTypes<CustomLayers...> >& Model()
124  {
125  if (model)
126  {
127  return network;
128  }
129 
130  return empty;
131  }
132 
134  OutputDataType const& Parameters() const { return weights; }
136  OutputDataType& Parameters() { return weights; }
137 
141  template<typename Archive>
142  void serialize(Archive& ar, const unsigned int /* version */);
143 
144  private:
146  bool model;
147 
150  bool run;
151 
153  bool ownsLayer;
154 
156  std::vector<LayerTypes<CustomLayers...> > network;
157 
159  std::vector<LayerTypes<CustomLayers...> > empty;
160 
162  DeleteVisitor deleteVisitor;
163 
165  OutputParameterVisitor outputParameterVisitor;
166 
168  DeltaVisitor deltaVisitor;
169 
171  OutputDataType delta;
172 
174  OutputDataType gradient;
175 
177  OutputDataType outputParameter;
178 
180  OutputDataType weights;
181 }; // class MultiplyMerge
182 
183 } // namespace ann
184 } // namespace mlpack
185 
186 // Include implementation.
187 #include "multiply_merge_impl.hpp"
188 
189 #endif
OutputDataType & Delta()
Modify the delta.
DeleteVisitor executes the destructor of the instantiated object.
OutputDataType const & Gradient() const
Get the gradient.
strip_type.hpp
Definition: add_to_po.hpp:21
The core includes that mlpack expects; standard C++ includes and Armadillo.
OutputDataType & Gradient()
Modify the gradient.
std::vector< LayerTypes< CustomLayers... > > & Model()
Return the model modules.
OutputDataType const & Delta() const
Get the delta.
OutputParameterVisitor exposes the output parameter of the given module.
OutputDataType const & OutputParameter() const
Get the output parameter.
~MultiplyMerge()
Destructor to release allocated memory.
OutputDataType const & Parameters() const
Get the parameters.
void Add(LayerTypes< CustomLayers... > layer)
OutputDataType & OutputParameter()
Modify the output parameter.
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< ValidConvolution >, 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 > *, WeightNorm< arma::mat, arma::mat > *, MoreTypes, CustomLayers *... > LayerTypes
OutputDataType & Parameters()
Modify the parameters.
DeltaVisitor exposes the delta parameter of the given module.
void Forward(InputType &&, OutputType &&output)
Ordinary feed forward pass of a neural network, evaluating the function f(x) by propagating the activ...
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
MultiplyMerge(const bool model=false, const bool run=true)
Create the MultiplyMerge object using the specified parameters.
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...