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 
156  size_t InputShape() const
157  {
158  return inSize;
159  }
160 
164  template<typename Archive>
165  void serialize(Archive& ar, const uint32_t /* version */);
166 
167  private:
169  size_t inSize;
170 
172  size_t outSize;
173 
175  OutputDataType weights;
176 
178  OutputDataType weight;
179 
181  OutputDataType bias;
182 
184  OutputDataType delta;
185 
187  OutputDataType gradient;
188 
190  InputDataType inputParameter;
191 
193  OutputDataType outputParameter;
194 
196  RegularizerType regularizer;
197 }; // class Linear
198 
199 } // namespace ann
200 } // namespace mlpack
201 
202 // Include implementation.
203 #include "linear_impl.hpp"
204 
205 #endif
OutputDataType const & Delta() const
Get the delta.
Definition: linear.hpp:124
size_t WeightSize() const
Get the size of the weights.
Definition: linear.hpp:150
Linear algebra utility functions, generally performed on matrices or vectors.
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...
size_t OutputSize() const
Get the output size.
Definition: linear.hpp:132
The core includes that mlpack expects; standard C++ includes and Armadillo.
OutputDataType & Bias()
Modify the bias weights of the layer.
Definition: linear.hpp:147
Linear & operator=(const Linear &layer)
Copy assignment operator.
size_t InputShape() const
Get the shape of the input.
Definition: linear.hpp:156
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...
OutputDataType const & Weight() const
Get the weight of the layer.
Definition: linear.hpp:140
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
OutputDataType const & Gradient() const
Get the gradient.
Definition: linear.hpp:135
OutputDataType const & Bias() const
Get the bias of the layer.
Definition: linear.hpp:145
OutputDataType const & Parameters() const
Get the parameters.
Definition: linear.hpp:109
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
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.
InputDataType const & InputParameter() const
Get the input parameter.
Definition: linear.hpp:114