MachineIntelligenceCore:Algorithms
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
UIntMatrixEncoder.hpp
Go to the documentation of this file.
1 
23 #ifndef SRC_ENCODERS_UINTMATRIXENCODER_HPP_
24 #define SRC_ENCODERS_UINTMATRIXENCODER_HPP_
25 
27 
28 #include <logger/Log.hpp>
29 
30 #include <cstring> // for memset
31 #ifdef _WIN32
32 #include <string.h>
33 #endif
34 
35 namespace mic {
36 namespace encoders {
37 
44 template<typename T = float>
45 class UIntMatrixEncoder: public mic::encoders::MatrixSDREncoder<unsigned int, T> {
46 public:
51  UIntMatrixEncoder(size_t sdr_length_) : MatrixSDREncoder<unsigned int, T>(sdr_length_) {
52 // std::cout<<" Hello CharMatrixXfEncoder\n";
53  };
54 
57 
63  virtual mic::types::MatrixPtr<T> encodeSample(const std::shared_ptr<unsigned int>& sample_){
64  // Create returned SDR - a vector, i.e. a matrix with number of columns equal to 1.
66  // Set all zeros.
67  sdr->setZero();
68 
69  unsigned int index = (*sample_);
70 
71  if (index >= sdr_length)
72  LOG(LERROR) << "The SDR is too short for proper encoding of "<<index<<"!";
73  else
74  (*sdr)(index) = 1;
75  return sdr;
76  }
77 
83  virtual std::shared_ptr<unsigned int> decodeSample(const mic::types::MatrixPtr<T>& sdr_){
84  // SDR must be in fact a vector, with number of columns equal to 1.
85  assert(sdr_->cols() == 1);
86 
87  // Find index of max value.
88  typename mic::types::Matrix<T>::Index maxRow, maxCol;
89  float max_value = sdr_->maxCoeff(&maxRow, &maxCol);
90  LOG(LDEBUG) << "(maxRow, maxCol) = max_value [ (" << maxRow << "," << maxCol <<") = " << max_value << "]";
91 
92  // Convert to int.
93  unsigned int decoded = maxRow;
94 
95  return std::make_shared<unsigned int>(decoded);
96  }
97 
98 private:
100 };
101 
102 
103 } /* namespace encoders */
104 } /* namespace mic */
105 
106 #endif /* SRC_ENCODERS_UINTMATRIXENCODER_HPP_ */
Encoder responsible for encoding unsigned integers into Matrix SDRs. A 1-of-k encoder, i.e. it simply turns on the adequate, i.e. there is no learning.
Abstract parent class for all encoders using MatrixXf as SDR datatype.
~UIntMatrixEncoder()
Default destructor - empty.
virtual std::shared_ptr< unsigned int > decodeSample(const mic::types::MatrixPtr< T > &sdr_)
typename std::shared_ptr< mic::types::Matrix< T > > MatrixPtr
Typedef for a shared pointer to template-typed dynamic matrices.
Definition: Matrix.hpp:479
virtual mic::types::MatrixPtr< T > encodeSample(const std::shared_ptr< unsigned int > &sample_)
Method responsible for encoding of input data sample into SDR (a vector of floats).
Template-typed Matrix of dynamic size. Uses OpenBLAS if found by CMAKE - overloaded, specializations of * operator for types: float, double.
Definition: Matrix.hpp:64