forked from numenta/nupic.core-legacy
/
py_SpatialPooler.cpp
478 lines (407 loc) · 21.4 KB
/
py_SpatialPooler.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/* ---------------------------------------------------------------------
* HTM Community Edition of NuPIC
* Copyright (C) 2018, Numenta, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero Public License for more details.
*
* You should have received a copy of the GNU Affero Public License
* along with this program. If not, see http://www.gnu.org/licenses.
*
* Author: @chhenning, 2018
* --------------------------------------------------------------------- */
/** @file
PyBind11 bindings for SpatialPooler class
*/
#include <tuple>
#include <iostream>
#include <bindings/suppress_register.hpp> //include before pybind11.h
#include <pybind11/pybind11.h>
#include <pybind11/iostream.h>
#include <pybind11/numpy.h>
#include <pybind11/stl.h>
#include <htm/algorithms/SpatialPooler.hpp>
#include <htm/types/Sdr.hpp>
#include "bindings/engine/py_utils.hpp"
namespace htm_ext
{
namespace py = pybind11;
using namespace htm;
void init_Spatial_Pooler(py::module& m)
{
py::class_<SpatialPooler> py_SpatialPooler(m, "SpatialPooler");
py_SpatialPooler.def(
py::init<vector<UInt>
, vector<UInt>
, UInt
, Real
, bool
, Real
, UInt
, UInt
, Real
, Real
, Real
, Real
, UInt
, Real
, Int
, UInt
, bool>()
, py::call_guard<py::scoped_ostream_redirect,
py::scoped_estream_redirect>(),
R"(
Argument inputDimensions A list of integers representing the
dimensions of the input vector. Format is [height, width,
depth, ...], where each value represents the size of the
dimension. For a topology of one dimensions with 100 inputs
use [100]. For a two dimensional topology of 10x5
use [10,5].
Argument columnDimensions A list of integers representing the
dimensions of the columns in the region. Format is [height,
width, depth, ...], where each value represents the size of
the dimension. For a topology of one dimensions with 2000
columns use 2000, or [2000]. For a three dimensional
topology of 32x64x16 use [32, 64, 16].
Argument potentialRadius This parameter determines the extent of the
input that each column can potentially be connected to. This
can be thought of as the input bits that are visible to each
column, or a 'receptive field' of the field of vision. A large
enough value will result in global coverage, meaning
that each column can potentially be connected to every input
bit. This parameter defines a square (or hyper square) area: a
column will have a max square potential pool with sides of
length (2 * potentialRadius + 1).
Argument potentialPct The percent of the inputs, within a column's
potential radius, that a column can be connected to. If set to
1, the column will be connected to every input within its
potential radius. This parameter is used to give each column a
unique potential pool when a large potentialRadius causes
overlap between the columns. At initialization time we choose
((2*potentialRadius + 1)^(# inputDimensions) * potentialPct)
input bits to comprise the column's potential pool.
Argument globalInhibition If true, then during inhibition phase the
winning columns are selected as the most active columns from the
region as a whole. Otherwise, the winning columns are selected
with respect to their local neighborhoods. Global inhibition
boosts performance significantly but there is no topology at the
output.
Argument localAreaDensity The desired density of active columns within
a local inhibition area (the size of which is set by the
internally calculated inhibitionRadius, which is in turn
determined from the average size of the connected potential
pools of all columns). The inhibition logic will insure that at
most N columns remain ON within a local inhibition area, where
N = localAreaDensity * (total number of columns in inhibition
area).
If localAreaDensity is set to 0,
output sparsity will be determined by the numActivePerInhArea.
Argument numActiveColumnsPerInhArea An alternate way to control the sparsity of
active columns. When numActivePerInhArea > 0, the inhibition logic will insure that
at most 'numActivePerInhArea' columns remain ON within a local
inhibition area (the size of which is set by the internally
calculated inhibitionRadius). When using this method, as columns
learn and grow their effective receptive fields, the
inhibitionRadius will grow, and hence the net density of the
active columns will *decrease*. This is in contrast to the
localAreaDensity method, which keeps the density of active
columns the same regardless of the size of their receptive
fields.
If numActivePerInhArea is specified then it overrides localAreaDensity..
Argument stimulusThreshold This is a number specifying the minimum
number of synapses that must be active in order for a column to
turn ON. The purpose of this is to prevent noisy input from
activating columns.
Argument synPermInactiveDec The amount by which the permanence of an
inactive synapse is decremented in each learning step.
Argument synPermActiveInc The amount by which the permanence of an
active synapse is incremented in each round.
Argument synPermConnected The default connected threshold. Any synapse
whose permanence value is above the connected threshold is
a "connected synapse", meaning it can contribute to
the cell's firing.
Argument minPctOverlapDutyCycle A number between 0 and 1.0, used to set
a floor on how often a column should have at least
stimulusThreshold active inputs. Periodically, each column looks
at the overlap duty cycle of all other column within its
inhibition radius and sets its own internal minimal acceptable
duty cycle to: minPctDutyCycleBeforeInh * max(other columns'
duty cycles). On each iteration, any column whose overlap duty
cycle falls below this computed value will get all of its
permanence values boosted up by synPermActiveInc. Raising all
permanences in response to a sub-par duty cycle before
inhibition allows a cell to search for new inputs when either
its previously learned inputs are no longer ever active, or when
the vast majority of them have been "hijacked" by other columns.
Argument dutyCyclePeriod The period used to calculate duty cycles.
Higher values make it take longer to respond to changes in
boost. Shorter values make it potentially more unstable and
likely to oscillate.
Argument boostStrength A number greater or equal than 0, used to
control boosting strength. No boosting is applied if it is set to 0.
The strength of boosting increases as a function of boostStrength.
Boosting encourages columns to have similar activeDutyCycles as their
neighbors, which will lead to more efficient use of columns. However,
too much boosting may also lead to instability of SP outputs.
Argument seed Seed for our random number generator. If seed is < 0
a randomly generated seed is used. The behavior of the spatial
pooler is deterministic once the seed is set.
Argument spVerbosity spVerbosity level: 0, 1, 2, or 3
Argument wrapAround boolean value that determines whether or not inputs
at the beginning and end of an input dimension are considered
neighbors for the purpose of mapping inputs to columns.
)"
, py::arg("inputDimensions") = vector<UInt>({ 32, 32 })
, py::arg("columnDimensions") = vector<UInt>({ 64, 64 })
, py::arg("potentialRadius") = 16
, py::arg("potentialPct") = 0.5
, py::arg("globalInhibition") = false
, py::arg("localAreaDensity") = 0.02f
, py::arg("numActiveColumnsPerInhArea") = 0
, py::arg("stimulusThreshold") = 0
, py::arg("synPermInactiveDec") = 0.01
, py::arg("synPermActiveInc") = 0.1
, py::arg("synPermConnected") = 0.1
, py::arg("minPctOverlapDutyCycle") = 0.001
, py::arg("dutyCyclePeriod") = 1000
, py::arg("boostStrength") = 0.0
, py::arg("seed") = 1
, py::arg("spVerbosity") = 0
, py::arg("wrapAround") = true
);
py_SpatialPooler.def("getColumnDimensions", &SpatialPooler::getColumnDimensions);
py_SpatialPooler.def("getInputDimensions", &SpatialPooler::getInputDimensions);
py_SpatialPooler.def("getNumColumns", &SpatialPooler::getNumColumns);
py_SpatialPooler.def("getNumInputs", &SpatialPooler::getNumInputs);
py_SpatialPooler.def("getPotentialRadius", &SpatialPooler::getPotentialRadius);
py_SpatialPooler.def("setPotentialRadius", &SpatialPooler::setPotentialRadius);
py_SpatialPooler.def("getPotentialPct", &SpatialPooler::getPotentialPct);
py_SpatialPooler.def("setPotentialPct", &SpatialPooler::setPotentialPct);
py_SpatialPooler.def("getGlobalInhibition", &SpatialPooler::getGlobalInhibition);
py_SpatialPooler.def("setGlobalInhibition", &SpatialPooler::setGlobalInhibition);
py_SpatialPooler.def("getNumActiveColumnsPerInhArea", &SpatialPooler::getNumActiveColumnsPerInhArea);
py_SpatialPooler.def("setNumActiveColumnsPerInhArea", &SpatialPooler::setNumActiveColumnsPerInhArea);
py_SpatialPooler.def("getLocalAreaDensity", &SpatialPooler::getLocalAreaDensity);
py_SpatialPooler.def("setLocalAreaDensity", &SpatialPooler::setLocalAreaDensity);
py_SpatialPooler.def("getStimulusThreshold", &SpatialPooler::getStimulusThreshold);
py_SpatialPooler.def("setStimulusThreshold", &SpatialPooler::setStimulusThreshold);
py_SpatialPooler.def("getInhibitionRadius", &SpatialPooler::getInhibitionRadius);
py_SpatialPooler.def("setInhibitionRadius", &SpatialPooler::setInhibitionRadius);
py_SpatialPooler.def("getDutyCyclePeriod", &SpatialPooler::getDutyCyclePeriod);
py_SpatialPooler.def("setDutyCyclePeriod", &SpatialPooler::setDutyCyclePeriod);
py_SpatialPooler.def("getBoostStrength", &SpatialPooler::getBoostStrength);
py_SpatialPooler.def("setBoostStrength", &SpatialPooler::setBoostStrength);
py_SpatialPooler.def("getIterationNum", &SpatialPooler::getIterationNum);
py_SpatialPooler.def("setIterationNum", &SpatialPooler::setIterationNum);
py_SpatialPooler.def("getIterationLearnNum", &SpatialPooler::getIterationLearnNum);
py_SpatialPooler.def("setIterationLearnNum", &SpatialPooler::setIterationLearnNum);
py_SpatialPooler.def("getSpVerbosity", &SpatialPooler::getSpVerbosity);
py_SpatialPooler.def("setSpVerbosity", &SpatialPooler::setSpVerbosity);
py_SpatialPooler.def("getWrapAround", &SpatialPooler::getWrapAround);
py_SpatialPooler.def("setWrapAround", &SpatialPooler::setWrapAround);
py_SpatialPooler.def("getUpdatePeriod", &SpatialPooler::getUpdatePeriod);
py_SpatialPooler.def("setUpdatePeriod", &SpatialPooler::setUpdatePeriod);
py_SpatialPooler.def("getSynPermActiveInc", &SpatialPooler::getSynPermActiveInc);
py_SpatialPooler.def("setSynPermActiveInc", &SpatialPooler::setSynPermActiveInc);
py_SpatialPooler.def("getSynPermInactiveDec", &SpatialPooler::getSynPermInactiveDec);
py_SpatialPooler.def("setSynPermInactiveDec", &SpatialPooler::setSynPermInactiveDec);
py_SpatialPooler.def("getSynPermBelowStimulusInc", &SpatialPooler::getSynPermBelowStimulusInc);
py_SpatialPooler.def("setSynPermBelowStimulusInc", &SpatialPooler::setSynPermBelowStimulusInc);
py_SpatialPooler.def("getSynPermConnected", &SpatialPooler::getSynPermConnected);
py_SpatialPooler.def("getSynPermMax", &SpatialPooler::getSynPermMax);
py_SpatialPooler.def("getMinPctOverlapDutyCycles", &SpatialPooler::getMinPctOverlapDutyCycles);
py_SpatialPooler.def("setMinPctOverlapDutyCycles", &SpatialPooler::setMinPctOverlapDutyCycles);
// saving and loading from file
py_SpatialPooler.def("saveToFile",
static_cast<void (htm::SpatialPooler::*)(std::string, std::string) const>(&htm::SpatialPooler::saveToFile),
py::arg("file"), py::arg("fmt") = "BINARY",
R"(Serializes object to file. file: filename to write to. fmt: format, one of 'BINARY', 'PORTABLE', 'JSON', or 'XML')");
py_SpatialPooler.def("loadFromFile",
static_cast<void (htm::SpatialPooler::*)(std::string, std::string)>(&htm::SpatialPooler::loadFromFile),
py::arg("file"), py::arg("fmt") = "BINARY",
R"(Deserializes object from file. file: filename to read from. fmt: format recorded by saveToFile(). )");
// loadFromString, loads SP from a JSON encoded string produced by writeToString().
py_SpatialPooler.def("loadFromString", [](SpatialPooler& self, const std::string& inString)
{
std::stringstream inStream(inString);
self.load(inStream, JSON);
},
R"(See also standard library function: pickle.loads(...))");
// writeToString, save SP to a JSON encoded string usable by loadFromString()
py_SpatialPooler.def("writeToString", [](const SpatialPooler& self)
{
std::ostringstream os;
os.precision(std::numeric_limits<double>::digits10 + 1);
os.precision(std::numeric_limits<float>::digits10 + 1);
self.save(os, JSON);
return os.str();
},
R"(See also standard library function: pickle.dumps(...))");
// compute
py_SpatialPooler.def("compute", [](SpatialPooler& self, const SDR& input, const bool learn, SDR& output)
{
const auto& overlaps = self.compute( input, learn, output );
return py::array_t<SynapseIdx>( overlaps.size(), overlaps.data());
},
R"(
This is the main workhorse method of the SpatialPooler class. This method
takes an input SDR and computes the set of output active columns. If 'learn' is
set to True, this method also performs learning.
Argument input An SDR that comprises the input to the spatial pooler. The size
of the SDR must match total number of input bits implied by the
constructor (also returned by the method getNumInputs).
Argument learn A boolean value indicating whether learning should be
performed. Learning entails updating the permanence values of
the synapses, duty cycles, etc. Learning is typically on but
setting learning to 'off' is useful for analyzing the current
state of the SP. For example, you might want to feed in various
inputs and examine the resulting SDR's. Note that if learning
is off, boosting is turned off and columns that have never won
will be removed from activeVector. TODO: we may want to keep
boosting on even when learning is off.
Argument output An SDR representing the winning columns after
inhibition. The size of the SDR is equal to the number of
columns (also returned by the method getNumColumns).
)",
py::arg("input"),
py::arg("learn") = true,
py::arg("output")
);
// setBoostFactors
py_SpatialPooler.def("setBoostFactors", [](SpatialPooler& self, py::array& x)
{
self.setBoostFactors(get_it<Real>(x));
});
// getBoostFactors
py_SpatialPooler.def("getBoostFactors", [](const SpatialPooler& self, py::array& x)
{
self.getBoostFactors(get_it<Real>(x));
});
// setOverlapDutyCycles
py_SpatialPooler.def("setOverlapDutyCycles", [](SpatialPooler& self, py::array& x)
{
self.setOverlapDutyCycles(get_it<Real>(x));
});
// getOverlapDutyCycles
py_SpatialPooler.def("getOverlapDutyCycles", [](const SpatialPooler& self, py::array& x)
{
self.getOverlapDutyCycles(get_it<Real>(x));
});
// setActiveDutyCycles
py_SpatialPooler.def("setActiveDutyCycles", [](SpatialPooler& self, py::array& x)
{
self.setActiveDutyCycles(get_it<Real>(x));
});
// getActiveDutyCycles
py_SpatialPooler.def("getActiveDutyCycles", [](const SpatialPooler& self, py::array& x)
{
self.getActiveDutyCycles(get_it<Real>(x));
});
// setMinOverlapDutyCycles
py_SpatialPooler.def("setMinOverlapDutyCycles", [](SpatialPooler& self, py::array& x)
{
self.setMinOverlapDutyCycles(get_it<Real>(x));
});
// getMinOverlapDutyCycles
py_SpatialPooler.def("getMinOverlapDutyCycles", [](const SpatialPooler& self, py::array& x)
{
self.getMinOverlapDutyCycles(get_it<Real>(x));
});
// setPotential
py_SpatialPooler.def("setPotential", [](SpatialPooler& self, UInt column, py::array& x)
{
self.setPotential(column, get_it<UInt>(x));
});
// getPotential
py_SpatialPooler.def("getPotential", [](const SpatialPooler& self, UInt column, py::array& x)
{
self.getPotential(column, get_it<UInt>(x));
});
// setPermanence
py_SpatialPooler.def("setPermanence", [](SpatialPooler& self, UInt column, py::array& x)
{
self.setPermanence(column, get_it<Real>(x));
});
// getPermanence
py_SpatialPooler.def("getPermanence", [](const SpatialPooler& self, const UInt column, py::array& x, const Permanence threshold)
{
const auto& perm = self.getPermanence(column, threshold);
std::copy(perm.begin(), perm.end(), get_it<Real>(x)); //TODO pass-by-value here only for compatibility, could have just returned perm
return perm;
},
"",
py::arg("column"),
py::arg("x"),
py::arg("threshold") = 0.0);
// getConnectedCounts
py_SpatialPooler.def("getConnectedCounts", [](const SpatialPooler& self, py::array& x)
{
self.getConnectedCounts(get_it<UInt>(x));
});
// getBoostedOverlaps
py_SpatialPooler.def("getBoostedOverlaps", [](SpatialPooler& self)
{
auto overlaps = self.getBoostedOverlaps();
return py::array_t<Real>( overlaps.size(), overlaps.data());
});
////////////////////
// inhibitColumns
auto inhibitColumns_func = [](SpatialPooler& self, py::array& overlaps)
{
std::vector<htm::Real> overlapsVector(get_it<Real>(overlaps), get_end<Real>(overlaps));
// converts from a vector of Real to a vector of UInt
std::vector<htm::UInt> activeColumnsVector = self.inhibitColumns_(overlapsVector);
return py::array_t<UInt>( activeColumnsVector.size(), activeColumnsVector.data());
};
py_SpatialPooler.def("_inhibitColumns", inhibitColumns_func);
py_SpatialPooler.def("inhibitColumns_", inhibitColumns_func);
//////////////////////
// getIterationLearnNum
py_SpatialPooler.def("getIterationLearnNum", &SpatialPooler::getIterationLearnNum);
py_SpatialPooler.def("__str__",
[](SpatialPooler &self) {
std::stringstream buf;
buf << self;
return buf.str(); });
py_SpatialPooler.def_property_readonly("connections", &SpatialPooler::getConnections,
R"(Internal Connections object.
This attribute is READ ONLY. It returns a copy of the
Connections object and changes to it are discarded.
Warning: The Connections class API is subject to change.)");
// pickle
py_SpatialPooler.def(py::pickle(
[](const SpatialPooler& sp) // __getstate__
{
std::stringstream ss;
sp.save(ss);
/* The values in stringstream are binary so pickle will get confused
* trying to treat it as utf8 if you just return ss.str().
* So we must treat it as py::bytes. Some characters could be null values.
*/
return py::bytes( ss.str() );
},
[](py::bytes &s) // __setstate__
{
/* pybind11 will pass in the bytes array without conversion.
* so we should be able to just create a string to initalize the stringstream.
*/
std::stringstream ss( s.cast<std::string>() );
std::unique_ptr<SpatialPooler> sp(new SpatialPooler());
sp->load(ss);
/*
* The __setstate__ part of the py::pickle() is actually a py::init() with some options.
* So the return value can be the object returned by value, by pointer,
* or by container (meaning a unique_ptr). SP has a problem with the copy constructor
* and pointers have problems knowing who the owner is so lets use unique_ptr.
* See: https://pybind11.readthedocs.io/en/stable/advanced/classes.html#custom-constructors
*/
return sp;
}));
}
} // namespace htm_ext