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 
55  /*
56  * Reset the layer parameter.
57  */
58  void Reset();
59 
67  template<typename eT>
68  void Forward(const arma::Mat<eT>& input, arma::Mat<eT>& output);
69 
79  template<typename eT>
80  void Backward(const arma::Mat<eT>& /* input */,
81  const arma::Mat<eT>& gy,
82  arma::Mat<eT>& g);
83 
84  /*
85  * Calculate the gradient using the output delta and the input activation.
86  *
87  * @param input The input parameter used for calculating the gradient.
88  * @param error The calculated error.
89  * @param gradient The calculated gradient.
90  */
91  template<typename eT>
92  void Gradient(const arma::Mat<eT>& input,
93  const arma::Mat<eT>& error,
94  arma::Mat<eT>& gradient);
95 
97  OutputDataType const& Parameters() const { return weights; }
99  OutputDataType& Parameters() { return weights; }
100 
102  InputDataType const& InputParameter() const { return inputParameter; }
104  InputDataType& InputParameter() { return inputParameter; }
105 
107  OutputDataType const& OutputParameter() const { return outputParameter; }
109  OutputDataType& OutputParameter() { return outputParameter; }
110 
112  OutputDataType const& Delta() const { return delta; }
114  OutputDataType& Delta() { return delta; }
115 
117  size_t InputSize() const { return inSize; }
118 
120  size_t OutputSize() const { return outSize; }
121 
123  OutputDataType const& Gradient() const { return gradient; }
125  OutputDataType& Gradient() { return gradient; }
126 
128  OutputDataType const& Weight() const { return weight; }
130  OutputDataType& Weight() { return weight; }
131 
133  OutputDataType const& Bias() const { return bias; }
135  OutputDataType& Bias() { return bias; }
136 
140  template<typename Archive>
141  void serialize(Archive& ar, const unsigned int /* version */);
142 
143  private:
145  size_t inSize;
146 
148  size_t outSize;
149 
151  OutputDataType weights;
152 
154  OutputDataType weight;
155 
157  OutputDataType bias;
158 
160  OutputDataType delta;
161 
163  OutputDataType gradient;
164 
166  InputDataType inputParameter;
167 
169  OutputDataType outputParameter;
170 
172  RegularizerType regularizer;
173 }; // class Linear
174 
175 } // namespace ann
176 } // namespace mlpack
177 
178 // Include implementation.
179 #include "linear_impl.hpp"
180 
181 #endif
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType const & Delta() const
Get the delta.
Definition: linear.hpp:112
Linear algebra utility functions, generally performed on matrices or vectors.
OutputDataType & Parameters()
Modify the parameters.
Definition: linear.hpp:99
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:120
The core includes that mlpack expects; standard C++ includes and Armadillo.
OutputDataType & Bias()
Modify the bias weights of the layer.
Definition: linear.hpp:135
OutputDataType & Gradient()
Modify the gradient.
Definition: linear.hpp:125
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:128
OutputDataType const & Gradient() const
Get the gradient.
Definition: linear.hpp:123
OutputDataType const & Bias() const
Get the bias of the layer.
Definition: linear.hpp:133
OutputDataType const & Parameters() const
Get the parameters.
Definition: linear.hpp:97
InputDataType & InputParameter()
Modify the input parameter.
Definition: linear.hpp:104
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: linear.hpp:107
size_t InputSize() const
Get the input size.
Definition: linear.hpp:117
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: linear.hpp:109
OutputDataType & Weight()
Modify the weight of the layer.
Definition: linear.hpp:130
OutputDataType & Delta()
Modify the delta.
Definition: linear.hpp:114
Linear()
Create the Linear object.
InputDataType const & InputParameter() const
Get the input parameter.
Definition: linear.hpp:102