Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

SP Cleanup: Use only sp.avgConnectedSpanForColumnND #119

Merged
merged 3 commits into from
Nov 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 0 additions & 59 deletions src/nupic/algorithms/SpatialPooler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,6 @@ static Real round5_(const Real f)
return ((Real) ((Int) (f * 100000.0f))) / 100000.0f;
}

class CoordinateConverter2D { //TODO move to Topology
breznak marked this conversation as resolved.
Show resolved Hide resolved

public:
CoordinateConverter2D(UInt nrows, UInt ncols)
: // TODO param nrows is unused
ncols_(ncols)
{
NTA_ASSERT(ncols > 0u);
}
UInt toRow(UInt index) const { return index / ncols_; };
UInt toCol(UInt index) const { return index % ncols_; };
UInt toIndex(UInt row, UInt col) const { return row * ncols_ + col; };

private:
UInt ncols_;
};

class CoordinateConverterND {

public:
Expand Down Expand Up @@ -740,48 +723,6 @@ Real SpatialPooler::avgColumnsPerInput_() const {
}


Real SpatialPooler::avgConnectedSpanForColumn1D_(UInt column) const {
breznak marked this conversation as resolved.
Show resolved Hide resolved
NTA_ASSERT(column < numColumns_);
NTA_ASSERT(inputDimensions_.size() == 1);

const vector<UInt> connectedSparse = connectedSynapses_.getSparseRow(column);
if (connectedSparse.empty())
return 0;
auto minmax = minmax_element(connectedSparse.begin(), connectedSparse.end());
return *minmax.second /*max*/ - *minmax.first /*min*/ + 1;
}


Real SpatialPooler::avgConnectedSpanForColumn2D_(UInt column) const {
NTA_ASSERT(column < numColumns_);
NTA_ASSERT(inputDimensions_.size() == 2);

const UInt nrows = inputDimensions_[0];
const UInt ncols = inputDimensions_[1];

const CoordinateConverter2D conv(nrows, ncols);

const vector<UInt> connectedSparse = connectedSynapses_.getSparseRow(column);
vector<UInt> rows, cols;
for (auto &elem : connectedSparse) {
rows.push_back(conv.toRow(elem));
cols.push_back(conv.toCol(elem));
}

if (rows.empty() && cols.empty()) {
return 0;
}

auto minmaxRows = minmax_element(rows.begin(), rows.end());
const UInt rowSpan = *minmaxRows.second /*max*/ - *minmaxRows.first /*min*/ + 1;

auto minmaxCols = minmax_element(cols.begin(), cols.end());
const UInt colSpan = *minmaxCols.second - *minmaxCols.first + 1;

return (rowSpan + colSpan) / 2.0f;
}


Real SpatialPooler::avgConnectedSpanForColumnND_(UInt column) const {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's interesting that the current SP does not use the 1D/2D, but only nD already?!

NTA_ASSERT(column < numColumns_);

Expand Down
20 changes: 0 additions & 20 deletions src/nupic/algorithms/SpatialPooler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1056,26 +1056,6 @@ class SpatialPooler : public Serializable
*/
Real avgColumnsPerInput_() const;

/**
The range of connected synapses for column. This is used to
calculate the inhibition radius. This variation of the function only
supports a 1 dimensional column topology.

@param column An int number identifying a column in the permanence,
potential and connectivity matrices.
*/
Real avgConnectedSpanForColumn1D_(UInt column) const;

/**
The range of connectedSynapses per column, averaged for each dimension.
This vaule is used to calculate the inhibition radius. This variation of
the function only supports a 2 dimensional column topology.

@param column An int number identifying a column in the permanence,
potential and connectivity matrices.
*/
Real avgConnectedSpanForColumn2D_(UInt column) const;

/**
The range of connectedSynapses per column, averaged for each dimension.
This vaule is used to calculate the inhibition radius. This variation of
Expand Down
6 changes: 3 additions & 3 deletions src/test/unit/algorithms/SpatialPoolerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,7 @@ TEST(SpatialPoolerTest, testAvgConnectedSpanForColumn1D) {

for (UInt i = 0; i < numColumns; i++) {
sp.setPermanence(i, permArr[i]);
UInt result = sp.avgConnectedSpanForColumn1D_(i);
UInt result = sp.avgConnectedSpanForColumnND_(i);
ASSERT_TRUE(result == trueAvgConnectedSpan[i]);
}
}
Expand Down Expand Up @@ -663,7 +663,7 @@ TEST(SpatialPoolerTest, testAvgConnectedSpanForColumn2D) {

for (UInt i = 0; i < numColumns; i++) {
sp.setPermanence(i, permArr1[i]);
UInt result = sp.avgConnectedSpanForColumn2D_(i);
UInt result = sp.avgConnectedSpanForColumnND_(i);
ASSERT_TRUE(result == (trueAvgConnectedSpan1[i]));
}

Expand All @@ -690,7 +690,7 @@ TEST(SpatialPoolerTest, testAvgConnectedSpanForColumn2D) {

for (UInt i = 0; i < numColumns; i++) {
sp.setPermanence(i, permArr2[i]);
UInt result = sp.avgConnectedSpanForColumn2D_(i);
UInt result = sp.avgConnectedSpanForColumnND_(i);
ASSERT_TRUE(result == (trueAvgConnectedSpan2[i] + 1) / 2);
}
}
Expand Down