MachineIntelligenceCore:NeuralNets
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros
NormalizedHebbianRule.hpp
Go to the documentation of this file.
1 
25 #ifndef NORMALIZEDHEBBIANRULE_HPP_
26 #define NORMALIZEDHEBBIANRULE_HPP_
27 
29 
30 namespace mic {
31 namespace neural_nets {
32 namespace learning {
33 
43 template <typename eT=float>
45 public:
51  NormalizedHebbianRule(size_t rows_, size_t cols_) {
52  delta = MAKE_MATRIX_PTR(eT, cols_, rows_);
53  delta->zeros();
54 
55  }
56 
57  // Virtual destructor - empty.
58  virtual ~NormalizedHebbianRule() { }
59 
60 
68  virtual void update(mic::types::MatrixPtr<eT> p_, mic::types::MatrixPtr<eT> x_, mic::types::MatrixPtr<eT> y_, eT learning_rate_ = 0.001) {
69  assert(p_->rows() == y_->rows());
70  assert(p_->cols() == x_->rows());
71  assert(x_->cols() == y_->cols());
72 
73  // Calculate the update using hebbian "fire together, wire together".
74  mic::types::MatrixPtr<eT> delta = calculateUpdate(x_, y_, learning_rate_);
75 
76  std::cout<<"x: " << (*x_).transpose() << std::endl;
77  std::cout<<"y: " << (*y_).transpose() << std::endl;
78  std::cout<<"przed update: " << (*p_)[0] << std::endl;
79  // weight += delta;
80  (*p_) += (*delta);
81  std::cout<<"przed normalizacjÄ…: " << (*p_)[0] << std::endl;
82  // Normalize.
83  (*p_) /= p_->squaredNorm();
84  std::cout<<"po normalizacji: " << (*p_)[0] << std::endl;
85  }
86 
93  virtual mic::types::MatrixPtr<eT> calculateUpdate(mic::types::MatrixPtr<eT> x_, mic::types::MatrixPtr<eT> y_, eT learning_rate_) {
94  // delta + alpha * x * y.
95  (*delta) = learning_rate_ * (*y_) * ((*x_).transpose());
96 
97  // Return delta normalized by batch size.
98  (*delta) /= x_->cols();
99  return delta;
100  }
101 
102 
103 protected:
105  mic::types::MatrixPtr<eT> delta;
106 
107 };
108 
109 } //: namespace learning
110 } /* namespace neural_nets */
111 } /* namespace mic */
112 
113 #endif /* NORMALIZEDHEBBIANRULE_HPP_ */
virtual void update(mic::types::MatrixPtr< eT > p_, mic::types::MatrixPtr< eT > x_, mic::types::MatrixPtr< eT > y_, eT learning_rate_=0.001)
Updates according to classical Hebbian rule (wij += ni * x * y) with additional normalization.
Abstract class representing interface to optimization function.
mic::types::MatrixPtr< eT > delta
Calculated update.
virtual mic::types::MatrixPtr< eT > calculateUpdate(mic::types::MatrixPtr< eT > x_, mic::types::MatrixPtr< eT > y_, eT learning_rate_)