MachineIntelligenceCore:NeuralNets
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
Sigmoid.hpp
Go to the documentation of this file.
1 
23 #ifndef SRC_MLNN_SIGMOID_HPP_
24 #define SRC_MLNN_SIGMOID_HPP_
25 
26 #include <mlnn/layer/Layer.hpp>
27 
28 namespace mic {
29 namespace mlnn {
30 namespace activation_function {
31 
36 template <typename eT=float>
37 class Sigmoid : public mic::mlnn::Layer<eT> {
38 public:
44  Sigmoid(size_t size_,std::string name_ = "Sigmoid") :
45  Sigmoid(size_, 1, 1, name_)
46  {
47 
48  }
49 
50 
58  Sigmoid(size_t height_, size_t width_, size_t depth_,
59  std::string name_ = "Sigmoid") :
60  Layer<eT>::Layer(height_, width_, depth_,
61  height_, width_, depth_,
62  LayerTypes::Sigmoid, name_)
63  {
64 
65  }
66 
70  virtual ~Sigmoid() {};
71 
72  void forward(bool test = false) {
73  // Access the data of both matrices.
74  eT* x = s['x']->data();
75  eT* y = s['y']->data();
76 
77  for (size_t i = 0; i < (size_t)s['x']->rows() * s['x']->cols(); i++) {
78  y[i] = 1.0f / (1.0f +::exp(-x[i])); //: float -> expf
79  }//: for
80  }
81 
82  void backward() {
83  // Access the data of matrices.
84  eT* gx = g['x']->data();
85  eT* gy = g['y']->data();
86  eT* y = s['y']->data();
87 
88  for (size_t i = 0; i < (size_t)g['x']->rows() * g['x']->cols(); i++) {
89  // "Pass" the gradient multiplied by the sigmoid derivative.
90  gx[i] = gy[i]* (y[i] * (1.0 - y[i]));
91  }//: for
92  }
93 
99  virtual void update(eT alpha_, eT decay_ = 0.0f) { };
100 
101 protected:
102  // Unhiding the template inherited fields via "using" statement.
103  using Layer<eT>::g;
104  using Layer<eT>::s;
105 
106 private:
107  // Friend class - required for using boost serialization.
108  template<typename tmp> friend class mic::mlnn::MultiLayerNeuralNetwork;
109 
114 
115 
116 };
117 
118 } /* activation_function */
119 } /* namespace mlnn */
120 } /* namespace mic */
121 
122 #endif /* SRC_MLNN_SIGMOID_HPP_ */
Sigmoid(size_t height_, size_t width_, size_t depth_, std::string name_="Sigmoid")
Definition: Sigmoid.hpp:58
Class representing a multi-layer neural network.
Definition: Layer.hpp:86
LayerTypes
Enumeration of possible layer types.
Definition: Layer.hpp:58
virtual void update(eT alpha_, eT decay_=0.0f)
Definition: Sigmoid.hpp:99
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
Sigmoid(size_t size_, std::string name_="Sigmoid")
Definition: Sigmoid.hpp:44
Contains a template class representing a layer.