int GLWidget::stepNuPIC(vector<UInt>& inputSDR, bool learn) { // clear the active columns indicies array m_activeColumnIndicies.assign(NUM_COLUMNS, 0); gs_SP.compute(inputSDR.data(), learn, m_activeColumnIndicies.data()); gs_SP.stripUnlearnedColumns(m_activeColumnIndicies.data()); gs_TM.compute(m_activeColumnIndicies.size(), m_activeColumnIndicies.data(), learn); return 0; }
void testSP() { Random random(10); nupic::Timer testTimer; const UInt inputSize = 500; const UInt numColumns = 500; const UInt w = 50; vector<UInt> inputDims{inputSize}; vector<UInt> colDims{numColumns}; SpatialPooler sp1; sp1.initialize(inputDims, colDims); UInt input[inputSize]; for (UInt i = 0; i < inputSize; ++i) { if (i < w) { input[i] = 1; } else { input[i] = 0; } } UInt output[numColumns]; for (UInt i = 0; i < 10000; ++i) { random.shuffle(input, input + inputSize); sp1.compute(input, true, output); } // Now we reuse the last input to test after serialization vector<UInt> activeColumnsBefore; for (UInt i = 0; i < numColumns; ++i) { if (output[i] == 1) { activeColumnsBefore.push_back(i); } } // Save initial trained model ofstream osA("outA.proto", ofstream::binary); sp1.write(osA); osA.close(); ofstream osC("outC.proto", ofstream::binary); sp1.save(osC); osC.close(); SpatialPooler sp2; long timeA = 0, timeC = 0; for (UInt i = 0; i < 100; ++i) { // Create new input random.shuffle(input, input + inputSize); // Get expected output UInt outputBaseline[numColumns]; sp1.compute(input, true, outputBaseline); UInt outputA[numColumns]; UInt outputC[numColumns]; // A - First do iostream version { SpatialPooler spTemp; testTimer.start(); // Deserialize ifstream is("outA.proto", ifstream::binary); spTemp.read(is); is.close(); // Feed new record through spTemp.compute(input, true, outputA); // Serialize ofstream os("outA.proto", ofstream::binary); spTemp.write(os); os.close(); testTimer.stop(); timeA = timeA + testTimer.getElapsed(); } // C - Next do old version { SpatialPooler spTemp; testTimer.start(); // Deserialize ifstream is("outC.proto", ifstream::binary); spTemp.load(is); is.close(); // Feed new record through spTemp.compute(input, true, outputC); // Serialize ofstream os("outC.proto", ofstream::binary); spTemp.save(os); os.close(); testTimer.stop(); timeC = timeC + testTimer.getElapsed(); } for (UInt i = 0; i < numColumns; ++i) { NTA_ASSERT(outputBaseline[i] == outputA[i]); NTA_ASSERT(outputBaseline[i] == outputC[i]); } } remove("outA.proto"); remove("outC.proto"); cout << "Time for iostream capnp: " << ((Real)timeA / 1000.0) << endl; cout << "Time for old method: " << ((Real)timeC / 1000.0) << endl; }