mlpack  3.4.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 
21 namespace mlpack {
22 namespace ann {
23 
33 template <
34  typename InputDataType = arma::mat,
35  typename OutputDataType = arma::mat,
36  typename RegularizerType = NoRegularizer
37 >
38 class Linear
39 {
40  public:
42  Linear();
43 
51  Linear(const size_t inSize,
52  const size_t outSize,
53  RegularizerType regularizer = RegularizerType());
54 
56  Linear(const Linear& layer);
57 
59  Linear(Linear&&);
60 
62  Linear& operator=(const Linear& layer);
63 
65  Linear& operator=(Linear&& layer);
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
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
InputDataType const & InputParameter() const
Get the input parameter.
Definition: linear.hpp:114
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: linear.hpp:119
OutputDataType const & Bias() const
Get the bias of the layer.
Definition: linear.hpp:145
OutputDataType & Parameters()
Modify the parameters.
Definition: linear.hpp:111
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...
The core includes that mlpack expects; standard C++ includes and Armadillo.
OutputDataType const & Weight() const
Get the weight of the layer.
Definition: linear.hpp:140
size_t WeightSize() const
Get the size of the weights.
Definition: linear.hpp:150
OutputDataType const & Gradient() const
Get the gradient.
Definition: linear.hpp:135
OutputDataType & Bias()
Modify the bias weights of the layer.
Definition: linear.hpp:147
OutputDataType const & Parameters() const
Get the parameters.
Definition: linear.hpp:109
Linear & operator=(const Linear &layer)
Copy assignment operator.
OutputDataType & Gradient()
Modify the gradient.
Definition: linear.hpp:137
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...
InputDataType & InputParameter()
Modify the input parameter.
Definition: linear.hpp:116
size_t OutputSize() const
Get the output size.
Definition: linear.hpp:132
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: linear.hpp:121
OutputDataType & Weight()
Modify the weight of the layer.
Definition: linear.hpp:142
OutputDataType & Delta()
Modify the delta.
Definition: linear.hpp:126
Linear()
Create the Linear object.
size_t InputSize() const
Get the input size.
Definition: linear.hpp:129
OutputDataType const & Delta() const
Get the delta.
Definition: linear.hpp:124