Skip to content
Merged
25 changes: 25 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
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...
135 changes: 135 additions & 0 deletions src/examples/napi_sine/napi_sine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/* ---------------------------------------------------------------------
* 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_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 5000)
}
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_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: \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 (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;
}

2 changes: 1 addition & 1 deletion src/htm/utils/Random.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class Random : public Serializable {
* return random from range [from, to)
*/
Real realRange(Real from, Real to) {
NTA_ASSERT(from <= to);
NTA_ASSERT(from <= to) << "realRange: invalid range.";
const Real split = to - from;
return from + static_cast<Real>(split * getReal64());
}
Expand Down