Skip to content
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

Example implemented using NetworkAPI #760

Merged
merged 11 commits into from
Nov 27, 2019
10 changes: 6 additions & 4 deletions bindings/py/tests/regions/network_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@

TEST_DATA = [0,1,2,3,4]
EXPECTED_RESULT1 = [ 4, 5 ]
EXPECTED_RESULT2 = [ 4, 11, 28, 42, 43, 87, 89, 93, 110, 127, 132, 137, 149, 187, 193]
EXPECTED_RESULT3 = [ 134, 371, 924, 1358, 1386, 2791, 2876, 2996, 3526, 4089, 4242, 4406, 4778, 5994, 6199]
#EXPECTED_RESULT2 = [ 4, 11, 28, 42, 43, 87, 89, 93, 110, 127, 132, 137, 149, 187, 193]
EXPECTED_RESULT2 = [ 16, 32, 44, 81, 104, 109, 114, 166, 197, 198 ]
EXPECTED_RESULT3 = [ 518, 1043, 1436, 2606, 3338, 3495, 3676, 5332, 6310, 6361 ]


class LinkRegion(PyRegion):
Expand Down Expand Up @@ -312,12 +313,13 @@ def testBuiltInRegions(self):

sp_output = sp.getOutputArray("bottomUpOut")
sdr = sp_output.getSDR()
#print(sdr)
self.assertTrue(np.array_equal(sdr.sparse, EXPECTED_RESULT2))

tm_output = tm.getOutputArray("predictedActiveCells")
sdr = tm_output.getSDR()
print(sdr.sparse)
print(EXPECTED_RESULT3)
#print(sdr)
#print(EXPECTED_RESULT3)
self.assertTrue(np.array_equal(sdr.sparse, EXPECTED_RESULT3))

def testExecuteCommand1(self):
Expand Down
26 changes: 26 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ set(examples_files
examples/hotgym/Hotgym.cpp # contains conflicting main()
examples/hotgym/HelloSPTP.cpp
examples/hotgym/HelloSPTP.hpp
examples/napi_sine/napi_sine.cpp
examples/mnist/MNIST_SP.cpp
)

Expand Down Expand Up @@ -356,6 +357,30 @@ else()
${EXTERNAL_INCLUDES}
)
endif()

#########################################################
## NetworkAPI version of benchmark_sine

set(src_executable_napi_sine napi_sine)
add_executable(${src_executable_napi_sine} examples/napi_sine/napi_sine.cpp)
# link with the static library
target_link_libraries(${src_executable_napi_sine}
${INTERNAL_LINKER_FLAGS}
${core_library}
${COMMON_OS_LIBS}
)
target_compile_options( ${src_executable_napi_sine} PUBLIC ${INTERNAL_CXX_FLAGS})
target_compile_definitions(${src_executable_napi_sine} PRIVATE ${COMMON_COMPILER_DEFINITIONS})
target_include_directories(${src_executable_napi_sine} PRIVATE
${CORE_LIB_INCLUDES}
${EXTERNAL_INCLUDES}
)
add_custom_target(sine_napi_run
COMMAND ${src_executable_napi_sine}
DEPENDS ${src_executable_napi_sine}
COMMENT "Executing ${src_executable_napi_sine}"
VERBATIM)

#########################################################
## MNIST Spatial Pooler Example
#
Expand Down Expand Up @@ -392,6 +417,7 @@ install(TARGETS

install(TARGETS
${src_executable_hotgym}
${src_executable_napi_sine}
${src_executable_mnistsp}
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
Expand Down
16 changes: 9 additions & 7 deletions src/examples/hotgym/HelloSPTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ EPOCHS = 2; // make test faster in Debug
input.addNoise(0.01f, rnd); //change 1% of the SDR for each iteration, this makes a random sequence, but seemingly stable
tRng.stop();

//SP (global x local)
//SP (global and local)
if(useSPlocal) {
tSPloc.start();
spLocal.compute(input, true, outSPlocal);
Expand Down Expand Up @@ -154,7 +154,7 @@ EPOCHS = 2; // make test faster in Debug
avgAnomOld_ = avgAnom10.getCurrentAvg(); //update
}
tAnLikelihood.start();
anLikelihood.anomalyProbability(an); //FIXME AnLikelihood is 0.0, probably not working correctly
anLikely = anLikelihood.anomalyProbability(an);
tAnLikelihood.stop();


Expand All @@ -173,13 +173,14 @@ EPOCHS = 2; // make test faster in Debug
<< "\n";

// output values
cout << "Epoch = " << e << endl;
cout << "Epoch = " << e+1 << endl;
cout << "Anomaly = " << an << endl;
cout << "Anomaly (avg) = " << avgAnom10.getCurrentAvg() << endl;
cout << "Anomaly (Likelihood) = " << anLikely << endl;
cout << "SP (g)= " << outSP << endl;
cout << "SP (l)= " << outSPlocal <<endl;
cout << "TM= " << outTM << endl;
cout << "input = " << input << endl;
if(useSPlocal) cout << "SP (g)= " << outSP << endl;
if(useSPlocal) cout << "SP (l)= " << outSPlocal <<endl;
if(useTM) cout << "TM= " << outTM << endl;

//timers
cout << "==============TIMERS============" << endl;
Expand Down Expand Up @@ -220,7 +221,7 @@ EPOCHS = 2; // make test faster in Debug
const float goldAnAvg = 0.411894f; // ...the averaged value, on the other hand, should improve/decrease.

#ifdef _ARCH_DETERMINISTIC
if(EPOCHS == 5000) {
if(e+1 == 5000) {
//these hand-written values are only valid for EPOCHS = 5000 (default), but not for debug and custom runs.
NTA_CHECK(input == goldEnc) << "Deterministic output of Encoder failed!\n" << input << "should be:\n" << goldEnc;
if(useSPglobal) { NTA_CHECK(outSPglobal == goldSP) << "Deterministic output of SP (g) failed!\n" << outSP << "should be:\n" << goldSP; }
Expand All @@ -230,6 +231,7 @@ EPOCHS = 2; // make test faster in Debug
<< "Deterministic output of Anomaly failed! " << an << "should be: " << goldAn;
NTA_CHECK(static_cast<UInt>(avgAnom10.getCurrentAvg() * 10000.0f) == static_cast<UInt>(goldAnAvg * 10000.0f))
<< "Deterministic average anom score failed:" << avgAnom10.getCurrentAvg() << " should be: " << goldAnAvg;
std::cout << "outputs match\n";
}
#endif

Expand Down
83 changes: 83 additions & 0 deletions src/examples/napi_sine/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# 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 a quantized format. This is passed to an instance of SpatialPooler (SP).

The output of the SP 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 (global) |
// | (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...
163 changes: 163 additions & 0 deletions src/examples/napi_sine/napi_sine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
/* ---------------------------------------------------------------------
* 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}";

// 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", "RDSERegion", 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);

// Setup data flows between regions
net.link("encoder", "sp_global", "", "", "encoded", "bottomUpIn");
net.link("sp_global", "tm", "", "", "bottomUpOut", "bottomUpIn");

net.initialize();


///////////////////////////////////////////////////////////////
//
// .----------------.
// | encoder |
// data--->| (RDSERegion) |
// | |
// `-----------------'
// |
// .-----------------.
// | sp_global |
// | (SPRegion) |
// | |
// `-----------------'
// |
// .-----------------.
// | tm |
// | (TMRegion) |
// | |
// `-----------------'
//
//////////////////////////////////////////////////////////////////


// 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();


std::cout << "finished\n";


} catch (Exception &ex) {
std::cerr << ex.what();
if (ofs.is_open())
ofs.close();
return 1;
}

return 0;
}

Loading