forked from numenta/nupic.core-legacy
-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Database region #860
Merged
Merged
Database region #860
Changes from 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5a3a901
Napi hello with file output
Zbysekz 1270127
Added regions
Zbysekz 9bd6ddd
Merge remote-tracking branch 'htm-community/sqlite3_external2' into D…
Zbysekz cab2f98
Database region implementing
Zbysekz a50e4e1
Working example
Zbysekz 13ab34f
Added unit test; speedup by using transaction
Zbysekz 0547814
napi hello original restored
Zbysekz 0b31653
Napi hello with file output
Zbysekz 42a9acc
Update README.md
Zbysekz fd8d335
Doc details
Zbysekz 0aea191
Merge remote-tracking branch 'origin/DatabaseRegion' into DatabaseRegion
Zbysekz 85ba939
Compiling napi_hello_database example executable
Zbysekz 7c7c092
DatabaseOut renamed to Database
Zbysekz 0992612
Datatype in for loop corrected
Zbysekz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
/* --------------------------------------------------------------------- | ||
* HTM Community Edition of NuPIC | ||
* Copyright (C) 2013-2015, Numenta, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | ||
* See the GNU Affero Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero Public License | ||
* along with this program. If not, see http://www.gnu.org/licenses. | ||
* --------------------------------------------------------------------- */ | ||
|
||
#include <htm/engine/Network.hpp> | ||
#include <htm/utils/Random.hpp> | ||
#include <htm/utils/Log.hpp> | ||
#include <htm/ntypes/Value.hpp> | ||
#include <htm/utils/MovingAverage.hpp> | ||
#include <htm/algorithms/AnomalyLikelihood.hpp> | ||
#include <fstream> | ||
|
||
using namespace htm; | ||
|
||
static bool verbose = true; | ||
#define VERBOSE if (verbose) std::cout << " " | ||
|
||
|
||
//this runs as an executable | ||
|
||
int main(int argc, char* argv[]) { | ||
htm::UInt EPOCHS = 5000; // number of iterations (calls to encoder/SP/TP compute() ) | ||
#ifndef NDEBUG | ||
EPOCHS = 2; // make test faster in Debug | ||
#endif | ||
|
||
const UInt DIM_INPUT = 1000; // Width of encoder output | ||
const UInt COLS = 2048; // number of columns in SP, TP | ||
const UInt CELLS = 8; // cells per column in TP | ||
Random rnd(42); // uses fixed seed for deterministic output | ||
std::ofstream ofs; | ||
|
||
std::string encoder_params = "{size: " + std::to_string(DIM_INPUT) + ", sparsity: 0.2, radius: 0.03, seed: 2019, noise: 0.01}"; | ||
std::string sp_global_params = "{columnCount: " + std::to_string(COLS) + ", globalInhibition: true}"; | ||
std::string tm_params = "{cellsPerColumn: " + std::to_string(CELLS) + ", orColumnOutputs: true}"; | ||
|
||
|
||
std::string output_file = "NapiOutputDir/Output.db"; | ||
|
||
|
||
// make a place to put output data. | ||
if (!Directory::exists("NapiOutputDir")) Directory::create("NapiOutputDir", false, true); | ||
if (Path::exists(output_file)) Path::remove(output_file); | ||
|
||
// Runtime arguments: napi_sine [epochs [filename]] | ||
if(argc >= 2) { | ||
EPOCHS = std::stoi(argv[1]); // number of iterations (default 5000) | ||
} | ||
if (argc >= 3) { | ||
ofs.open(argv[2], std::ios::out); // output filename (for plotting) | ||
} | ||
|
||
try { | ||
|
||
std::cout << "initializing. DIM_INPUT=" << DIM_INPUT << ", COLS=" << COLS << ", CELLS=" << CELLS << "\n"; | ||
|
||
Network net; | ||
|
||
// Declare the regions to use | ||
std::shared_ptr<Region> encoder = net.addRegion("encoder", "RDSEEncoderRegion", encoder_params); | ||
std::shared_ptr<Region> sp_global = net.addRegion("sp_global", "SPRegion", sp_global_params); | ||
std::shared_ptr<Region> tm = net.addRegion("tm", "TMRegion", tm_params); | ||
std::shared_ptr<Region> output = net.addRegion("output", "DatabaseOutRegion", "{outputFile: '"+ output_file + "'}"); | ||
|
||
// Setup data flows between regions | ||
net.link("encoder", "sp_global", "", "", "encoded", "bottomUpIn"); | ||
net.link("sp_global", "tm", "", "", "bottomUpOut", "bottomUpIn"); | ||
net.link("tm", "output", "", "", "anomaly", "dataIn0"); | ||
net.link("encoder", "output", "", "", "bucket", "dataIn1"); | ||
|
||
|
||
net.initialize(); | ||
|
||
|
||
/////////////////////////////////////////////////////////////// | ||
// | ||
// .------------------. | ||
// | encoder | | ||
// data--->|(RDSEEncoderRegion)| | ||
// | | | ||
// `-------------------' | ||
// | | ||
// .-----------------. | ||
// | sp_global | | ||
// | (SPRegion) | | ||
// | | | ||
// `-----------------' | ||
// | | ||
// .-----------------. | ||
// | tm | | ||
// | (TMRegion) |--->SQLite3 | ||
// | | | ||
// `-----------------' | ||
// | ||
////////////////////////////////////////////////////////////////// | ||
|
||
|
||
// enable this to see a trace as it executes | ||
//net.setLogLevel(LogLevel::LogLevel_Verbose); | ||
|
||
std::cout << "Running: " << EPOCHS << " Iterations.\n "; | ||
|
||
float anLikely = 0.0f; | ||
MovingAverage avgAnomaly(1000); | ||
AnomalyLikelihood anLikelihood; | ||
|
||
// RUN | ||
float x = 0.00f; | ||
for (size_t e = 0; e < EPOCHS; e++) { | ||
// genarate some data to send to the encoder | ||
|
||
// -- A sine wave, one degree rotation per iteration (an alternate function) | ||
//double data = std::sin(i * (3.1415 / 180)); | ||
|
||
// -- sine wave, 0.01 radians per iteration (Note: first iteration is for x=0.01, not 0) | ||
x += 0.01f; // step size for fn(x) | ||
double data = std::sin(x); | ||
encoder->setParameterReal64("sensedValue", data); // feed data into RDSE encoder for this iteration. | ||
|
||
// Execute an iteration. | ||
net.run(1); | ||
|
||
float an = ((float *)tm->getOutputData("anomaly").getBuffer())[0]; | ||
avgAnomaly.compute(an); | ||
anLikely = anLikelihood.anomalyProbability(an); | ||
|
||
|
||
// Save the data for plotting. <iteration>, <sin data>, <anomaly>, <likelyhood>\n | ||
if (ofs.is_open()) { | ||
ofs << e << "," << data << "," << an << "," << anLikely << std::endl; | ||
} | ||
|
||
if (e == EPOCHS - 1) | ||
{ | ||
|
||
// output values | ||
VERBOSE << "Result after " << e + 1 << " iterations.\n"; | ||
VERBOSE << " Anomaly = " << an << std::endl; | ||
VERBOSE << " Anomaly(avg) = " << avgAnomaly.getCurrentAvg() << std::endl; | ||
VERBOSE << " Anomaly(Likelihood) = " << anLikely << endl; | ||
VERBOSE << " Encoder out = " << encoder->getOutputData("encoded").getSDR(); | ||
VERBOSE << " SP (global) = " << sp_global->getOutputData("bottomUpOut").getSDR(); | ||
VERBOSE << " TM predictive = " << tm->getOutputData("predictiveCells").getSDR(); | ||
} | ||
} | ||
if (ofs.is_open()) | ||
ofs.close(); | ||
|
||
// close output file | ||
output->executeCommand({ "closeFile" }); | ||
|
||
std::cout << "finished\n"; | ||
|
||
|
||
} catch (Exception &ex) { | ||
std::cerr << ex.what(); | ||
if (ofs.is_open()) | ||
ofs.close(); | ||
return 1; | ||
} | ||
|
||
|
||
|
||
return 0; | ||
} | ||
|
This file contains 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: "UniformLink", or anything in this argument, is ignored. This is a hold-over from the NuPIC code.