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  size_t InputSize() const { return inSize; }
117 
119  size_t OutputSize() const { return outSize; }
120 
122  OutputDataType const& Gradient() const { return gradient; }
124  OutputDataType& Gradient() { return gradient; }
125 
127  arma::mat& Bias() { return bias; }
128 
132  template<typename Archive>
133  void serialize(Archive& ar, const unsigned int /* version */);
134 
135  private:
137  size_t inSize;
138 
140  size_t outSize;
141 
143  OutputDataType weights;
144 
146  OutputDataType weight;
147 
149  OutputDataType bias;
150 
152  OutputDataType delta;
153 
155  OutputDataType gradient;
156 
158  InputDataType inputParameter;
159 
161  OutputDataType outputParameter;
162 
164  RegularizerType regularizer;
165 }; // class Linear
166 
167 } // namespace ann
168 } // namespace mlpack
169 
170 // Include implementation.
171 #include "linear_impl.hpp"
172 
173 #endif
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType const & Delta() const
Get the delta.
Definition: linear.hpp:111
strip_type.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
size_t OutputSize() const
Get the output size.
Definition: linear.hpp:119
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:127
OutputDataType & Gradient()
Modify the gradient.
Definition: linear.hpp:124
OutputDataType const & Gradient() const
Get the gradient.
Definition: linear.hpp:122
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
size_t InputSize() const
Get the input size.
Definition: linear.hpp:116
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