mlpack 3.4.2
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
24namespace mlpack {
25namespace ann {
26
37template<
38 typename InputDataType = arma::mat,
39 typename OutputDataType = arma::mat,
40 typename... CustomLayers
41>
43{
44 public:
51 AddMerge(const bool model = false, const bool run = true);
52
60 AddMerge(const bool model, const bool run, const bool ownsLayers);
61
64
72 template<typename InputType, typename OutputType>
73 void Forward(const InputType& /* input */, OutputType& output);
74
84 template<typename eT>
85 void Backward(const arma::Mat<eT>& /* input */,
86 const arma::Mat<eT>& gy,
87 arma::Mat<eT>& g);
88
98 template<typename eT>
99 void Backward(const arma::Mat<eT>& /* input */,
100 const arma::Mat<eT>& gy,
101 arma::Mat<eT>& g,
102 const size_t index);
103
104 /*
105 * Calculate the gradient using the output delta and the input activation.
106 *
107 * @param input The input parameter used for calculating the gradient.
108 * @param error The calculated error.
109 * @param gradient The calculated gradient.
110 */
111 template<typename eT>
112 void Gradient(const arma::Mat<eT>& input,
113 const arma::Mat<eT>& error,
114 arma::Mat<eT>& gradient);
115
116 /*
117 * This is the overload of Gradient() that runs a specific layer with the
118 * given input.
119 *
120 * @param input The input parameter used for calculating the gradient.
121 * @param error The calculated error.
122 * @param gradient The calculated gradient.
123 * @param The index of the layer to run.
124 */
125 template<typename eT>
126 void Gradient(const arma::Mat<eT>& input,
127 const arma::Mat<eT>& error,
128 arma::Mat<eT>& gradient,
129 const size_t index);
130
131 /*
132 * Add a new module to the model.
133 *
134 * @param args The layer parameter.
135 */
136 template <class LayerType, class... Args>
137 void Add(Args... args) { network.push_back(new LayerType(args...)); }
138
139 /*
140 * Add a new module to the model.
141 *
142 * @param layer The Layer to be added to the model.
143 */
144 void Add(LayerTypes<CustomLayers...> layer) { network.push_back(layer); }
145
147 InputDataType const& InputParameter() const { return inputParameter; }
149 InputDataType& InputParameter() { return inputParameter; }
150
152 OutputDataType const& OutputParameter() const { return outputParameter; }
154 OutputDataType& OutputParameter() { return outputParameter; }
155
157 OutputDataType const& Delta() const { return delta; }
159 OutputDataType& Delta() { return delta; }
160
162 std::vector<LayerTypes<CustomLayers...> >& Model()
163 {
164 if (model)
165 {
166 return network;
167 }
168
169 return empty;
170 }
171
173 OutputDataType const& Parameters() const { return weights; }
175 OutputDataType& Parameters() { return weights; }
176
178 bool Run() const { return run; }
180 bool& Run() { return run; }
181
185 template<typename Archive>
186 void serialize(Archive& ar, const unsigned int /* version */);
187
188 private:
190 bool model;
191
194 bool run;
195
198 bool ownsLayers;
199
201 std::vector<LayerTypes<CustomLayers...> > network;
202
204 std::vector<LayerTypes<CustomLayers...> > empty;
205
207 DeleteVisitor deleteVisitor;
208
210 OutputParameterVisitor outputParameterVisitor;
211
213 DeltaVisitor deltaVisitor;
214
216 OutputDataType delta;
217
219 OutputDataType gradient;
220
222 InputDataType inputParameter;
223
225 OutputDataType outputParameter;
226
228 OutputDataType weights;
229}; // class AddMerge
230
231} // namespace ann
232} // namespace mlpack
233
235namespace boost {
236namespace serialization {
237
238template<
239 typename InputDataType,
240 typename OutputDataType,
241 typename... CustomLayers
242>
243struct version<mlpack::ann::AddMerge<
244 InputDataType, OutputDataType, CustomLayers...>>
245{
246 BOOST_STATIC_CONSTANT(int, value = 1);
247};
248
249} // namespace serialization
250} // namespace boost
251
252// Include implementation.
253#include "add_merge_impl.hpp"
254
255#endif
Implementation of the AddMerge module class.
Definition: add_merge.hpp:43
OutputDataType const & Delta() const
Get the delta.
Definition: add_merge.hpp:157
void Forward(const InputType &, OutputType &output)
Ordinary feed forward pass of a neural network, evaluating the function f(x) by propagating the activ...
bool & Run()
Modify the value of run parameter.
Definition: add_merge.hpp:180
OutputDataType const & Parameters() const
Get the parameters.
Definition: add_merge.hpp:173
void Backward(const arma::Mat< eT > &, const arma::Mat< eT > &gy, arma::Mat< eT > &g, const size_t index)
This is the overload of Backward() that runs only a specific layer with the given input.
void Gradient(const arma::Mat< eT > &input, const arma::Mat< eT > &error, arma::Mat< eT > &gradient, const size_t index)
InputDataType & InputParameter()
Modify the input parameter.
Definition: add_merge.hpp:149
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: add_merge.hpp:152
void Add(LayerTypes< CustomLayers... > layer)
Definition: add_merge.hpp:144
AddMerge(const bool model, const bool run, const bool ownsLayers)
Create the AddMerge object using the specified parameters.
std::vector< LayerTypes< CustomLayers... > > & Model()
Return the model modules.
Definition: add_merge.hpp:162
void Add(Args... args)
Definition: add_merge.hpp:137
AddMerge(const bool model=false, const bool run=true)
Create the AddMerge object using the specified parameters.
void Gradient(const arma::Mat< eT > &input, const arma::Mat< eT > &error, arma::Mat< eT > &gradient)
InputDataType const & InputParameter() const
Get the input parameter.
Definition: add_merge.hpp:147
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.
Definition: add_merge.hpp:154
~AddMerge()
Destructor to release allocated memory.
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType & Parameters()
Modify the parameters.
Definition: add_merge.hpp:175
OutputDataType & Delta()
Modify the delta.
Definition: add_merge.hpp:159
bool Run() const
Get the value of run parameter.
Definition: add_merge.hpp:178
DeleteVisitor executes the destructor of the instantiated object.
DeltaVisitor exposes the delta parameter of the given module.
OutputParameterVisitor exposes the output parameter of the given module.
Set the serialization version of the adaboost class.
Definition: adaboost.hpp:198
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.