mlpack
leaky_relu_impl.hpp
Go to the documentation of this file.
1 
14 #ifndef MLPACK_METHODS_ANN_LAYER_LEAKYRELU_IMPL_HPP
15 #define MLPACK_METHODS_ANN_LAYER_LEAKYRELU_IMPL_HPP
16 
17 // In case it hasn't yet been included.
18 #include "leaky_relu.hpp"
19 
20 namespace mlpack {
21 namespace ann {
22 
23 template<typename InputDataType, typename OutputDataType>
25  const double alpha) : alpha(alpha)
26 {
27  // Nothing to do here.
28 }
29 
30 template<typename InputDataType, typename OutputDataType>
31 template<typename InputType, typename OutputType>
33  const InputType& input, OutputType& output)
34 {
35  output = arma::max(input, alpha * input);
36 }
37 
38 template<typename InputDataType, typename OutputDataType>
39 template<typename DataType>
41  const DataType& input, const DataType& gy, DataType& g)
42 {
43  DataType derivative;
44  derivative.set_size(arma::size(input));
45  for (size_t i = 0; i < input.n_elem; ++i)
46  derivative(i) = (input(i) >= 0) ? 1 : alpha;
47 
48  g = gy % derivative;
49 }
50 
51 template<typename InputDataType, typename OutputDataType>
52 template<typename Archive>
54  Archive& ar,
55  const uint32_t /* version */)
56 {
57  ar(CEREAL_NVP(alpha));
58 }
59 
60 } // namespace ann
61 } // namespace mlpack
62 
63 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
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: leaky_relu_impl.hpp:32
void serialize(Archive &ar, const uint32_t)
Serialize the layer.
Definition: leaky_relu_impl.hpp:53
LeakyReLU(const double alpha=0.03)
Create the LeakyReLU object using the specified parameters.
Definition: leaky_relu_impl.hpp:24
void Backward(const DataType &input, const DataType &gy, DataType &g)
Ordinary feed backward pass of a neural network, calculating the function f(x) by propagating x backw...
Definition: leaky_relu_impl.hpp:40