mlpack  master
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>
17 
18 #include "layer_types.hpp"
19 
20 namespace mlpack {
21 namespace ann {
22 
32 template <
33  typename InputDataType = arma::mat,
34  typename OutputDataType = arma::mat
35 >
36 class Linear
37 {
38  public:
40  Linear();
41 
48  Linear(const size_t inSize, const size_t outSize);;
49 
50  /*
51  * Reset the layer parameter.
52  */
53  void Reset();
54 
62  template<typename eT>
63  void Forward(const arma::Mat<eT>&& input, arma::Mat<eT>&& output);
64 
74  template<typename eT>
75  void Backward(const arma::Mat<eT>&& /* input */,
76  arma::Mat<eT>&& gy,
77  arma::Mat<eT>&& g);
78 
79  /*
80  * Calculate the gradient using the output delta and the input activation.
81  *
82  * @param input The input parameter used for calculating the gradient.
83  * @param error The calculated error.
84  * @param gradient The calculated gradient.
85  */
86  template<typename eT>
87  void Gradient(const arma::Mat<eT>&& input,
88  arma::Mat<eT>&& error,
89  arma::Mat<eT>&& gradient);
90 
92  OutputDataType const& Parameters() const { return weights; }
94  OutputDataType& Parameters() { return weights; }
95 
97  InputDataType const& InputParameter() const { return inputParameter; }
99  InputDataType& InputParameter() { return inputParameter; }
100 
102  OutputDataType const& OutputParameter() const { return outputParameter; }
104  OutputDataType& OutputParameter() { return outputParameter; }
105 
107  OutputDataType const& Delta() const { return delta; }
109  OutputDataType& Delta() { return delta; }
110 
112  OutputDataType const& Gradient() const { return gradient; }
114  OutputDataType& Gradient() { return gradient; }
115 
119  template<typename Archive>
120  void Serialize(Archive& ar, const unsigned int /* version */);
121 
122  private:
124  size_t inSize;
125 
127  size_t outSize;
128 
130  OutputDataType weights;
131 
133  OutputDataType weight;
134 
136  OutputDataType bias;
137 
139  OutputDataType delta;
140 
142  OutputDataType gradient;
143 
145  InputDataType inputParameter;
146 
148  OutputDataType outputParameter;
149 }; // class Linear
150 
151 } // namespace ann
152 } // namespace mlpack
153 
154 // Include implementation.
155 #include "linear_impl.hpp"
156 
157 #endif
OutputDataType gradient
Locally-stored gradient object.
Definition: linear.hpp:142
void Serialize(Archive &ar, const unsigned int)
Serialize the layer.
InputDataType const & InputParameter() const
Get the input parameter.
Definition: linear.hpp:97
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: linear.hpp:104
The core includes that mlpack expects; standard C++ includes and Armadillo.
OutputDataType weights
Locally-stored weight object.
Definition: linear.hpp:130
OutputDataType const & Parameters() const
Get the parameters.
Definition: linear.hpp:92
OutputDataType outputParameter
Locally-stored output parameter object.
Definition: linear.hpp:148
OutputDataType & Gradient()
Modify the gradient.
Definition: linear.hpp:114
OutputDataType weight
Locally-stored weight paramters.
Definition: linear.hpp:133
InputDataType & InputParameter()
Modify the input parameter.
Definition: linear.hpp:99
OutputDataType const & Delta() const
Get the delta.
Definition: linear.hpp:107
OutputDataType & Delta()
Modify the delta.
Definition: linear.hpp:109
InputDataType inputParameter
Locally-stored input parameter object.
Definition: linear.hpp:145
OutputDataType bias
Locally-stored bias term parameters.
Definition: linear.hpp:136
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 const & Gradient() const
Get the gradient.
Definition: linear.hpp:112
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 & Parameters()
Modify the parameters.
Definition: linear.hpp:94
size_t inSize
Locally-stored number of input units.
Definition: linear.hpp:124
size_t outSize
Locally-stored number of output units.
Definition: linear.hpp:127
OutputDataType delta
Locally-stored delta object.
Definition: linear.hpp:139
Linear()
Create the Linear object.
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: linear.hpp:102