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 
54  MultiplyMerge(const MultiplyMerge& layer);
55 
58 
60  MultiplyMerge& operator=(const MultiplyMerge& layer);
61 
64 
67 
75  template<typename InputType, typename OutputType>
76  void Forward(const InputType& /* input */, OutputType& output);
77 
87  template<typename eT>
88  void Backward(const arma::Mat<eT>& /* input */,
89  const arma::Mat<eT>& gy,
90  arma::Mat<eT>& g);
91 
92  /*
93  * Calculate the gradient using the output delta and the input activation.
94  *
95  * @param input The input parameter used for calculating the gradient.
96  * @param error The calculated error.
97  * @param gradient The calculated gradient.
98  */
99  template<typename eT>
100  void Gradient(const arma::Mat<eT>& input,
101  const arma::Mat<eT>& error,
102  arma::Mat<eT>& gradient);
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  OutputDataType const& OutputParameter() const { return outputParameter; }
122  OutputDataType& OutputParameter() { return outputParameter; }
123 
125  OutputDataType const& Delta() const { return delta; }
127  OutputDataType& Delta() { return delta; }
128 
130  OutputDataType const& Gradient() const { return gradient; }
132  OutputDataType& Gradient() { return gradient; }
133 
135  std::vector<LayerTypes<CustomLayers...> >& Model()
136  {
137  if (model)
138  {
139  return network;
140  }
141 
142  return empty;
143  }
144 
146  OutputDataType const& Parameters() const { return weights; }
148  OutputDataType& Parameters() { return weights; }
149 
151  size_t WeightSize() const { return 0; }
152 
156  template<typename Archive>
157  void serialize(Archive& ar, const uint32_t /* version */);
158 
159  private:
161  bool model;
162 
165  bool run;
166 
168  bool ownsLayer;
169 
171  std::vector<LayerTypes<CustomLayers...> > network;
172 
174  std::vector<LayerTypes<CustomLayers...> > empty;
175 
177  DeleteVisitor deleteVisitor;
178 
180  OutputParameterVisitor outputParameterVisitor;
181 
183  DeltaVisitor deltaVisitor;
184 
186  OutputDataType delta;
187 
189  OutputDataType gradient;
190 
192  OutputDataType outputParameter;
193 
195  OutputDataType weights;
196 }; // class MultiplyMerge
197 
198 } // namespace ann
199 } // namespace mlpack
200 
201 // Include implementation.
202 #include "multiply_merge_impl.hpp"
203 
204 #endif
OutputDataType & Delta()
Modify the delta.
DeleteVisitor executes the destructor of the instantiated object.
OutputDataType const & Gradient() const
Get the gradient.
Linear algebra utility functions, generally performed on matrices or vectors.
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.
void Backward(const arma::Mat< eT > &, const 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...
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.
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
void Forward(const InputType &, OutputType &output)
Ordinary feed forward pass of a neural network, evaluating the function f(x) by propagating the activ...
OutputDataType const & Parameters() const
Get the parameters.
void Add(LayerTypes< CustomLayers... > layer)
size_t WeightSize() const
Get the size of the weights.
OutputDataType & OutputParameter()
Modify the output parameter.
OutputDataType & Parameters()
Modify the parameters.
DeltaVisitor exposes the delta parameter of the given module.
boost::variant< AdaptiveMaxPooling< arma::mat, arma::mat > *, AdaptiveMeanPooling< arma::mat, arma::mat > *, Add< arma::mat, arma::mat > *, AddMerge< arma::mat, arma::mat > *, AlphaDropout< 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< SoftplusFunction, arma::mat, arma::mat > *, BaseLayer< RectifierFunction, arma::mat, arma::mat > *, BatchNorm< arma::mat, arma::mat > *, BilinearInterpolation< arma::mat, arma::mat > *, CELU< 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 > *, CReLU< arma::mat, arma::mat > *, DropConnect< arma::mat, arma::mat > *, Dropout< arma::mat, arma::mat > *, ELU< arma::mat, arma::mat > *, FastLSTM< arma::mat, arma::mat > *, GRU< 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, NoRegularizer > *, LinearNoBias< arma::mat, arma::mat, NoRegularizer > *, 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 > *, MiniBatchDiscrimination< arma::mat, arma::mat > *, MultiplyConstant< arma::mat, arma::mat > *, MultiplyMerge< arma::mat, arma::mat > *, NegativeLogLikelihood< arma::mat, arma::mat > *, NoisyLinear< arma::mat, arma::mat > *, Padding< arma::mat, arma::mat > *, PReLU< arma::mat, arma::mat > *, Sequential< arma::mat, arma::mat, false > *, Sequential< arma::mat, arma::mat, true > *, Softmax< arma::mat, arma::mat > *, TransposedConvolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > *, WeightNorm< arma::mat, arma::mat > *, MoreTypes, CustomLayers *... > LayerTypes
MultiplyMerge(const bool model=false, const bool run=true)
Create the MultiplyMerge object using the specified parameters.
MultiplyMerge & operator=(const MultiplyMerge &layer)
Copy assignment operator.