forked from numenta/nupic.core-legacy
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathnapi_hello.cpp
173 lines (131 loc) · 6.29 KB
/
napi_hello.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* ---------------------------------------------------------------------
* 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/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.csv";
// 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", "FileOutputRegion", "{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", "UniformLink", "", "bottomUpOut", "dataIn");
net.initialize();
///////////////////////////////////////////////////////////////
//
// .------------------.
// | encoder |
// data--->|(RDSEEncoderRegion)|
// | |
// `-------------------'
// |
// .-----------------.
// | sp_global |
// | (SPRegion) |
// | |
// `-----------------'
// |
// .-----------------.
// | tm |
// | (TMRegion) |---->CSV file
// | |
// `-----------------'
//
//////////////////////////////////////////////////////////////////
// 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;
}