Skip to content
This repository has been archived by the owner on Sep 1, 2023. It is now read-only.

Commit

Permalink
SP: add check for unstable params in constructor
Browse files Browse the repository at this point in the history
on each initialization, SP is tested to produce stable output.
  • Loading branch information
breznak committed Jan 12, 2018
1 parent 30f9227 commit c01a95b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 14 additions & 0 deletions src/nupic/algorithms/SpatialPooler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,17 @@ const vector<Real>& SpatialPooler::getBoostedOverlaps() const
return boostedOverlaps_;
}

/** helper method that checks SP output is stable for given configuration */
bool checkUnstableParams_(SpatialPooler &sp) {
vector<UInt> input(sp.getNumInputs(), 1); //auto size of input
vector<UInt> out1(sp.getNumColumns(), 0);
vector<UInt> out2(sp.getNumColumns(), 0);
sp.compute(input.data(), true, out1.data());
sp.compute(input.data(), true, out2.data());
//TODO should we add SP.reset() and call it here?
return std::equal(std::begin(out1), std::end(out1), std::begin(out2)); //compare all, element wise
}

void SpatialPooler::initialize(vector<UInt> inputDimensions,
vector<UInt> columnDimensions,
UInt potentialRadius,
Expand Down Expand Up @@ -593,6 +604,9 @@ void SpatialPooler::initialize(vector<UInt> inputDimensions,
printParameters();
std::cout << "CPP SP seed = " << seed << std::endl;
}

//check for reasonable params
NTA_CHECK(checkUnstableParams_(*this)); //TODO the assert runs only at debug builds, make mandatory?
}

void SpatialPooler::compute(UInt inputArray[], bool learn,
Expand Down
2 changes: 1 addition & 1 deletion src/test/unit/algorithms/SpatialPoolerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2244,7 +2244,7 @@ namespace {
vector<UInt> out2(sp.getNumColumns(), 0);
sp.compute(input.data(), true, out1.data());
sp.compute(input.data(), true, out2.data());
EXPECT_EQ(out1, out2);
EXPECT_EQ(out1, out2); //not necessary with the check in SP initialize(), but keep here as example
}


Expand Down

0 comments on commit c01a95b

Please sign in to comment.