forked from numenta/nupic.core-legacy
-
Notifications
You must be signed in to change notification settings - Fork 82
Example implemented using NetworkAPI #760
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a6c24f5
hotgym implemented using NetworkAPI
dkeeney 8aa2d46
Merge branch 'master' into hotgym_napi
dkeeney e60a84c
Clean up and change name to napi_sine
dkeeney ce55d3a
commented out SP (local)
dkeeney ca035fb
documentation changes
dkeeney 76e0abb
napi_sine now is the same as HelloSPTP
dkeeney ea5245b
got rid if the unused variable
dkeeney d296444
fixed unit tests (default parameters where changed).
dkeeney 83cf73a
GlobalInibition should be the default for SP
dkeeney df86af2
corrected .py tests resulting from change in defaults
dkeeney b162f83
requested fixes
dkeeney 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 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| # C++ example using Network API | ||
| The program `sine_napi` is an example of an app using the Network API tools | ||
| available in the htm.core library. In this example we generate a sine wave | ||
| with some noise as the input. This is passed to an encoder to turn that | ||
| into SDR format. This is passed to two instances of SpatialPooler (SP), | ||
| one is configured for local inhibition and one for global inhibition. | ||
|
|
||
| The output of the SP for global inhibition is passed on to the temporalMemory (TM) | ||
| algorithm. The output of the TM can be written to a file so that it can be plotted. | ||
|
|
||
| ``` | ||
| /////////////////////////////////////////////////////////////// | ||
| // | ||
| // .------------------. | ||
| // | encoder | | ||
| // data--->| (RDSERegion) | | ||
| // | | | ||
| // `------------------' | ||
| // | | | ||
| // .------------------. .------------------. | ||
| // | SP (local) | | SP (global) | | ||
| // | (SPRegion) | | (SPRegion) | | ||
| // | | | | | ||
| // `------------------' `------------------' | ||
| // | | ||
| // .------------------. | ||
| // | TM | | ||
| // | (TMRegion) | | ||
| // | | | ||
| // `------------------' | ||
| // | ||
| ////////////////////////////////////////////////////////////////// | ||
| ``` | ||
|
|
||
| Each "region" is a wrapper around an algorithm. This wrapper provides a uniform interface that can be plugged into the Network API engine for execution. The htm.core library contains regions for each of the primary algorithms in the library. The user can create their own algorithms and corresponding regions and plug them into the Network API engine by registering them with the Network class. The following chart shows the 'built-in' C++ regions. | ||
| <table> | ||
| <thead> | ||
| <tr> | ||
| <th>Built-in Region</th> | ||
| <th>Algorithm</th> | ||
| </tr> | ||
| </thead> | ||
| <tbody> | ||
| <tr> | ||
| <td>ScalarSensor</td> | ||
| <td>ScalarEncoder; Original encoder for numeric values</td> | ||
| </tr> | ||
| <tr> | ||
| <td>RDSERegion</td> | ||
| <td>RandomDistributedScalarEncoder (RDSE); advanced encoder for numeric values.</td> | ||
| </tr> | ||
| <tr> | ||
| <td>SPRegion</td> | ||
| <td>SpatialPooler (SP)</td> | ||
| </tr> | ||
| <tr> | ||
| <td>TMRegion</td> | ||
| <td>TemporalMemory (TM)</td> | ||
| </tr> | ||
| <tr> | ||
| <td>VectorFileSensor</td> | ||
| <td>for reading from a file</td> | ||
| </tr> | ||
| <tr> | ||
| <td>VectorFileEffector</td> | ||
| <td>for writing to a file</td> | ||
| </tr> | ||
| </tbody> | ||
| </table> | ||
|
|
||
| ## Usage | ||
|
|
||
| ``` | ||
| sine_napi [iterations [filename]] | ||
| ``` | ||
| - *iterations* is the number of times to execute the regions configured into the network. The default is 5000. | ||
| - *filename* is the path for a file to be written which contains the following for each iteration. The default is no file written. | ||
| ``` | ||
| <iteration>, <sin data>, <anomaly>\n | ||
| ``` | ||
|
|
||
|
|
||
| ## Experimentation | ||
| It is intended that this program be used as a launching point for experimenting with combinations of the regions and using different parameters. Try it and see what happens... | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| /* --------------------------------------------------------------------- | ||
| * 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 <fstream> | ||
|
|
||
| using namespace htm; | ||
|
|
||
| static bool verbose = false; | ||
| #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() ) | ||
| 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) + ", activeBits: 4, radius: 0.5, seed: 2019 }"; | ||
| // std::string sp_local_params = "{columnCount: " + std::to_string(COLS) + "}"; | ||
| std::string sp_global_params = "{columnCount: " + std::to_string(COLS) + ", globalInhibition: true}"; | ||
| std::string tm_params = "{activationThreshold: 9, cellsPerColumn: " + std::to_string(CELLS) + "}"; | ||
|
|
||
| // Runtime arguments: napi_sine [epochs [filename]] | ||
| if(argc >= 2) { | ||
| EPOCHS = std::stoi(argv[1]); // number of iterations (default 500) | ||
|
breznak marked this conversation as resolved.
Outdated
|
||
| } | ||
| if (argc >= 3) { | ||
| ofs.open(argv[2], std::ios::out); // output filename (for plotting) | ||
| } | ||
|
|
||
| try { | ||
|
|
||
| std::cout << "initializing\n"; | ||
|
|
||
| Network net; | ||
|
|
||
| // Declare the regions to use | ||
| std::shared_ptr<Region> encoder = net.addRegion("encoder", "RDSERegion", encoder_params); | ||
| // std::shared_ptr<Region> sp_local = net.addRegion("sp_local", "SPRegion", sp_local_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); | ||
|
|
||
| // Setup data flows between regions | ||
| // net.link("encoder", "sp_local", "", "", "encoded", "bottomUpIn"); | ||
| net.link("encoder", "sp_global", "", "", "encoded", "bottomUpIn"); | ||
| net.link("sp_global", "tm", "", "", "bottomUpOut", "bottomUpIn"); | ||
|
|
||
| net.initialize(); | ||
|
|
||
| /////////////////////////////////////////////////////////////// | ||
| // | ||
| // .------------------. | ||
| // | encoder | | ||
| // data--->| (RDSERegion) | | ||
| // | | | ||
| // `------------------' | ||
| // | | | ||
| // .------------------. .------------------. | ||
| // | sp_local | | sp_global | | ||
| // | (SPRegion) | | (SPRegion) | | ||
| // | | | | | ||
| // `------------------' `------------------' | ||
| // | | ||
| // .------------------. | ||
| // | tm | | ||
| // | (TMRegion) | | ||
| // | | | ||
| // `------------------' | ||
| // | ||
| ////////////////////////////////////////////////////////////////// | ||
|
|
||
|
|
||
| // enable this to see a trace as it executes | ||
| //net.setLogLevel(LogLevel::LogLevel_Verbose); | ||
|
|
||
| std::cout << "Running: \n"; | ||
| // RUN | ||
| for (size_t i = 0; i < EPOCHS; i++) { | ||
| // genarate some data to send to the encoder | ||
| // -- A sin wave, one degree rotation per iteration, 1% noise added | ||
| double data = std::sin(i * (3.1415 / 180)) + (double)rnd.realRange(-0.01f, +0.1f); | ||
| encoder->setParameterReal64("sensedValue", data); // feed data into RDSE encoder for this iteration. | ||
|
|
||
| // Execute an iteration. | ||
| net.run(1); | ||
|
|
||
| // output values | ||
| float an = ((float *)tm->getOutputData("anomaly").getBuffer())[0]; | ||
| VERBOSE << "Epoch = " << i << std::endl; | ||
| VERBOSE << " Data = " << data << std::endl; | ||
| VERBOSE << " Encoder out = " << encoder->getOutputData("encoded").getSDR(); | ||
| // VERBOSE << " SP (local) = " << sp_local->getOutputData("bottomUpOut").getSDR(); | ||
| VERBOSE << " SP (global) = " << sp_global->getOutputData("bottomUpOut").getSDR(); | ||
| VERBOSE << " TM output = " << tm->getOutputData("bottomUpOut").getSDR(); | ||
| VERBOSE << " ActiveCells = " << tm->getOutputData("activeCells").getSDR(); | ||
| VERBOSE << " winners = " << tm->getOutputData("predictedActiveCells").getSDR(); | ||
| VERBOSE << " Anomaly = " << an << std::endl; | ||
|
|
||
| // Save the data for plotting. <iteration>, <sin data>, <anomaly>\n | ||
| if (ofs.is_open()) | ||
| ofs << i << "," << data << "," << an << std::endl; | ||
| } | ||
| if (ofs.is_open()) | ||
| ofs.close(); | ||
| std::cout << "finished\n"; | ||
|
|
||
|
|
||
| } catch (Exception &e) { | ||
| std::cerr << e.what(); | ||
| if (ofs.is_open()) | ||
| ofs.close(); | ||
| return 1; | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
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
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.
Uh oh!
There was an error while loading. Please reload this page.