Last active
November 5, 2019 15:18
-
-
Save marty1885/4a916622a762d3340760c2dc8e74a012 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <htm/algorithms/TemporalMemory.hpp> | |
#include <htm/encoders/ScalarEncoder.hpp> | |
using namespace htm; | |
float benchmarkTemporalMemory(const std::vector<UInt>& out_shape, const std::vector<SDR>& x, size_t num_epoch) | |
{ | |
TemporalMemory tm; | |
tm.initialize(out_shape, 16, 2, 0.21, 0.1, 1, 1024, 0.1, 0.1, 0, 42, 1, 1024, false); | |
// sp.permanences_ = sp.permanences_.cast(DType::Half); | |
SDR y = x[0]; | |
auto t0 = std::chrono::high_resolution_clock::now(); | |
for(size_t i=0;i<num_epoch;i++) { | |
for(const auto& d : x) | |
tm.compute(d, true); | |
} | |
auto t1 = std::chrono::high_resolution_clock::now(); | |
return std::chrono::duration_cast<std::chrono::duration<float>>(t1-t0).count()/num_epoch; | |
} | |
std::vector<SDR> generateRandomData(size_t input_length, size_t num_data) | |
{ | |
std::vector<SDR> res(num_data, SDR({(UInt)input_length})); | |
static std::mt19937 rng; | |
std::uniform_real_distribution<float> dist(0, 1); | |
ScalarEncoderParameters param; | |
param.maximum = 1.0; | |
param.activeBits = input_length*0.15; | |
param.size = input_length; | |
ScalarEncoder encoder(param); | |
for(size_t i=0;i<num_data;i++) { | |
encoder.encode(dist(rng), res[i]); | |
res[i].getDense(); // Precompute the dense representation | |
} | |
return res; | |
} | |
int main() | |
{ | |
//std::shared_ptr<Backend> backend = std::make_shared<OpenCLBackend>(); | |
//setDefaultBackend(backend); | |
std::cout << "Benchmarking SpatialPooler algorithm from HTM.core: \n\n"; | |
std::vector<SDR> input_data; | |
std::vector<size_t> input_size; | |
for(size_t i=64;i<=9000;i+=64) | |
input_size.push_back(i); | |
input_size.push_back(9000); | |
size_t num_data = 1000; | |
std::cout << "[" << std::flush; | |
for(auto input_len : input_size) { | |
auto input_data = generateRandomData(input_len, num_data); | |
float t = benchmarkTemporalMemory({(UInt)input_len}, input_data, 1); | |
//std::cout << input_len << " bits per SDR, " << t/num_data*1000 << "ms per forward" << std::endl; | |
std::cout << t/num_data*1000 << ", " << std::flush; | |
//std::cout << input_len << "," << t/num_data*1000 << std::endl; | |
} | |
std::cout << "]" << std::flush; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment