19
19
#include < htm/utils/Random.hpp>
20
20
#include < htm/utils/Log.hpp>
21
21
#include < htm/ntypes/Value.hpp>
22
+ #include " htm/utils/MovingAverage.hpp"
23
+ #include " htm/algorithms/AnomalyLikelihood.hpp"
22
24
#include < fstream>
23
25
24
26
using namespace htm ;
25
27
26
- static bool verbose = false ;
28
+ static bool verbose = true ;
27
29
#define VERBOSE if (verbose) std::cout << " "
28
30
29
31
30
32
// this runs as an executable
31
33
32
34
int main (int argc, char * argv[]) {
33
35
htm::UInt EPOCHS = 5000 ; // number of iterations (calls to encoder/SP/TP compute() )
36
+ #ifndef NDEBUG
37
+ EPOCHS = 2 ; // make test faster in Debug
38
+ #endif
39
+
34
40
const UInt DIM_INPUT = 1000 ; // Width of encoder output
35
41
const UInt COLS = 2048 ; // number of columns in SP, TP
36
42
const UInt CELLS = 8 ; // cells per column in TP
37
43
Random rnd (42 ); // uses fixed seed for deterministic output
38
44
std::ofstream ofs;
39
45
40
- std::string encoder_params = " {size: " + std::to_string (DIM_INPUT) + " , activeBits: 4 , radius: 0.5 , seed: 2019 }" ;
46
+ std::string encoder_params = " {size: " + std::to_string (DIM_INPUT) + " , sparsity: 0.2 , radius: 0.03 , seed: 2019, noise: 0.01 }" ;
41
47
std::string sp_global_params = " {columnCount: " + std::to_string (COLS) + " , globalInhibition: true}" ;
42
- std::string tm_params = " {activationThreshold: 9, cellsPerColumn: " + std::to_string (CELLS) + " }" ;
48
+ std::string tm_params = " {cellsPerColumn: " + std::to_string (CELLS) + " , orColumnOutputs: true }" ;
43
49
44
50
// Runtime arguments: napi_sine [epochs [filename]]
45
51
if (argc >= 2 ) {
@@ -51,7 +57,7 @@ int main(int argc, char* argv[]) {
51
57
52
58
try {
53
59
54
- std::cout << " initializing\n " ;
60
+ std::cout << " initializing. DIM_INPUT= " << DIM_INPUT << " , COLS= " << COLS << " , CELLS= " << CELLS << " \n " ;
55
61
56
62
Network net;
57
63
@@ -66,6 +72,7 @@ int main(int argc, char* argv[]) {
66
72
67
73
net.initialize ();
68
74
75
+
69
76
// /////////////////////////////////////////////////////////////
70
77
//
71
78
// .----------------.
@@ -92,39 +99,60 @@ int main(int argc, char* argv[]) {
92
99
// enable this to see a trace as it executes
93
100
// net.setLogLevel(LogLevel::LogLevel_Verbose);
94
101
95
- std::cout << " Running: \n " ;
102
+ std::cout << " Running: " << EPOCHS << " Iterations.\n " ;
103
+
104
+ float anLikely = 0 .0f ;
105
+ MovingAverage avgAnomaly (1000 );
106
+ AnomalyLikelihood anLikelihood;
107
+
96
108
// RUN
97
- for (size_t i = 0 ; i < EPOCHS; i++) {
109
+ float x = 0 .00f ;
110
+ for (size_t e = 0 ; e < EPOCHS; e++) {
98
111
// genarate some data to send to the encoder
99
- // -- A sin wave, one degree rotation per iteration, 1% noise added
100
- double data = std::sin (i * (3.1415 / 180 )) + (double )rnd.realRange (-0 .01f , +0 .1f );
112
+
113
+ // -- A sine wave, one degree rotation per iteration (an alternate function)
114
+ // double data = std::sin(i * (3.1415 / 180));
115
+
116
+ // -- sine wave, 0.01 radians per iteration (Note: first iteration is for x=0.01, not 0)
117
+ x += 0 .01f ; // step size for fn(x)
118
+ double data = std::sin (x);
101
119
encoder->setParameterReal64 (" sensedValue" , data); // feed data into RDSE encoder for this iteration.
102
120
103
121
// Execute an iteration.
104
122
net.run (1 );
105
123
106
- // output values
107
124
float an = ((float *)tm ->getOutputData (" anomaly" ).getBuffer ())[0 ];
108
- VERBOSE << " Epoch = " << i << std::endl;
109
- VERBOSE << " Data = " << data << std::endl;
110
- VERBOSE << " Encoder out = " << encoder->getOutputData (" encoded" ).getSDR ();
111
- VERBOSE << " SP (global) = " << sp_global->getOutputData (" bottomUpOut" ).getSDR ();
112
- VERBOSE << " TM output = " << tm ->getOutputData (" bottomUpOut" ).getSDR ();
113
- VERBOSE << " ActiveCells = " << tm ->getOutputData (" activeCells" ).getSDR ();
114
- VERBOSE << " winners = " << tm ->getOutputData (" predictedActiveCells" ).getSDR ();
115
- VERBOSE << " Anomaly = " << an << std::endl;
116
-
117
- // Save the data for plotting. <iteration>, <sin data>, <anomaly>\n
118
- if (ofs.is_open ())
119
- ofs << i << " ," << data << " ," << an << std::endl;
125
+ avgAnomaly.compute (an);
126
+ anLikely = anLikelihood.anomalyProbability (an);
127
+
128
+
129
+ // Save the data for plotting. <iteration>, <sin data>, <anomaly>, <likelyhood>\n
130
+ if (ofs.is_open ()) {
131
+ ofs << e << " ," << data << " ," << an << " ," << anLikely << std::endl;
132
+ }
133
+
134
+ if (e == EPOCHS - 1 )
135
+ {
136
+
137
+ // output values
138
+ float final_an = ((float *)tm ->getOutputData (" anomaly" ).getBuffer ())[0 ];
139
+ VERBOSE << " Result after " << e + 1 << " iterations.\n " ;
140
+ VERBOSE << " Anomaly(avg) = " << avgAnomaly.getCurrentAvg () << std::endl;
141
+ VERBOSE << " Anomaly(Likelihood) = " << anLikely << endl;
142
+ VERBOSE << " Encoder out = " << encoder->getOutputData (" encoded" ).getSDR ();
143
+ VERBOSE << " SP (global) = " << sp_global->getOutputData (" bottomUpOut" ).getSDR ();
144
+ VERBOSE << " TM predictive = " << tm ->getOutputData (" predictiveCells" ).getSDR ();
145
+ }
120
146
}
121
147
if (ofs.is_open ())
122
148
ofs.close ();
149
+
150
+
123
151
std::cout << " finished\n " ;
124
152
125
153
126
- } catch (Exception &e ) {
127
- std::cerr << e .what ();
154
+ } catch (Exception &ex ) {
155
+ std::cerr << ex .what ();
128
156
if (ofs.is_open ())
129
157
ofs.close ();
130
158
return 1 ;
0 commit comments