@mraptor

Here is the python version of a class named QuickTest.py. It demonstrates
the connection of all the major algorithms (except Anomaly). This was
written about a year ago, so forgive me if there are one or two tweaks
necessary to make it compatible with the current NuPIC code? (Chances are
that it is still compatible, but just in case - be forewarned).

Maybe some enterprising Numenta engineer can take a look and make sure that
the code is still relevant (then share the results again with the
community)? This file seems to come in handy for new-comers and so may be
useful as a tool to share?

(see attached)

Cheers,
David

On Mon, Feb 22, 2016 at 10:29 PM, mraptor <[email protected]> wrote:

> that will be nice, if it is not too much of a hassle .
> -------| http://ifni.co
>
>
> On Mon, Feb 22, 2016 at 11:50 AM, cogmission (David Ray)
> <[email protected]> wrote:
> > If it helps, I also have an example Python file that shows the usage of
> each
> > of the typical algorithms used in standalone fashion - in one quick easy
> > file with an actual use-case? I can email it to you if you'd like?
> >
> > Cheers,
> > David
> >
> > On Mon, Feb 22, 2016 at 10:24 AM, Matthew Taylor <[email protected]>
> wrote:
> >>
> >> Have you seen Scott's "Beginner's Guide"?
> >>
> https://github.com/numenta/nupic/wiki/Using-NuPIC#beginners-guide-to-nupic
> >>
> >> He instantiates the ScalarEncoder directly and uses it in a standalone
> >> fashion. There is a link to the code too. Does that help?
> >> ---------
> >> Matt Taylor
> >> OS Community Flag-Bearer
> >> Numenta
> >>
> >>
> >> On Sat, Feb 20, 2016 at 10:36 PM, mraptor <[email protected]> wrote:
> >> > thanks,
> >> >
> >> > I was talking mostly of standalone use i.e. separating it in its own
> >> > class w/o any non-standard python dependencies
> >> > -------| http://ifni.co
> >> >
> >> >
> >> > On Sat, Feb 20, 2016 at 9:28 PM, Kentaro Iizuka
> >> > <[email protected]> wrote:
> >> >> Hello mraptor,
> >> >>
> >> >> Yes!
> >> >> You can use NuPIC's ScalarEncoder as Python class.
> >> >> NuPIC’s unit test code of ScalarEncoder may helpful for you.
> >> >>
> >> >>
> https://github.com/numenta/nupic/blob/master/tests/unit/nupic/encoders/scalar_test.py
> >> >>
> >> >> Here is my small sample code using ScalarEncoder.
> >> >> https://gist.github.com/iizukak/2c77e9e4ee8a842e86b7
> >> >>
> >> >> Best regards.
> >> >>
> >> >> 2016-02-21 5:01 GMT+09:00 mraptor <[email protected]>:
> >> >>> hi,
> >> >>>
> >> >>> Is there a easy way to separate Scalar encoder as a standalone
> class.
> >> >>> I only need init, encode/decode functionality.
> >> >>>
> >> >>>
> >> >>>
> https://github.com/numenta/nupic/blob/master/src/nupic/encoders/scalar.py
> >> >>>
> >> >>> I don't want to reinvent the wheel :)
> >> >>>
> >> >>> thanks
> >> >>>
> >> >>> -------| http://ifni.co
> >> >>>
> >> >>
> >> >>
> >> >>
> >> >> --
> >> >> Kentaro Iizuka<[email protected]>
> >> >>
> >> >> Github
> >> >> https://github.com/iizukak/
> >> >>
> >> >
> >>
> >
> >
> >
> > --
> > With kind regards,
> >
> > David Ray
> > Java Solutions Architect
> >
> > Cortical.io
> > Sponsor of:  HTM.java
> >
> > [email protected]
> > http://cortical.io
>
>


-- 
*With kind regards,*

David Ray
Java Solutions Architect

*Cortical.io <http://cortical.io/>*
Sponsor of:  HTM.java <https://github.com/numenta/htm.java>

[email protected]
http://cortical.io
'''
Created on Feb 8, 2015

@author: David Ray
'''

import numpy as np

from nupic.encoders.scalar import ScalarEncoder as ScalarEncoder
from nupic.algorithms.CLAClassifier import CLAClassifier as CLAClassifier
from nupic.research.spatial_pooler import SpatialPooler as SpatialPooler
from nupic.research.temporal_memory import TemporalMemory as TemporalMemory


class Layer():
    
    """ Makeshift Layer to contain and operate on algorithmic entities """
    
    def __init__(self, encoder, sp, tm, classifier):
        
        self.encoder = encoder
        self.sp = sp
        self.tm = tm
        self.classifier = classifier
        self.theNum = 0
       
        
    def input(self, value, recordNum, sequenceNum):
        """ Feed the incremented input into the Layer components """
         
        if recordNum == 1:
            recordOut = "Monday (1)"
        elif recordNum == 2:
            recordOut = "Tuesday (2)"
        elif recordNum == 3:
            recordOut = "Wednesday (3)"
        elif recordNum == 4:
            recordOut = "Thursday (4)"
        elif recordNum == 5:
            recordOut = "Friday (5)"
        elif recordNum == 6:
            recordOut = "Saturday (6)"
        else: recordOut = "Sunday (7)"
        
        if recordNum == 1:
            self.theNum += 1
            if self.theNum == 100:
                print "bl"
            
            print "--------------------------------------------------------"
            print "Iteration: " + str(self.theNum)
            
        print "===== " + str(recordOut) + " - Sequence Num: " + str(sequenceNum) + " ====="
        
        output = np.zeros(sp._columnDimensions)
        
        # Input through encoder
        print "ScalarEncoder Input = " + str(value)
        encoding = encoder.encode(value)
        print "ScalarEncoder Output = " + str(encoding)
        bucketIdx = encoder.getBucketIndices(value)[0]
        
        # Input through spatial pooler
        sp.compute(encoding, True, output)
        print "SpatialPooler Output = " + str(np.where(output > 0)[0])
        
        # Input through temporal memory
        input = set(sorted(np.where(output > 0)[0].flat))
        print "input = " + str(input)
        tm.compute(input, True)        
        predictiveCells = tm.activeCells #getSDR(tm.predictiveCells)
        print "TemporalMemory Input = " + str(input)
        
        # Input into classifier
        retVal = classifier.compute(recordNum=0,
            patternNZ=predictiveCells,
            classification= {'bucketIdx': bucketIdx, 'actValue':value},
            learn=True,
            infer=True
        )
        
        print "TemporalMemory Prediction = " + str(getSDR(predictiveCells)) +\
        "  |  CLAClassifier 1 step prob = " + str(retVal[1]) 
        print ""
        

def getSDR(cells):
    retVal = set()
    for cell in cells:
        retVal.add(cell / tm.cellsPerColumn)
    return retVal     
        
def runThroughLayer(layer, recordNum, sequenceNum):
    
    layer.input(recordNum, recordNum, sequenceNum)        
        
        
if __name__ == '__main__':
    encoder = ScalarEncoder(
        n = 8,
        w = 3,
        radius = 0,
        minval = 1,
        maxval = 8,
        periodic = True,
        forced = True,
        resolution = 0
    )
    
    sp = SpatialPooler(
        inputDimensions = (8),
        columnDimensions = (20),
        potentialRadius = 12,
        potentialPct = 0.5,
        globalInhibition = True,
        localAreaDensity = -1.0,
        numActiveColumnsPerInhArea = 5.0,
        stimulusThreshold = 1.0,
        synPermInactiveDec = 0.0005,
        synPermActiveInc = 0.0015,
        synPermConnected = 0.1,
        minPctOverlapDutyCycle = 0.1,
        minPctActiveDutyCycle = 0.1,
        dutyCyclePeriod = 10, 
        maxBoost = 10.0,
        seed = 42,
        spVerbosity = 0
    )
    
    tm = TemporalMemory(
        columnDimensions = (256,),
        cellsPerColumn = (6),
        initialPermanence = 0.2,
        connectedPermanence = 0.8,
        minThreshold = 5,
        maxNewSynapseCount = 6,
        permanenceDecrement = 0.1,
        permanenceIncrement = 0.1,
        activationThreshold = 4
    )
    
    classifier = CLAClassifier(
        steps = [1],
        alpha = 0.1,
        actValueAlpha = 0.3,
        verbosity = 0
    )
    
    sp.printParameters()
    print ""
    
    layer = Layer(encoder, sp, tm, classifier)
    
    i = 1
    for x in range(2000):
        if i == 1:
            tm.reset()
            
        runThroughLayer(layer, i, x)
        i = 1 if i == 7 else i + 1
        
        
        
        
    

        
        
        
        

Reply via email to