mlpack  master
leaky_relu.hpp
Go to the documentation of this file.
1 
14 #ifndef MLPACK_METHODS_ANN_LAYER_LEAKYRELU_HPP
15 #define MLPACK_METHODS_ANN_LAYER_LEAKYRELU_HPP
16 
17 #include <mlpack/prereqs.hpp>
18 
19 namespace mlpack {
20 namespace ann {
21 
40 template <
41  typename InputDataType = arma::mat,
42  typename OutputDataType = arma::mat
43 >
44 class LeakyReLU
45 {
46  public:
54  LeakyReLU(const double alpha = 0.03);
55 
63  template<typename InputType, typename OutputType>
64  void Forward(const InputType&& input, OutputType&& output);
65 
75  template<typename DataType>
76  void Backward(const DataType&& input, DataType&& gy, DataType&& g);
77 
79  InputDataType const& InputParameter() const { return inputParameter; }
81  InputDataType& InputParameter() { return inputParameter; }
82 
84  OutputDataType const& OutputParameter() const { return outputParameter; }
86  OutputDataType& OutputParameter() { return outputParameter; }
87 
89  OutputDataType const& Delta() const { return delta; }
91  OutputDataType& Delta() { return delta; }
92 
94  double const& Alpha() const { return alpha; }
96  double& Alpha() { return alpha; }
97 
101  template<typename Archive>
102  void Serialize(Archive& ar, const unsigned int /* version */);
103 
104  private:
111  double Fn(const double x)
112  {
113  return std::max(x, alpha * x);
114  }
115 
122  template<typename eT>
123  void Fn(const arma::Mat<eT>& x, arma::Mat<eT>& y)
124  {
125  y = arma::max(x, alpha * x);
126  }
127 
134  double Deriv(const double x)
135  {
136  return (x >= 0) ? 1 : alpha;
137  }
138 
146  template<typename InputType, typename OutputType>
147  void Deriv(const InputType& x, OutputType& y)
148  {
149  y = x;
150 
151  for (size_t i = 0; i < x.n_elem; i++)
152  {
153  y(i) = Deriv(x(i));
154  }
155  }
156 
158  OutputDataType delta;
159 
161  InputDataType inputParameter;
162 
164  OutputDataType outputParameter;
165 
167  double alpha;
168 
169 }; // class LeakyReLU
170 
171 } // namespace ann
172 } // namespace mlpack
173 
174 // Include implementation.
175 #include "leaky_relu_impl.hpp"
176 
177 #endif
void Forward(const InputType &&input, OutputType &&output)
Ordinary feed forward pass of a neural network, evaluating the function f(x) by propagating the activ...
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 outputParameter
Locally-stored output parameter object.
Definition: leaky_relu.hpp:164
void Serialize(Archive &ar, const unsigned int)
Serialize the layer.
OutputDataType & OutputParameter()
Modify the output parameter.
Definition: leaky_relu.hpp:86
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
double const & Alpha() const
Get the non zero gradient.
Definition: leaky_relu.hpp:94
The core includes that mlpack expects; standard C++ includes and Armadillo.
The LeakyReLU activation function, defined by.
Definition: leaky_relu.hpp:44
InputDataType & InputParameter()
Modify the input parameter.
Definition: leaky_relu.hpp:81
void Fn(const arma::Mat< eT > &x, arma::Mat< eT > &y)
Computes the Leaky ReLU function using a dense matrix as input.
Definition: leaky_relu.hpp:123
OutputDataType & Delta()
Modify the delta.
Definition: leaky_relu.hpp:91
OutputDataType const & OutputParameter() const
Get the output parameter.
Definition: leaky_relu.hpp:84
LeakyReLU(const double alpha=0.03)
Create the LeakyReLU object using the specified parameters.
OutputDataType delta
Locally-stored delta object.
Definition: leaky_relu.hpp:158
OutputDataType const & Delta() const
Get the delta.
Definition: leaky_relu.hpp:89
double & Alpha()
Modify the non zero gradient.
Definition: leaky_relu.hpp:96
double alpha
Leakyness Parameter in the range 0 <alpha< 1.
Definition: leaky_relu.hpp:167
double Fn(const double x)
Computes the LeakReLU function.
Definition: leaky_relu.hpp:111
InputDataType inputParameter
Locally-stored input parameter object.
Definition: leaky_relu.hpp:161
InputDataType const & InputParameter() const
Get the input parameter.
Definition: leaky_relu.hpp:79
void Deriv(const InputType &x, OutputType &y)
Computes the first derivative of the LeakyReLU function.
Definition: leaky_relu.hpp:147
double Deriv(const double x)
Computes the first derivative of the LeakyReLU function.
Definition: leaky_relu.hpp:134