Skip to content

Commit 9ac7eee

Browse files
committed
code cleanup
1 parent a940b47 commit 9ac7eee

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

src/examples/mnist/MNIST_SP.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void setup() {
8888
/* synPermConnected */ 0.5f, //no difference, let's leave at 0.5 in the middle
8989
/* minPctOverlapDutyCycles */ 0.2f, //speed of re-learning?
9090
/* dutyCyclePeriod */ 1402,
91-
/* boostStrength */ 7.0f, // Boosting does help, but entropy is high, on MNIST it does not matter, for learning with TM prefer boosting off (=0.0), or "neutral"=1.0
91+
/* boostStrength */ 7.0f, // Boosting does help, but entropy is high, on MNIST it does not matter, for learning with TM prefer boosting off (BOOSTING_DISABLED), or "neutral"=1.0
9292
/* seed */ 4u,
9393
/* spVerbosity */ 1u,
9494
/* wrapAround */ true); // does not matter (helps slightly)

src/htm/algorithms/SpatialPooler.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -758,20 +758,20 @@ void applyBoosting_(const UInt i,
758758
const vector<Real>& actualDensity,
759759
const Real boost,
760760
vector<Real>& output) {
761-
762-
if(boost == SpatialPooler::BOOSTING_LOG) { //logarithmic boosting
761+
if(boost == SpatialPooler::BOOSTING_DISABLED) output[i] = actualDensity[i]; //no change
762+
else if(boost == SpatialPooler::BOOSTING_LOG) { //logarithmic boosting
763763
output[i] = log2(actualDensity[i]) / log2(targetDensity);
764-
} else if(boost > 0) { //exponential boost
764+
} else if(boost >= SpatialPooler::BOOSTING_EXP) { //exponential boost
765765
output[i] = exp((targetDensity - actualDensity[i]) * boost);
766-
} //else: BOOSTING_DISABLED
766+
} else NTA_THROW << "Invalid boost mode! " << boost;
767767
}
768768

769769

770770
void SpatialPooler::updateBoostFactorsGlobal_() {
771771
Real targetDensity;
772772
if (numActiveColumnsPerInhArea_ > 0) {
773773
UInt inhibitionArea =
774-
(UInt)(pow((Real)(2 * inhibitionRadius_ + 1), (Real)columnDimensions_.size()));
774+
(UInt)(pow((Real)(2 * inhibitionRadius_ + 1), (Real)columnDimensions_.size())); //FIXME make this correct for nD with smaller dims: (28,28) vs (28,28,1) vs (28*28, 1)
775775
inhibitionArea = min(inhibitionArea, numColumns_);
776776
NTA_ASSERT(inhibitionArea > 0);
777777
targetDensity = ((Real)numActiveColumnsPerInhArea_) / inhibitionArea;
@@ -792,12 +792,12 @@ void SpatialPooler::updateBoostFactorsLocal_() {
792792
Real localActivityDensity = 0.0f;
793793

794794
if (wrapAround_) {
795-
for(auto neighbor: WrappingNeighborhood(i, inhibitionRadius_, columnDimensions_)) {
795+
for(const auto neighbor: WrappingNeighborhood(i, inhibitionRadius_, columnDimensions_)) {
796796
localActivityDensity += activeDutyCycles_[neighbor];
797797
numNeighbors += 1;
798798
}
799799
} else {
800-
for(auto neighbor: Neighborhood(i, inhibitionRadius_, columnDimensions_)) {
800+
for(const auto neighbor: Neighborhood(i, inhibitionRadius_, columnDimensions_)) {
801801
localActivityDensity += activeDutyCycles_[neighbor];
802802
numNeighbors += 1;
803803
}

src/htm/algorithms/SpatialPooler.hpp

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ namespace htm {
3535

3636
using namespace std;
3737

38-
static const int DISABLED = -1; //value denoting a feature is disabled
39-
4038
/**
4139
* CLA spatial pooler implementation in C++.
4240
*
@@ -63,19 +61,30 @@ static const int DISABLED = -1; //value denoting a feature is disabled
6361
class SpatialPooler : public Serializable
6462
{
6563
public:
64+
static const constexpr Real DISABLED = -1.0f; //value denoting a feature is disabled
65+
//boosting modes:
6666
static const constexpr Real BOOSTING_DISABLED = 0.0f;
67-
static const constexpr Real BOOSTING_LOG = -1234.0f;
67+
static const constexpr Real BOOSTING_LOG = -1.0f * htm::Epsilon; //negative value closest to 0, so it's possible to search <BOOSTING_LOG, (DISABLED=0.0),..10.0> in parameter optimization to try all 3 combinations of boosting.
68+
static const constexpr Real BOOSTING_EXP = 1.0f * htm::Epsilon; //any value > BOOSTING_DISABLED enables the exponential boosting mode
6869

6970
SpatialPooler();
70-
SpatialPooler(const vector<UInt> inputDimensions, const vector<UInt> columnDimensions,
71-
UInt potentialRadius = 16u, Real potentialPct = 0.5f,
72-
bool globalInhibition = true, Real localAreaDensity = DISABLED,
71+
SpatialPooler(const vector<UInt> inputDimensions,
72+
const vector<UInt> columnDimensions,
73+
UInt potentialRadius = 16u,
74+
Real potentialPct = 0.5f,
75+
bool globalInhibition = true,
76+
Real localAreaDensity = DISABLED,
7377
Int numActiveColumnsPerInhArea = 10u,
74-
UInt stimulusThreshold = 0u, Real synPermInactiveDec = 0.008f,
75-
Real synPermActiveInc = 0.05f, Real synPermConnected = 0.1f,
78+
UInt stimulusThreshold = 0u,
79+
Real synPermInactiveDec = 0.008f,
80+
Real synPermActiveInc = 0.05f,
81+
Real synPermConnected = 0.1f,
7682
Real minPctOverlapDutyCycles = 0.001f,
77-
UInt dutyCyclePeriod = 1000u, Real boostStrength = 0.0f,
78-
Int seed = 1, UInt spVerbosity = 0u, bool wrapAround = true);
83+
UInt dutyCyclePeriod = 1000u,
84+
Real boostStrength = BOOSTING_DISABLED,
85+
Int seed = 1,
86+
UInt spVerbosity = 0u,
87+
bool wrapAround = true);
7988

8089
virtual ~SpatialPooler() {}
8190

@@ -204,6 +213,9 @@ class SpatialPooler : public Serializable
204213
This helps achieving the target sparsity of the output.
205214
However, too much boosting may also lead to instability of SP outputs.
206215
216+
Notes:
217+
- Log boosting does not require a parameter, but it is slower than exp.
218+
207219
208220
@param seed Seed for our random number generator. If seed is < 0
209221
a randomly generated seed is used. The behavior of the spatial

src/htm/types/Types.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ typedef std::size_t Size;
130130
* numeric_limits<float>::epsilon() == 1.19209e-7
131131
* numeric_limits<double>::epsilon() == 2.22045e-16
132132
*/
133-
static const htm::Real32 Epsilon = htm::Real(1e-6);
133+
static const constexpr htm::Real32 Epsilon = htm::Real(1e-6);
134134

135135
/**
136136
* Represents a signed integer.

0 commit comments

Comments
 (0)