mlpack 3.4.2
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
24namespace mlpack {
25namespace ann {
26
37template<
38 typename InputDataType = arma::mat,
39 typename OutputDataType = arma::mat,
40 typename... CustomLayers
41>
43{
44 public:
51 MultiplyMerge(const bool model = false, const bool run = true);
52
55
63 template<typename InputType, typename OutputType>
64 void Forward(const InputType& /* input */, OutputType& output);
65
75 template<typename eT>
76 void Backward(const arma::Mat<eT>& /* input */,
77 const 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(const arma::Mat<eT>& input,
89 const 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
DeleteVisitor executes the destructor of the instantiated object.
DeltaVisitor exposes the delta parameter of the given module.
Implementation of the MultiplyMerge module class.
OutputDataType const & Delta() const
Get the delta.
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.
MultiplyMerge(const bool model=false, const bool run=true)
Create the MultiplyMerge object using the specified parameters.
OutputDataType const & OutputParameter() const
Get the output parameter.
void Add(LayerTypes< CustomLayers... > layer)
std::vector< LayerTypes< CustomLayers... > > & Model()
Return the model modules.
void Gradient(const arma::Mat< eT > &input, const arma::Mat< eT > &error, arma::Mat< eT > &gradient)
OutputDataType const & Gradient() const
Get the gradient.
OutputDataType & Gradient()
Modify the gradient.
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 & OutputParameter()
Modify the output parameter.
~MultiplyMerge()
Destructor to release allocated memory.
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType & Parameters()
Modify the parameters.
OutputDataType & Delta()
Modify the delta.
OutputParameterVisitor exposes the output 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 > *, FlexibleReLU< 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 > *, Softmax< arma::mat, arma::mat > *, SpatialDropout< arma::mat, arma::mat > *, TransposedConvolution< NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, NaiveConvolution< ValidConvolution >, arma::mat, arma::mat > *, WeightNorm< arma::mat, arma::mat > *, MoreTypes, CustomLayers *... > LayerTypes
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
The core includes that mlpack expects; standard C++ includes and Armadillo.