Advertisement
Guest User

Untitled

a guest
Aug 25th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. //Released under BSD-3 Cluse licsence
  2. //How to run: root -b stackedsp.cpp
  3. //or: c++ stackedsp.cpp -o stackedsp -O3 -lEtaler `root-config --cflags --ldflags --glibs` && ./stackedsp
  4. //Assuming you have Etaler installed in /use/local/lib. Change this if you have it in other places
  5. #pragma cling load("/usr/local/lib/libEtaler.so")
  6. #include <Etaler/Etaler.hpp>
  7. #include <Etaler/Algorithms/SpatialPooler.hpp>
  8. #include <Etaler/Encoders/GridCell1d.hpp>
  9. #include <Etaler/Encoders/Scalar.hpp>
  10. #include <Etaler/Backends/OpenCLBackend.hpp>
  11. using namespace et;
  12.  
  13. #include <random>
  14.  
  15. #include <xtensor/xarray.hpp>
  16.  
  17. constexpr intmax_t INPUT_SDR_SIZE = 2048;
  18.  
  19. #include <TGraph.h>
  20. #include <TCanvas.h>
  21. #include <TAxis.h>
  22. #include <TH2.h>
  23. #include <TStyle.h>
  24.  
  25. void run_experiment(int num_sps)
  26. {
  27. //Make a stack of N SpatialPoolers
  28. std::vector<SpatialPooler> sps;
  29. for(int i=0;i<num_sps;i++) {
  30. SpatialPooler sp(Shape({INPUT_SDR_SIZE}), Shape({INPUT_SDR_SIZE}));
  31. sp.setBoostingFactor(10);
  32. sp.setGlobalDensity(0.04);
  33. sps.push_back(sp);
  34. }
  35.  
  36. //Encode a scalar using a 1D GridCell encoder. A ScalarEncoder does the same job.
  37. auto encode = [](float v){return encoder::gridCell1d(v,INPUT_SDR_SIZE/16);};
  38. //Like an RBM, inference on each layer, train it and use it's outout as the
  39. //next layer's input
  40. auto train = [&](auto x){for(auto& sp : sps) {auto y = sp.compute(x); sp.learn(x, y); x = y;}};
  41.  
  42. //Train the SP with random inputs
  43. std::mt19937 rng;
  44. std::uniform_real_distribution<float> dist;
  45. for(size_t i=0;i<1000;i++) {
  46. Tensor x = encode(dist(rng));
  47. train(x);
  48. }
  49.  
  50. auto inference = [&](auto sdr) {Tensor x = sdr; for(auto& sp : sps) x = sp.compute(x); return x;};
  51.  
  52. //Generate a graph of the on bits in the SDR
  53. const int num_steps = 128;
  54. auto c1 = std::make_unique<TCanvas>("c1", "canvas", 3000, 700);
  55. gPad->SetLeftMargin(0.06);
  56. gPad->SetRightMargin(0.01);
  57. auto t = std::make_unique<TH2F>("h2", ("plot of SDR generated by SP"+std::to_string(num_sps)).c_str(), INPUT_SDR_SIZE, 0, INPUT_SDR_SIZE, num_steps, 0, 1);
  58.  
  59. auto bins = xt::linspace<float>(0.f, 1.f, num_steps);
  60. for(auto v : bins) {
  61. auto sdr = inference(encode(v)).toHost<uint8_t>();
  62. for(size_t i=0;i<sdr.size();i++)
  63. t->Fill(i, v, sdr[i]+0.0001);
  64. }
  65. gStyle->SetOptStat(0);
  66. t->GetXaxis()->SetTitle("SDR");
  67. t->GetYaxis()->SetTitle("value sent into SP stack");
  68. t->Draw("col");
  69. c1->SaveAs(("plot_" + std::to_string(num_sps) + ".png").c_str());
  70. c1->Close();
  71. }
  72.  
  73. void stackedsp_plot_sdr()
  74. {
  75. //C++17 type reduction is awesome
  76. //setDefaultBackend(std::make_shared<OpenCLBackend>());
  77.  
  78. std::vector num_sps = {0, 1, 2, 4, 8, 16};
  79. for(auto n : num_sps)
  80. run_experiment(n);
  81. }
  82.  
  83. int main()
  84. {
  85. stackedsp();
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement