I’m doing very first steps with nupic.
I installed nupic and nupic.bindings for Python27 (x86) via pip on Windows 10 x64 machine.
I wrote very simple program for testing SpatialPooler inspired by this example: http://nbviewer.jupyter.org/github/numenta/nupic/blob/master/examples/NuPIC%20Walkthrough.ipynb.
Here it is:
import sys
from optparse import OptionParser
import numpy as np
from nupic.encoders import CategoryEncoder
def importAlgorithms(research):
global SpatialPooler
if(research):
from nupic.research.spatial_pooler import SpatialPooler
else:
from nupic.bindings.algorithms import SpatialPooler
INPUT_DIMENSIONS = 15
INPUT_WIDTH = 3
COLUMNS_COUNT = 4
ACTIVE_PCT = 25
ACTIVE_COUNT = COLUMNS_COUNT * ACTIVE_PCT / 100
TRAIN_STEPS = 200
def train(sp, inputs, enc, count):
for i in inputs:
for step in xrange(count):
output = np.zeros((COLUMNS_COUNT,), dtype="int")
sp.compute(enc.encode(i), True, output)
def printConnections(headerMessage, sp):
print headerMessage
for column in xrange(COLUMNS_COUNT):
connected = np.zeros((INPUT_DIMENSIONS,), dtype="int")
sp.getConnectedSynapses(column, connected)
print "col{}\t{} ({})".format(column, connected, np.sum(connected))
def main(argv = None):
if(argv is None):
argv = sys.argv
parser = OptionParser()
parser.add_option("-r", "--research", action="store_true", dest="research", help="do not use cpp bindings", default=False)
(options, args) = parser.parse_args(argv[1:])
importAlgorithms(options.research)
inputs = ("cat", "dog", "monkey", "loris")
enc = CategoryEncoder(w=INPUT_WIDTH, categoryList=inputs, forced=True)
sp = SpatialPooler(inputDimensions = (INPUT_DIMENSIONS,),
columnDimensions = (COLUMNS_COUNT,),
potentialRadius = INPUT_DIMENSIONS,
numActiveColumnsPerInhArea = ACTIVE_COUNT,
globalInhibition = True,
synPermActiveInc = 0.03,
potentialPct = 1.0)
printConnections("Connections before training:", sp)
train(sp, inputs, enc, TRAIN_STEPS)
print
for i in inputs:
columns = np.zeros((COLUMNS_COUNT,), dtype="int")
sp.compute(enc.encode(i), False, columns)
print "{}:\t{} {}".format(i, enc.encode(i), columns)
print
printConnections("Connections after training:", sp)
return 0
if __name__ == "__main__":
main()
My problem is that when I run it with SptialPooler from numenta.research.spatic_pooler (-r
flag) I get expected result:
cat: [0 0 0 1 1 1 0 0 0 0 0 0 0 0 0] [0 1 0 0]
dog: [0 0 0 0 0 0 1 1 1 0 0 0 0 0 0] [0 0 0 1]
monkey: [0 0 0 0 0 0 0 0 0 1 1 1 0 0 0] [1 0 0 0]
loris: [0 0 0 0 0 0 0 0 0 0 0 0 1 1 1] [0 0 1 0]
Connections after training:
col0 [0 0 0 0 0 0 0 0 0 1 1 1 0 0 0] (3)
col1 [0 0 0 1 1 1 0 0 0 0 0 0 0 0 0] (3)
col2 [0 0 0 0 0 0 0 0 0 0 0 0 1 1 1] (3)
col3 [0 0 0 0 0 0 1 1 1 0 0 0 0 0 0] (3)
But without -r
option (i.e. using SptialPooler from nupic.bindings.algorithms) SP is not learning correctly and I get very wrong results:
cat: [0 0 0 1 1 1 0 0 0 0 0 0 0 0 0] [0 0 0 1]
dog: [0 0 0 0 0 0 1 1 1 0 0 0 0 0 0] [1 0 0 0]
monkey: [0 0 0 0 0 0 0 0 0 1 1 1 0 0 0] [1 0 0 0]
loris: [0 0 0 0 0 0 0 0 0 0 0 0 1 1 1] [1 0 0 0]
Connections after training:
col0 [1 1 0 0 0 1 0 0 1 1 0 0 0 1 0] (6)
col1 [1 0 1 0 1 1 1 1 0 1 1 0 0 0 1] (9)
col2 [1 1 0 1 0 1 0 0 0 0 0 0 0 0 1] (5)
col3 [0 0 0 1 1 1 1 1 0 1 1 1 0 0 0] (8)
What am I doing wrong there?