MachineIntelligenceCore:NeuralNets
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
ELU.hpp
Go to the documentation of this file.
1 
23 #ifndef SRC_MLNN_ELU_HPP_
24 #define SRC_MLNN_ELU_HPP_
25 
26 #include <mlnn/layer/Layer.hpp>
27 
28 namespace mic {
29 namespace mlnn {
30 namespace activation_function {
31 
38 template <typename eT=float>
39 class ELU : public mic::mlnn::Layer<eT> {
40 public:
41 
47  ELU(size_t size_,std::string name_ = "ELU") :
48  ELU(size_, 1, 1, name_)
49  {
50 
51  }
52 
53 
61  ELU(size_t height_, size_t width_, size_t depth_, std::string name_ = "ELU") :
62  Layer<eT>::Layer(height_, width_, depth_,
63  height_, width_, depth_,
64  LayerTypes::ELU, name_)
65  {
66 
67  }
68 
72  virtual ~ELU() {};
73 
74  void forward(bool test = false) {
75  // Access the data of both matrices.
76  eT* x = s['x']->data();
77  eT* y = s['y']->data();
78 
79  // Iterate through elements.
80  size_t size = (size_t) s['x']->rows() * s['x']->cols();
81  for (size_t i = 0; i < size; i++) {
82  y[i] = x[i] > 0.0f ? x[i] : (expf(x[i]) - 1.0f);
83  }//: for
84  }
85 
86  void backward() {
87  // Access the data of matrices.
88  eT* gx = g['x']->data();
89  eT* gy = g['y']->data();
90  eT* y = s['y']->data();
91 
92  // Iterate through elements.
93  size_t size = (size_t) g['x']->rows() * g['x']->cols();
94  for (size_t i = 0; i < size; i++) {
95  // Calculate the ELU y derivative.
96  eT dy = y[i] > 0.0f ? 1.0f : exp(y[i]);
97  // Pass the gradient.
98  gx[i] = dy * gy[i];
99 
100  }//: for
101  }
102 
108  virtual void update(eT alpha_, eT decay_ = 0.0f) { };
109 
110 protected:
111  // Unhiding the template inherited fields via "using" statement.
112  using Layer<eT>::g;
113  using Layer<eT>::s;
114 
115 private:
116  // Friend class - required for using boost serialization.
117  template<typename tmp> friend class mic::mlnn::MultiLayerNeuralNetwork;
118 
122  ELU<eT>() : Layer<eT> () { }
123 
124 };
125 
126 } /* activation_function */
127 } /* namespace mlnn */
128 } /* namespace mic */
129 
130 #endif /* SRC_MLNN_ELU_HPP_ */
Class implementing the layer with Exponential Linear Unit (ELU). http://arxiv.org/pdf/1511.07289v5.pdf.
Definition: ELU.hpp:39
Class representing a multi-layer neural network.
Definition: Layer.hpp:86
ELU(size_t height_, size_t width_, size_t depth_, std::string name_="ELU")
Definition: ELU.hpp:61
LayerTypes
Enumeration of possible layer types.
Definition: Layer.hpp:58
mic::types::MatrixArray< eT > s
States - contains input [x] and output [y] matrices.
Definition: Layer.hpp:753
mic::types::MatrixArray< eT > g
Gradients - contains input [x] and output [y] matrices.
Definition: Layer.hpp:756
void forward(bool test=false)
Definition: ELU.hpp:74
virtual void update(eT alpha_, eT decay_=0.0f)
Definition: ELU.hpp:108
Contains a template class representing a layer.
ELU(size_t size_, std::string name_="ELU")
Definition: ELU.hpp:47