Skip to content
24 changes: 12 additions & 12 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ set(examples_files
examples/hotgym/Hotgym.cpp # contains conflicting main()
examples/hotgym/HelloSPTP.cpp
examples/hotgym/HelloSPTP.hpp
examples/hotgym_NetworkAPI/hotgym_napi.cpp
examples/napi_sine/napi_sine.cpp
examples/mnist/MNIST_SP.cpp
)

Expand Down Expand Up @@ -359,26 +359,26 @@ else()
endif()

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

set(src_executable_hotgym_napi hotgym_napi)
add_executable(${src_executable_hotgym_napi} examples/hotgym_NetworkAPI/hotgym_napi.cpp)
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_hotgym_napi}
target_link_libraries(${src_executable_napi_sine}
${INTERNAL_LINKER_FLAGS}
${core_library}
${COMMON_OS_LIBS}
)
target_compile_options( ${src_executable_hotgym_napi} PUBLIC ${INTERNAL_CXX_FLAGS})
target_compile_definitions(${src_executable_hotgym_napi} PRIVATE ${COMMON_COMPILER_DEFINITIONS})
target_include_directories(${src_executable_hotgym_napi} PRIVATE
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(hotgym_napi_run
COMMAND ${src_executable_hotgym_napi}
DEPENDS ${src_executable_hotgym_napi}
COMMENT "Executing ${src_executable_hotgym_napi}"
add_custom_target(sine_napi_run
COMMAND ${src_executable_napi_sine}
DEPENDS ${src_executable_napi_sine}
COMMENT "Executing ${src_executable_napi_sine}"
VERBATIM)

#########################################################
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# C++ example using Network API
The program `hotgym_napi` is an example of an app using the Network API tools available in the htm.core library. In this example we generate a sin 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.
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),
Comment thread
breznak marked this conversation as resolved.
Outdated
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.

```
///////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -64,17 +71,14 @@ Each "region" is a wrapper around an algorithm. This wrapper provides a uniform
## Usage

```
hotgym_napi [iterations [filename]]
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
```

## Plotting

The `hotgym_napi` program can output data if a filename is specified. Any plotting program can be used to display this CSD data.

## 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...
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#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;
Expand All @@ -36,9 +37,14 @@ int main(int argc, char* argv[]) {
Random rnd(42); // uses fixed seed for deterministic output
std::ofstream ofs;

// Runtime arguments: hotgym_napi [epochs [filename]]
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 5000)
EPOCHS = std::stoi(argv[1]); // number of iterations (default 500)
Comment thread
breznak marked this conversation as resolved.
Outdated
}
if (argc >= 3) {
ofs.open(argv[2], std::ios::out); // output filename (for plotting)
Expand All @@ -51,16 +57,15 @@ int main(int argc, char* argv[]) {
Network net;

// Declare the regions to use
std::string encoder_parameters = "{size: " + std::to_string(DIM_INPUT) + ", activeBits: 4, radius: 0.5, seed: 2019 }";
std::shared_ptr<Region> region1 = net.addRegion("region1", "RDSERegion", encoder_parameters);
std::shared_ptr<Region> region2a = net.addRegion("region2a", "SPRegion", "{columnCount: " + std::to_string(COLS) + "}");
std::shared_ptr<Region> region2b = net.addRegion("region2b", "SPRegion", "{columnCount: " + std::to_string(COLS) + ", globalInhibition: true}");
std::shared_ptr<Region> region3 = net.addRegion("region3", "TMRegion", "{activationThreshold: 9, cellsPerColumn: " + std::to_string(CELLS) + "}");
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("region1", "region2a", "", "", "encoded", "bottomUpIn");
net.link("region1", "region2b", "", "", "encoded", "bottomUpIn");
net.link("region2b", "region3", "", "", "bottomUpOut", "bottomUpIn");
// net.link("encoder", "sp_local", "", "", "encoded", "bottomUpIn");
net.link("encoder", "sp_global", "", "", "encoded", "bottomUpIn");
net.link("sp_global", "tm", "", "", "bottomUpOut", "bottomUpIn");

net.initialize();

Expand All @@ -73,13 +78,13 @@ int main(int argc, char* argv[]) {
// `------------------'
// | |
// .------------------. .------------------.
// | SP (local) | | SP (global) |
// | | | |
// | sp_local | | sp_global |
// | (SPRegion) | | (SPRegion) |
// | | | |
// `------------------' `------------------'
// |
// .------------------.
// | TM |
// | tm |
// | (TMRegion) |
// | |
// `------------------'
Expand All @@ -88,29 +93,29 @@ int main(int argc, char* argv[]) {


// enable this to see a trace as it executes
// net.setLogLevel(LogLevel::LogLevel_Verbose);
//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);
region1->setParameterReal64("sensedValue", data); // feed data into RDSE encoder for this iteration.
encoder->setParameterReal64("sensedValue", data); // feed data into RDSE encoder for this iteration.

// Execute an iteration.
net.run(1);

// output values
double an = ((double *)region3->getOutputData("anomaly").getBuffer())[0];
float an = ((float *)tm->getOutputData("anomaly").getBuffer())[0];
VERBOSE << "Epoch = " << i << std::endl;
VERBOSE << " Data = " << data << std::endl;
VERBOSE << " Encoder out = " << region1->getOutputData("encoded").getSDR() << std::endl;
VERBOSE << " SP (local) = " << region2a->getOutputData("bottomUpOut").getSDR() << std::endl;
VERBOSE << " SP (global) = " << region2b->getOutputData("bottomUpOut").getSDR() << std::endl;
VERBOSE << " TM output = " << region3->getOutputData("bottomUpOut").getSDR() << std::endl;
VERBOSE << " ActiveCells = " << region3->getOutputData("activeCells").getSDR() << std::endl;
VERBOSE << " winners = " << region3->getOutputData("predictedActiveCells").getSDR() << 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
Expand All @@ -119,6 +124,8 @@ int main(int argc, char* argv[]) {
}
if (ofs.is_open())
ofs.close();
std::cout << "finished\n";


} catch (Exception &e) {
std::cerr << e.what();
Expand Down