mlpack  3.4.2
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 
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
mlpack::ann::Linear::InputParameter
InputDataType const & InputParameter() const
Get the input parameter.
Definition: linear.hpp:114
mlpack::ann::Linear::Backward
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...
mlpack::ann::Linear::Delta
OutputDataType const & Delta() const
Get the delta.
Definition: linear.hpp:124
prereqs.hpp
The core includes that mlpack expects; standard C++ includes and Armadillo.
mlpack::ann::Linear::OutputSize
size_t OutputSize() const
Get the output size.
Definition: linear.hpp:132
mlpack::ann::Linear::Linear
Linear(Linear &&)
Move constructor.
mlpack::ann::Linear::Bias
OutputDataType & Bias()
Modify the bias weights of the layer.
Definition: linear.hpp:147
mlpack::ann::Linear::WeightSize
size_t WeightSize() const
Get the size of the weights.
Definition: linear.hpp:150
mlpack::ann::Linear::Parameters
OutputDataType & Parameters()
Modify the parameters.
Definition: linear.hpp:111
mlpack::ann::Linear::Linear
Linear(const size_t inSize, const size_t outSize, RegularizerType regularizer=RegularizerType())
Create the Linear layer object using the specified number of units.
mlpack::ann::Linear::operator=
Linear & operator=(const Linear &layer)
Copy assignment operator.
mlpack
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: add_to_cli11.hpp:21
mlpack::ann::Linear::Bias
OutputDataType const & Bias() const
Get the bias of the layer.
Definition: linear.hpp:145
mlpack::ann::Linear::Forward
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...
mlpack::ann::Linear::operator=
Linear & operator=(Linear &&layer)
Move assignment operator.
mlpack::ann::Linear::InputParameter
InputDataType & InputParameter()
Modify the input parameter.
Definition: linear.hpp:116
mlpack::ann::Linear::Gradient
OutputDataType const & Gradient() const
Get the gradient.
Definition: linear.hpp:135
mlpack::ann::Linear::Weight
OutputDataType const & Weight() const
Get the weight of the layer.
Definition: linear.hpp:140
mlpack::ann::Linear::Gradient
OutputDataType & Gradient()
Modify the gradient.
Definition: linear.hpp:137
no_regularizer.hpp
mlpack::ann::Linear::Reset
void Reset()
mlpack::ann::Linear::Weight
OutputDataType & Weight()
Modify the weight of the layer.
Definition: linear.hpp:142
mlpack::ann::Linear::OutputParameter
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: linear.hpp:119
mlpack::ann::Linear::Linear
Linear(const Linear &layer)
Copy constructor.
mlpack::ann::Linear::Parameters
OutputDataType const & Parameters() const
Get the parameters.
Definition: linear.hpp:109
mlpack::ann::Linear::InputSize
size_t InputSize() const
Get the input size.
Definition: linear.hpp:129
mlpack::ann::Linear::Linear
Linear()
Create the Linear object.
mlpack::ann::Linear::OutputParameter
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: linear.hpp:121
mlpack::ann::Linear::serialize
void serialize(Archive &ar, const unsigned int)
Serialize the layer.
layer_types.hpp
mlpack::ann::Linear::Delta
OutputDataType & Delta()
Modify the delta.
Definition: linear.hpp:126
mlpack::ann::Linear
Implementation of the Linear layer class.
Definition: linear.hpp:39
mlpack::ann::Linear::Gradient
void Gradient(const arma::Mat< eT > &input, const arma::Mat< eT > &error, arma::Mat< eT > &gradient)