MachineIntelligenceCore:NeuralNets
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
Softmax.hpp
Go to the documentation of this file.
1 
23 #ifndef SRC_MLNN_SOFTMAX_HPP_
24 #define SRC_MLNN_SOFTMAX_HPP_
25 
26 #include <mlnn/layer/Layer.hpp>
27 
28 namespace mic {
29 namespace mlnn {
30 namespace cost_function {
31 
37 template <typename eT=float>
38 class Softmax : public mic::mlnn::Layer<eT> {
39 public:
40 
46  Softmax(size_t size_, std::string name_ = "Softmax") :
47  Softmax(size_, 1, 1, name_)
48  {
49 
50  }
51 
52 
60  Softmax(size_t height_, size_t width_, size_t depth_,
61  std::string name_ = "Softmax") :
62  Layer<eT>::Layer(height_, width_, depth_,
63  height_, width_, depth_,
64  LayerTypes::Softmax, name_)
65  {
66  // Add "temporary" parameters.
67  m.add("e", Layer<eT>::inputSize(), 1);
68  m.add("sum", 1, 1);
69  m.add("max", 1, 1);
70  }
71 
72 
76  virtual ~Softmax() {};
77 
82  virtual void resizeBatch(size_t batch_size_) {
83  // Call parent resize.
84  Layer<eT>::resizeBatch(batch_size_);
85 
86  // Reshape the temporary matrices.
87  m["e"]->resize(m["e"]->rows(), batch_size_);
88  m["sum"]->resize(m["sum"]->rows(), batch_size_);
89  m["max"]->resize(m["max"]->rows(), batch_size_);
90  }
91 
92 
93 
94  void forward(bool test_ = false) {
95  mic::types::MatrixPtr<eT> x = s["x"];
96  mic::types::MatrixPtr<eT> y = s["y"];
97  mic::types::MatrixPtr<eT> e = m["e"];
98  mic::types::MatrixPtr<eT> max = m["max"];
99  mic::types::MatrixPtr<eT> sum = m["sum"];
100 
101  //std::cout << "Softmax forward: s['x'] = \n" << (*s['x']) << std::endl;
102 
103  // Prevent overflow according to: http://eric-yuan.me/softmax/
104  (*max) = x->colwise().maxCoeff();
105 
106  // Calculate the e matrix - with overflow prevention.
107  for (size_t i = 0; i < (size_t)y->rows(); i++)
108  for (size_t j = 0; j < (size_t)y->cols(); j++)
109  (*e)(i, j) = std::exp( (*x)(i, j) - (*max)(j) );
110 
111  // Sum the values in columns (single batch), one by one.
112  (*sum) = e->colwise().sum();
113 
114  // Iterate through elements.
115  for (size_t i = 0; i < (size_t)y->rows(); i++) {
116  for (size_t j = 0; j < (size_t)y->cols(); j++) {
117  (*y)(i, j) = (*e)(i, j) / (*sum)(j);
118  }//: for
119  }//: for
120 
121 // std::cout << "Softmax forward: s['y'] = \n" << (*s['y']) << std::endl;
122  }
123 
124  void backward() {
125  mic::types::MatrixPtr<eT> y = s["y"];
126  mic::types::MatrixPtr<eT> dx = g["x"];
127  mic::types::MatrixPtr<eT> dy = g["y"];
128 
129  // Pass the gradient.
130  for (size_t i = 0; i < (size_t)y->size(); i++)
131  // dx = dy * derivative of softmax, i.e. y * (1 - y);
132  (*dx)[i] = (*dy)[i] * (*y)[i] * (1 - (*y)[i]);
133 
134  /*std::cout << "Softmax backward: g['y'] = \n" << (*g['y']) << std::endl;
135  std::cout << "Softmax backward: g['x'] = \n" << (*g['x']) << std::endl;*/
136  }
137 
143  virtual void update(eT alpha_, eT decay_ = 0.0f) { };
144 
145  // Unhide the overloaded methods inherited from the template class Layer fields via "using" statement.
146  using Layer<eT>::forward;
147  using Layer<eT>::backward;
148 
149 protected:
150  // Unhiding the template inherited fields via "using" statement.
151  using Layer<eT>::g;
152  using Layer<eT>::s;
153  using Layer<eT>::m;
154 
155 
156 private:
157  // Friend class - required for using boost serialization.
158  template<typename tmp> friend class mic::mlnn::MultiLayerNeuralNetwork;
159 
164 
165 
166 };
167 
168 } /* namespace cost_function */
169 } /* namespace mlnn */
170 } /* namespace mic */
171 
172 #endif /* SRC_MLNN_SOFTMAX_HPP_ */
Softmax(size_t height_, size_t width_, size_t depth_, std::string name_="Softmax")
Definition: Softmax.hpp:60
virtual void resizeBatch(size_t batch_size_)
Definition: Softmax.hpp:82
virtual void resizeBatch(size_t batch_size_)
Definition: Layer.hpp:199
Softmax activation function.
Definition: Softmax.hpp:38
Class representing a multi-layer neural network.
Definition: Layer.hpp:86
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: Softmax.hpp:94
Softmax(size_t size_, std::string name_="Softmax")
Definition: Softmax.hpp:46
virtual void update(eT alpha_, eT decay_=0.0f)
Definition: Softmax.hpp:143
Contains a template class representing a layer.
mic::types::MatrixArray< eT > m
Memory - a list of temporal parameters, to be used by the derived classes.
Definition: Layer.hpp:762