MachineIntelligenceCore:Algorithms
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
char_encoder_test.cpp
Go to the documentation of this file.
1 
23 #include <boost/thread/thread.hpp>
24 #include <boost/bind.hpp>
25 
26 
27 #include <types/Color.hpp>
29 
31 
32 #include <logger/Log.hpp>
33 #include <logger/ConsoleOutput.hpp>
34 using namespace mic::logger;
35 
36 #include <application/ApplicationState.hpp>
37 
38 
39 
40 
48 int main(int argc, char* argv[]) {
49  // Set console output to logger.
50  LOGGER->addOutput(new ConsoleOutput());
51  LOG(LINFO) << "Logger initialized. Starting application";
52 
53  // Manualy set sdr and batch size.
54  size_t sdr_size = 128;
55  size_t batch_size = 2;
56 
57  mic::encoders::CharMatrixXfEncoder encoder(sdr_size);
58 
59  // Load dataset.
61  // Manually set paths. DEPRICATED! Used here only for simplification of the test.
62  importer.setDataFilename("/Users/tkornut/Documents/workspace/machine-intelligence-core/data/txt/pl/ep-06-01-16-003.txt");
63  importer.setBatchSize(batch_size);
64 
65  if (!importer.importData())
66  return -1;
67 
68  LOG(LINFO)<<"There were " << importer.classes() << " distinctive classes imported";
69 
70  // Main application loop.
71  while (!APP_STATE->Quit()) {
72 
73  // If not paused.
74  if (!APP_STATE->isPaused()) {
75 
76  // If single step mode - pause after the step.
77  if (APP_STATE->isSingleStepModeOn())
78  APP_STATE->pressPause();
79 
80  // Random select sample.
81  mic::types::CharSample sample = importer.getRandomSample();
82 
83  // Encode the selected sample into SDR.
84  std::shared_ptr<mic::types::MatrixXf> sdr = encoder.encodeSample(sample.data());
85 
86  // Decode SDR.
87  std::shared_ptr<char> dec_char = encoder.decodeSample(sdr);
88 
89  // Display result.
90  LOG(LINFO)<<" Orig = '" << *(sample.data()) << "' decoded SDR = '" << (*dec_char) << "' label = '" << *(sample.label()) << "'";
91 
92  // Get next batch.
93  mic::types::CharBatch batch = importer.getNextBatch();
94  LOG(LINFO)<<" Batch: ";
95  for (size_t i=0; i < batch.size(); i++ ) {
96  LOG(LINFO)<<" ["<<i<< "] = '" << *(batch.data(i)) <<"'";
97  }//: for
98 
99 
100  // Encode the whole batch.
101  std::shared_ptr<mic::types::MatrixXf> batch_matrix = encoder.encodeBatch(batch.data());
102  LOG(LDEBUG)<<" Batched matrix: ";
103  LOG(LDEBUG) << (*batch_matrix);
104 
105  // Decode batch matrix.
106  std::vector<std::shared_ptr<char> > decoded_batch = encoder.decodeBatch(batch_matrix);
107 
108 
109  LOG(LINFO)<<" Decoded batch: ";
110  for (size_t i=0; i < decoded_batch.size(); i++ ) {
111  LOG(LINFO)<<" ["<<i<< "] = '" << *(decoded_batch[i]) <<"'";
112  }//: for
113 
114 
115  // Check if the batch was the last one.
116  if (importer.isLastBatch())
117  break;
118  }//: if ! paused
119 
120  // Sleep.
121  APP_SLEEP();
122  }//: while
123 
124 
125 
126  LOG(LINFO) << "Terminating application";
127 }//: main
std::vector< std::shared_ptr< DataType > > & data()
Returns sample data.
Definition: Batch.hpp:98
size_t size()
Definition: Batch.hpp:143
void setBatchSize(size_t batch_size_)
Definition: Batch.hpp:151
int main(int argc, char *argv[])
Main program function - tests character importer and encoder.
mic::types::Batch< DataType, LabelType > getNextBatch()
Definition: Batch.hpp:191
virtual std::shared_ptr< char > decodeSample(const mic::types::MatrixXfPtr &sdr_)
void setDataFilename(std::string data_filename_)
virtual std::vector< std::shared_ptr< inputDataType > > decodeBatch(const std::shared_ptr< mic::types::Matrix< outputDataType > > &sdrs_)
Template class storing the data-label pairs. Additionally it stores the the index of the sample (main...
Definition: Sample.hpp:38
bool isLastBatch()
Definition: Batch.hpp:261
std::shared_ptr< LabelType > label() const
Returns the returns the sample label.
Definition: Sample.hpp:85
size_t classes()
Definition: Batch.hpp:427
Encoder responsible for encoding character into Matrix SDRs. A 1-of-k encoder, i.e. it simply encodes char to ASCII and turns on that bit, i.e. there is no learning.
Importer responsible for importing/loading raw text files and returning characters one by one...
virtual mic::types::MatrixXfPtr encodeSample(const std::shared_ptr< char > &sample_)
Method responsible for encoding of input data sample into SDR (a vector of floats).
virtual std::shared_ptr< mic::types::Matrix< outputDataType > > encodeBatch(const std::vector< std::shared_ptr< inputDataType > > &batch_)
mic::types::Sample< DataType, LabelType > getRandomSample()
Definition: Batch.hpp:270
std::shared_ptr< DataType > data() const
Returns the sample data.
Definition: Sample.hpp:80