MachineIntelligenceCore:NeuralNets
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
BinaryCorrelator.hpp
Go to the documentation of this file.
1 
26 #ifndef SRC_MLNN_BINRARYCORRELATOR_HPP_
27 #define SRC_MLNN_BINRARYCORRELATOR_HPP_
28 
29 #include <mlnn/layer/Layer.hpp>
30 
31 namespace mic {
32 namespace mlnn {
33 namespace fully_connected {
34 
40 template <typename eT=float>
41 class BinaryCorrelator : public mic::mlnn::Layer<eT> {
42 public:
43 
50  BinaryCorrelator(size_t inputs_, size_t outputs_, eT permanence_threshold_ = 0.5, eT proximal_threshold_ = 0.5, std::string name_ = "BinaryCorrelator") :
51  BinaryCorrelator(inputs_, 1, 1, outputs_, 1, 1, permanence_threshold_, proximal_threshold_, name_)
52  {
53 
54  }
55 
66  BinaryCorrelator(size_t input_height_, size_t input_width_, size_t input_depth_,
67  size_t output_height_, size_t output_width_, size_t output_depth_,
68  eT permanence_threshold_ = 0.5, eT proximal_threshold_ = 0.5,
69  std::string name_ = "BinaryCorrelator") :
70  Layer<eT>::Layer(input_height_, input_width_, input_depth_,
71  output_height_, output_width_, output_depth_,
73  permanence_threshold(permanence_threshold_),
74  proximal_threshold(proximal_threshold_)
75  {
76  // Create the permanence matrix.
78  // Create "connectivity" matrix.
80 
81  // Initialize permanence matrix.
82  //double range = sqrt(6.0 / double(inputs_ + outputs_));
83  p['p']->rand(0, 1);
84 
85  // Initialize connectivity.
86  mic::types::MatrixPtr<eT> c = m['c'];
87  mic::types::MatrixPtr<eT> perm = p['p'];
88  // Threshold.
89  for (size_t i = 0; i < (size_t)c->size(); i++) {
90  (*c)[i] = ((*perm)[i] > permanence_threshold) ? 1.0f : 0.0f;
91  }//: for
92 
93  // Set hebbian learning as default optimization function.
94  Layer<eT>::template setOptimization<mic::neural_nets::learning::HebbianRule<eT> > ();
95  };
96 
97 
101  virtual ~BinaryCorrelator() {};
102 
107  void forward(bool test_ = false) {
108  // Get input matrices.
109  mic::types::Matrix<eT> x = (*s['x']);
110  mic::types::Matrix<eT> c = (*m['c']);
111  // Get output pointer - so the results will be stored!
112  mic::types::MatrixPtr<eT> y = s['y'];
113 
114  // Forward pass.
115  (*y) = c * x;
116  for (size_t i = 0; i < (size_t)y->size(); i++) {
117  // Threshold.
118  (*y)[i] = ((*y)[i] > proximal_threshold) ? 1.0f : 0.0f;
119  }//: for
120  }
121 
125  void backward() {
126  throw std::logic_error("Backward propagation should not be used with layers using Hebbian learning!");
127  }
128 
134  void update(eT alpha_, eT decay_ = 0.0f) {
135  //std::cout<<"p before update: " << (*p['p']) << std::endl;
136  // Update permanence using the learning rule.
137  opt["p"]->update(p['p'], s['x'], s['y'], alpha_);
138  //std::cout<<"p after update: " << (*p['p']) << std::endl;
139 
140  // Update connectivity matrix.
141  mic::types::MatrixPtr<eT> c = m['c'];
142  mic::types::MatrixPtr<eT> perm = p['p'];
143  //std::cout<<"C before threshold: " << (*c) << std::endl;
144  // Threshold.
145  for (size_t i = 0; i < (size_t)c->size(); i++) {
146  (*c)[i] = ((*perm)[i] > permanence_threshold) ? 1.0f : 0.0f;
147  }//: for
148  //std::cout<<"C after threshold: " << (*c) << std::endl;
149  }
150 
154  std::vector< std::shared_ptr <mic::types::Matrix<eT> > > & getActivations(size_t height_, size_t width_) {
155  // Check if memory for the activations was allocated.
156  if (neuron_activations.size() == 0) {
157  for (size_t i=0; i < outputSize(); i++) {
158  // Allocate memory for activation of every neuron.
159  mic::types::MatrixPtr<eT> row = MAKE_MATRIX_PTR(eT, inputSize(), 1);
160  neuron_activations.push_back(row);
161  }//: for
162  }//: if
163 
164  // Epsilon added for numerical stability.
165  eT eps = 1e-10;
166 
167  mic::types::MatrixPtr<eT> perm = p["p"];
168  // Iterate through "neurons" and generate "activation image" for each one.
169  for (size_t i=0; i < outputSize(); i++) {
170  // Get row.
171  mic::types::MatrixPtr<eT> row = neuron_activations[i];
172  // Copy data.
173  (*row) = perm->row(i);
174  // Resize row.
175  row->resize( height_, width_);
176  // Calculate l2 norm.
177  eT l2 = row->norm() + eps;
178  // Normalize the inputs to <-0.5,0.5> and add 0.5f -> range <0.0, 1.0>.
179  (*row) = row->unaryExpr ( [&] ( eT x ) { return ( x / l2 + 0.5f); } );
180  }//: for
181 
182  // Return activations.
183  return neuron_activations;
184  }
185 
186 
187  // Unhide the overloaded methods inherited from the template class Layer fields via "using" statement.
188  using Layer<eT>::forward;
189  using Layer<eT>::backward;
190 
191 protected:
192  // Unhide the fields inherited from the template class Layer via "using" statement.
193  using Layer<eT>::s;
194  using Layer<eT>::p;
195  using Layer<eT>::m;
196  using Layer<eT>::inputSize;
197  using Layer<eT>::outputSize;
198  using Layer<eT>::batch_size;
199  using Layer<eT>::opt;
200 
201 private:
202  // Friend class - required for using boost serialization.
203  template<typename tmp> friend class mic::mlnn::MultiLayerNeuralNetwork;
204  //template<typename tmp> friend class mic::mlnn::HebbianNeuralNetwork;
205 
207  std::vector< std::shared_ptr <mic::types::Matrix<eT> > > neuron_activations;
208 
209  // Permanence threshold - used for calculation of binary connectivity.
211 
212  // Proximal threshold - used for activation of a given dendrite segment.
214 
219 
220 };
221 
222 
223 } /* namespace fully_connected */
224 } /* namespace mlnn */
225 } /* namespace mic */
226 
227 #endif /* SRC_MLNN_BINRARYCORRELATOR_HPP_ */
size_t inputSize()
Returns size (length) of inputs.
Definition: Layer.hpp:255
BinaryCorrelator(size_t inputs_, size_t outputs_, eT permanence_threshold_=0.5, eT proximal_threshold_=0.5, std::string name_="BinaryCorrelator")
std::vector< std::shared_ptr< mic::types::Matrix< eT > > > & getActivations(size_t height_, size_t width_)
std::vector< std::shared_ptr< mic::types::Matrix< eT > > > neuron_activations
Vector containing activations of neurons.
size_t outputSize()
Returns size (length) of outputs.
Definition: Layer.hpp:260
BinaryCorrelator(size_t input_height_, size_t input_width_, size_t input_depth_, size_t output_height_, size_t output_width_, size_t output_depth_, eT permanence_threshold_=0.5, eT proximal_threshold_=0.5, std::string name_="BinaryCorrelator")
Class implementing a linear, fully connected layer.
Class representing a multi-layer neural network.
Definition: Layer.hpp:86
mic::neural_nets::optimization::OptimizationArray< eT > opt
Array of optimization functions.
Definition: Layer.hpp:765
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
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
mic::types::MatrixArray< eT > p
Parameters - parameters of the layer, to be used by the derived classes.
Definition: Layer.hpp:759