mlpack  master
elu.hpp
Go to the documentation of this file.
1 
26 #ifndef MLPACK_METHODS_ANN_LAYER_ELU_HPP
27 #define MLPACK_METHODS_ANN_LAYER_ELU_HPP
28 
29 #include <mlpack/prereqs.hpp>
30 
31 namespace mlpack {
32 namespace ann {
33 
57 template <
58  typename InputDataType = arma::mat,
59  typename OutputDataType = arma::mat
60 >
61 class ELU
62 {
63  public:
71  ELU(const double alpha = 1.0);
72 
80  template<typename InputType, typename OutputType>
81  void Forward(const InputType&& input, OutputType&& output);
82 
92  template<typename DataType>
93  void Backward(const DataType&& input, DataType&& gy, DataType&& g);
94 
96  InputDataType const& InputParameter() const { return inputParameter; }
98  InputDataType& InputParameter() { return inputParameter; }
99 
101  OutputDataType const& OutputParameter() const { return outputParameter; }
103  OutputDataType& OutputParameter() { return outputParameter; }
104 
106  OutputDataType const& Delta() const { return delta; }
108  OutputDataType& Delta() { return delta; }
109 
111  double const& Alpha() const { return alpha; }
113  double& Alpha() { return alpha; }
114 
118  template<typename Archive>
119  void Serialize(Archive& ar, const unsigned int /* version */);
120 
121  private:
128  double fn(const double x)
129  {
130  if (x < DBL_MAX)
131  return (x > 0) ? x : alpha * (std::exp(x) - 1);
132  return 1.0;
133  }
134 
141  template<typename eT>
142  void fn(const arma::Mat<eT>& x, arma::Mat<eT>& y)
143  {
144  y = x;
145 
146  for (size_t i = 0; i < x.n_elem; i++)
147  {
148  y(i) = fn(x(i));
149  }
150  }
151 
158  double Deriv(const double y)
159  {
160  return (y > 0) ? 1 : (y + alpha);
161  }
162 
170  template<typename InputType, typename OutputType>
171  void Deriv(const InputType& x, OutputType& y)
172  {
173  y = x;
174 
175  for (size_t i = 0; i < x.n_elem; i++)
176  {
177  y(i) = Deriv(x(i));
178  }
179  }
180 
182  OutputDataType delta;
183 
185  InputDataType inputParameter;
186 
188  OutputDataType outputParameter;
189 
191  double alpha;
192 
193 }; // class ELU
194 
195 } // namespace ann
196 } // namespace mlpack
197 
198 // Include implementation.
199 #include "elu_impl.hpp"
200 
201 #endif
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: elu.hpp:101
double Deriv(const double y)
Computes the first derivative of the ELU function.
Definition: elu.hpp:158
double & Alpha()
Modify the non zero gradient.
Definition: elu.hpp:113
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
OutputDataType const & Delta() const
Get the delta.
Definition: elu.hpp:106
The core includes that mlpack expects; standard C++ includes and Armadillo.
void Deriv(const InputType &x, OutputType &y)
Computes the first derivative of the ELU function.
Definition: elu.hpp:171
void fn(const arma::Mat< eT > &x, arma::Mat< eT > &y)
Computes the ELU function using a dense matrix as input.
Definition: elu.hpp:142
double alpha
ELU Hyperparameter (0 < alpha)
Definition: elu.hpp:191
InputDataType & InputParameter()
Modify the input parameter.
Definition: elu.hpp:98
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: elu.hpp:103
InputDataType const & InputParameter() const
Get the input parameter.
Definition: elu.hpp:96
void Serialize(Archive &ar, const unsigned int)
Serialize the layer.
void Forward(const InputType &&input, OutputType &&output)
Ordinary feed forward pass of a neural network, evaluating the function f(x) by propagating the activ...
OutputDataType & Delta()
Modify the delta.
Definition: elu.hpp:108
OutputDataType outputParameter
Locally-stored output parameter object.
Definition: elu.hpp:188
double const & Alpha() const
Get the non zero gradient.
Definition: elu.hpp:111
InputDataType inputParameter
Locally-stored input parameter object.
Definition: elu.hpp:185
void Backward(const DataType &&input, DataType &&gy, DataType &&g)
Ordinary feed backward pass of a neural network, calculating the function f(x) by propagating x backw...
OutputDataType delta
Locally-stored delta object.
Definition: elu.hpp:182
The ELU activation function, defined by.
Definition: elu.hpp:61
ELU(const double alpha=1.0)
Create the ELU object using the specified parameters.
double fn(const double x)
Computes the ELU function.
Definition: elu.hpp:128