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 
50  Linear(const size_t inSize,
51  const size_t outSize,
52  RegularizerType regularizer = RegularizerType());
53 
54  /*
55  * Reset the layer parameter.
56  */
57  void Reset();
58 
66  template<typename eT>
67  void Forward(const arma::Mat<eT>&& input, arma::Mat<eT>&& output);
68 
78  template<typename eT>
79  void Backward(const arma::Mat<eT>&& /* input */,
80  arma::Mat<eT>&& gy,
81  arma::Mat<eT>&& g);
82 
83  /*
84  * Calculate the gradient using the output delta and the input activation.
85  *
86  * @param input The input parameter used for calculating the gradient.
87  * @param error The calculated error.
88  * @param gradient The calculated gradient.
89  */
90  template<typename eT>
91  void Gradient(const arma::Mat<eT>&& input,
92  arma::Mat<eT>&& error,
93  arma::Mat<eT>&& gradient);
94 
96  OutputDataType const& Parameters() const { return weights; }
98  OutputDataType& Parameters() { return weights; }
99 
101  InputDataType const& InputParameter() const { return inputParameter; }
103  InputDataType& InputParameter() { return inputParameter; }
104 
106  OutputDataType const& OutputParameter() const { return outputParameter; }
108  OutputDataType& OutputParameter() { return outputParameter; }
109 
111  OutputDataType const& Delta() const { return delta; }
113  OutputDataType& Delta() { return delta; }
114 
116  OutputDataType const& Gradient() const { return gradient; }
118  OutputDataType& Gradient() { return gradient; }
119 
121  arma::mat& Bias() { return bias; }
122 
126  template<typename Archive>
127  void serialize(Archive& ar, const unsigned int /* version */);
128 
129  private:
131  size_t inSize;
132 
134  size_t outSize;
135 
137  OutputDataType weights;
138 
140  OutputDataType weight;
141 
143  OutputDataType bias;
144 
146  OutputDataType delta;
147 
149  OutputDataType gradient;
150 
152  InputDataType inputParameter;
153 
155  OutputDataType outputParameter;
156 
158  RegularizerType regularizer;
159 }; // class Linear
160 
161 } // namespace ann
162 } // namespace mlpack
163 
164 // Include implementation.
165 #include "linear_impl.hpp"
166 
167 #endif
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType const & Delta() const
Get the delta.
Definition: linear.hpp:111
.hpp
Definition: add_to_po.hpp:21
void Backward(const arma::Mat< eT > &&, 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 & Parameters()
Modify the parameters.
Definition: linear.hpp:98
The core includes that mlpack expects; standard C++ includes and Armadillo.
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...
arma::mat & Bias()
Modify the bias weights of the layer.
Definition: linear.hpp:121
OutputDataType & Gradient()
Modify the gradient.
Definition: linear.hpp:118
OutputDataType const & Gradient() const
Get the gradient.
Definition: linear.hpp:116
OutputDataType const & Parameters() const
Get the parameters.
Definition: linear.hpp:96
InputDataType & InputParameter()
Modify the input parameter.
Definition: linear.hpp:103
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: linear.hpp:106
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: linear.hpp:108
OutputDataType & Delta()
Modify the delta.
Definition: linear.hpp:113
Linear()
Create the Linear object.
InputDataType const & InputParameter() const
Get the input parameter.
Definition: linear.hpp:101