|
| 1 | +/* --------------------------------------------------------------------- |
| 2 | + * HTM Community Edition of NuPIC |
| 3 | + * Copyright (C) 2013-2015, Numenta, Inc. |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU Affero Public License version 3 as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the GNU Affero Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU Affero Public License |
| 15 | + * along with this program. If not, see http://www.gnu.org/licenses. |
| 16 | + * --------------------------------------------------------------------- */ |
| 17 | + |
| 18 | +#include <htm/engine/Network.hpp> |
| 19 | +#include <htm/utils/Random.hpp> |
| 20 | +#include <htm/utils/Log.hpp> |
| 21 | +#include <htm/ntypes/Value.hpp> |
| 22 | +#include "htm/utils/MovingAverage.hpp" |
| 23 | +#include "htm/algorithms/AnomalyLikelihood.hpp" |
| 24 | +#include <fstream> |
| 25 | + |
| 26 | +using namespace htm; |
| 27 | + |
| 28 | +static bool verbose = true; |
| 29 | +#define VERBOSE if (verbose) std::cout << " " |
| 30 | + |
| 31 | + |
| 32 | +//this runs as an executable |
| 33 | + |
| 34 | +int main(int argc, char* argv[]) { |
| 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 | + |
| 40 | + const UInt DIM_INPUT = 1000; // Width of encoder output |
| 41 | + const UInt COLS = 2048; // number of columns in SP, TP |
| 42 | + const UInt CELLS = 8; // cells per column in TP |
| 43 | + Random rnd(42); // uses fixed seed for deterministic output |
| 44 | + std::ofstream ofs; |
| 45 | + |
| 46 | + std::string encoder_params = "{size: " + std::to_string(DIM_INPUT) + ", sparsity: 0.2, radius: 0.03, seed: 2019, noise: 0.01}"; |
| 47 | + std::string sp_global_params = "{columnCount: " + std::to_string(COLS) + ", globalInhibition: true}"; |
| 48 | + std::string tm_params = "{cellsPerColumn: " + std::to_string(CELLS) + ", orColumnOutputs: true}"; |
| 49 | + |
| 50 | + // Runtime arguments: napi_sine [epochs [filename]] |
| 51 | + if(argc >= 2) { |
| 52 | + EPOCHS = std::stoi(argv[1]); // number of iterations (default 5000) |
| 53 | + } |
| 54 | + if (argc >= 3) { |
| 55 | + ofs.open(argv[2], std::ios::out); // output filename (for plotting) |
| 56 | + } |
| 57 | + |
| 58 | + try { |
| 59 | + |
| 60 | + std::cout << "initializing. DIM_INPUT=" << DIM_INPUT << ", COLS=" << COLS << ", CELLS=" << CELLS << "\n"; |
| 61 | + |
| 62 | + Network net; |
| 63 | + |
| 64 | + // Declare the regions to use |
| 65 | + std::shared_ptr<Region> encoder = net.addRegion("encoder", "RDSERegion", encoder_params); |
| 66 | + std::shared_ptr<Region> sp_global = net.addRegion("sp_global", "SPRegion", sp_global_params); |
| 67 | + std::shared_ptr<Region> tm = net.addRegion("tm", "TMRegion", tm_params); |
| 68 | + |
| 69 | + // Setup data flows between regions |
| 70 | + net.link("encoder", "sp_global", "", "", "encoded", "bottomUpIn"); |
| 71 | + net.link("sp_global", "tm", "", "", "bottomUpOut", "bottomUpIn"); |
| 72 | + |
| 73 | + net.initialize(); |
| 74 | + |
| 75 | + |
| 76 | + /////////////////////////////////////////////////////////////// |
| 77 | + // |
| 78 | + // .----------------. |
| 79 | + // | encoder | |
| 80 | + // data--->| (RDSERegion) | |
| 81 | + // | | |
| 82 | + // `-----------------' |
| 83 | + // | |
| 84 | + // .-----------------. |
| 85 | + // | sp_global | |
| 86 | + // | (SPRegion) | |
| 87 | + // | | |
| 88 | + // `-----------------' |
| 89 | + // | |
| 90 | + // .-----------------. |
| 91 | + // | tm | |
| 92 | + // | (TMRegion) | |
| 93 | + // | | |
| 94 | + // `-----------------' |
| 95 | + // |
| 96 | + ////////////////////////////////////////////////////////////////// |
| 97 | + |
| 98 | + |
| 99 | + // enable this to see a trace as it executes |
| 100 | + //net.setLogLevel(LogLevel::LogLevel_Verbose); |
| 101 | + |
| 102 | + std::cout << "Running: " << EPOCHS << " Iterations.\n "; |
| 103 | + |
| 104 | + float anLikely = 0.0f; |
| 105 | + MovingAverage avgAnomaly(1000); |
| 106 | + AnomalyLikelihood anLikelihood; |
| 107 | + |
| 108 | + // RUN |
| 109 | + float x = 0.00f; |
| 110 | + for (size_t e = 0; e < EPOCHS; e++) { |
| 111 | + // genarate some data to send to the encoder |
| 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); |
| 119 | + encoder->setParameterReal64("sensedValue", data); // feed data into RDSE encoder for this iteration. |
| 120 | + |
| 121 | + // Execute an iteration. |
| 122 | + net.run(1); |
| 123 | + |
| 124 | + float an = ((float *)tm->getOutputData("anomaly").getBuffer())[0]; |
| 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 | + VERBOSE << "Result after " << e + 1 << " iterations.\n"; |
| 139 | + VERBOSE << " Anomaly = " << an << std::endl; |
| 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 | + } |
| 146 | + } |
| 147 | + if (ofs.is_open()) |
| 148 | + ofs.close(); |
| 149 | + |
| 150 | + |
| 151 | + std::cout << "finished\n"; |
| 152 | + |
| 153 | + |
| 154 | + } catch (Exception &ex) { |
| 155 | + std::cerr << ex.what(); |
| 156 | + if (ofs.is_open()) |
| 157 | + ofs.close(); |
| 158 | + return 1; |
| 159 | + } |
| 160 | + |
| 161 | + return 0; |
| 162 | +} |
| 163 | + |
0 commit comments