mlpack 3.4.2
linear.hpp
Go to the documentation of this file.
1
13#ifndef MLPACK_METHODS_ANN_LAYER_LINEAR_HPP
14#define MLPACK_METHODS_ANN_LAYER_LINEAR_HPP
15
16#include <mlpack/prereqs.hpp>
18
19#include "layer_types.hpp"
20
21namespace mlpack {
22namespace ann {
23
33template <
34 typename InputDataType = arma::mat,
35 typename OutputDataType = arma::mat,
36 typename RegularizerType = NoRegularizer
37>
38class Linear
39{
40 public:
43
51 Linear(const size_t inSize,
52 const size_t outSize,
53 RegularizerType regularizer = RegularizerType());
54
56 Linear(const Linear& layer);
57
60
62 Linear& operator=(const Linear& layer);
63
66
67 /*
68 * Reset the layer parameter.
69 */
70 void Reset();
71
79 template<typename eT>
80 void Forward(const arma::Mat<eT>& input, arma::Mat<eT>& output);
81
91 template<typename eT>
92 void Backward(const arma::Mat<eT>& /* input */,
93 const arma::Mat<eT>& gy,
94 arma::Mat<eT>& g);
95
96 /*
97 * Calculate the gradient using the output delta and the input activation.
98 *
99 * @param input The input parameter used for calculating the gradient.
100 * @param error The calculated error.
101 * @param gradient The calculated gradient.
102 */
103 template<typename eT>
104 void Gradient(const arma::Mat<eT>& input,
105 const arma::Mat<eT>& error,
106 arma::Mat<eT>& gradient);
107
109 OutputDataType const& Parameters() const { return weights; }
111 OutputDataType& Parameters() { return weights; }
112
114 InputDataType const& InputParameter() const { return inputParameter; }
116 InputDataType& InputParameter() { return inputParameter; }
117
119 OutputDataType const& OutputParameter() const { return outputParameter; }
121 OutputDataType& OutputParameter() { return outputParameter; }
122
124 OutputDataType const& Delta() const { return delta; }
126 OutputDataType& Delta() { return delta; }
127
129 size_t InputSize() const { return inSize; }
130
132 size_t OutputSize() const { return outSize; }
133
135 OutputDataType const& Gradient() const { return gradient; }
137 OutputDataType& Gradient() { return gradient; }
138
140 OutputDataType const& Weight() const { return weight; }
142 OutputDataType& Weight() { return weight; }
143
145 OutputDataType const& Bias() const { return bias; }
147 OutputDataType& Bias() { return bias; }
148
150 size_t WeightSize() const
151 {
152 return (inSize * outSize) + outSize;
153 }
154
158 template<typename Archive>
159 void serialize(Archive& ar, const unsigned int /* version */);
160
161 private:
163 size_t inSize;
164
166 size_t outSize;
167
169 OutputDataType weights;
170
172 OutputDataType weight;
173
175 OutputDataType bias;
176
178 OutputDataType delta;
179
181 OutputDataType gradient;
182
184 InputDataType inputParameter;
185
187 OutputDataType outputParameter;
188
190 RegularizerType regularizer;
191}; // class Linear
192
193} // namespace ann
194} // namespace mlpack
195
196// Include implementation.
197#include "linear_impl.hpp"
198
199#endif
Implementation of the Linear layer class.
Definition: linear.hpp:39
Linear(const Linear &layer)
Copy constructor.
OutputDataType const & Delta() const
Get the delta.
Definition: linear.hpp:124
Linear(const size_t inSize, const size_t outSize, RegularizerType regularizer=RegularizerType())
Create the Linear layer object using the specified number of units.
OutputDataType const & Parameters() const
Get the parameters.
Definition: linear.hpp:109
Linear & operator=(Linear &&layer)
Move assignment operator.
OutputDataType const & Weight() const
Get the weight of the layer.
Definition: linear.hpp:140
Linear(Linear &&)
Move constructor.
void Forward(const arma::Mat< eT > &input, arma::Mat< eT > &output)
Ordinary feed forward pass of a neural network, evaluating the function f(x) by propagating the activ...
OutputDataType & Weight()
Modify the weight of the layer.
Definition: linear.hpp:142
OutputDataType const & Bias() const
Get the bias of the layer.
Definition: linear.hpp:145
InputDataType & InputParameter()
Modify the input parameter.
Definition: linear.hpp:116
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: linear.hpp:119
size_t InputSize() const
Get the input size.
Definition: linear.hpp:129
Linear()
Create the Linear object.
size_t WeightSize() const
Get the size of the weights.
Definition: linear.hpp:150
size_t OutputSize() const
Get the output size.
Definition: linear.hpp:132
Linear & operator=(const Linear &layer)
Copy assignment operator.
void Gradient(const arma::Mat< eT > &input, const arma::Mat< eT > &error, arma::Mat< eT > &gradient)
OutputDataType const & Gradient() const
Get the gradient.
Definition: linear.hpp:135
InputDataType const & InputParameter() const
Get the input parameter.
Definition: linear.hpp:114
OutputDataType & Bias()
Modify the bias weights of the layer.
Definition: linear.hpp:147
OutputDataType & Gradient()
Modify the gradient.
Definition: linear.hpp:137
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: linear.hpp:121
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType & Parameters()
Modify the parameters.
Definition: linear.hpp:111
OutputDataType & Delta()
Modify the delta.
Definition: linear.hpp:126
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.