base_layer.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_ANN_LAYER_BASE_LAYER_HPP
14 #define MLPACK_METHODS_ANN_LAYER_BASE_LAYER_HPP
15 
16 #include <mlpack/prereqs.hpp>
23 
24 namespace mlpack {
25 namespace ann {
26 
44 template <
45  class ActivationFunction = LogisticFunction,
46  typename InputDataType = arma::mat,
47  typename OutputDataType = arma::mat
48 >
49 class BaseLayer
50 {
51  public:
56  {
57  // Nothing to do here.
58  }
59 
67  template<typename InputType, typename OutputType>
68  void Forward(const InputType&& input, OutputType&& output)
69  {
70  ActivationFunction::Fn(input, output);
71  }
72 
82  template<typename eT>
83  void Backward(const arma::Mat<eT>&& input,
84  arma::Mat<eT>&& gy,
85  arma::Mat<eT>&& g)
86  {
87  arma::Mat<eT> derivative;
88  ActivationFunction::Deriv(input, derivative);
89  g = gy % derivative;
90  }
91 
93  OutputDataType const& OutputParameter() const { return outputParameter; }
95  OutputDataType& OutputParameter() { return outputParameter; }
96 
98  OutputDataType const& Delta() const { return delta; }
100  OutputDataType& Delta() { return delta; }
101 
105  template<typename Archive>
106  void serialize(Archive& /* ar */, const unsigned int /* version */)
107  {
108  /* Nothing to do here */
109  }
110 
111  private:
113  OutputDataType delta;
114 
116  OutputDataType outputParameter;
117 }; // class BaseLayer
118 
119 // Convenience typedefs.
120 
124 template <
125  class ActivationFunction = LogisticFunction,
126  typename InputDataType = arma::mat,
127  typename OutputDataType = arma::mat
128 >
129 using SigmoidLayer = BaseLayer<
130  ActivationFunction, InputDataType, OutputDataType>;
131 
135 template <
136  class ActivationFunction = IdentityFunction,
137  typename InputDataType = arma::mat,
138  typename OutputDataType = arma::mat
139 >
140 using IdentityLayer = BaseLayer<
141  ActivationFunction, InputDataType, OutputDataType>;
142 
146 template <
147  class ActivationFunction = RectifierFunction,
148  typename InputDataType = arma::mat,
149  typename OutputDataType = arma::mat
150 >
151 using ReLULayer = BaseLayer<
152  ActivationFunction, InputDataType, OutputDataType>;
153 
157 template <
158  class ActivationFunction = TanhFunction,
159  typename InputDataType = arma::mat,
160  typename OutputDataType = arma::mat
161 >
162 using TanHLayer = BaseLayer<
163  ActivationFunction, InputDataType, OutputDataType>;
164 
168 template <
169  class ActivationFunction = SoftplusFunction,
170  typename InputDataType = arma::mat,
171  typename OutputDataType = arma::mat
172 >
173 using SoftPlusLayer = BaseLayer<
174  ActivationFunction, InputDataType, OutputDataType>;
175 
179 template <
180  class ActivationFunction = HardSigmoidFunction,
181  typename InputDataType = arma::mat,
182  typename OutputDataType = arma::mat
183 >
185  ActivationFunction, InputDataType, OutputDataType>;
186 
187 } // namespace ann
188 } // namespace mlpack
189 
190 #endif
The identity function, defined by.
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: base_layer.hpp:95
BaseLayer()
Create the BaseLayer object.
Definition: base_layer.hpp:55
OutputDataType & Delta()
Modify the delta.
Definition: base_layer.hpp:100
void serialize(Archive &, const unsigned int)
Serialize the layer.
Definition: base_layer.hpp:106
The tanh function, defined by.
.hpp
Definition: add_to_po.hpp:21
The core includes that mlpack expects; standard C++ includes and Armadillo.
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: base_layer.hpp:93
OutputDataType const & Delta() const
Get the delta.
Definition: base_layer.hpp:98
Implementation of the base layer.
Definition: base_layer.hpp:49
The logistic function, defined by.
void Forward(const InputType &&input, OutputType &&output)
Ordinary feed forward pass of a neural network, evaluating the function f(x) by propagating the activ...
Definition: base_layer.hpp:68
The softplus function, defined by.
The hard sigmoid function, defined by.
The rectifier function, defined by.
void Backward(const arma::Mat< eT > &&input, 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...
Definition: base_layer.hpp:83