In [1]:
import htmsanity.nupic.stackable as stackable
stackable.init_notebook_mode()

Simple hotgym SP + TM

This is a simple hotgym experiment. I've added a "logger". Look at the lines below each "ADD THIS".

In [2]:
import csv
import datetime
import os

from pkg_resources import resource_filename

import numpy as np
from nupic.bindings.algorithms import SpatialPooler, TemporalMemory
from nupic.encoders.date import DateEncoder
from nupic.encoders.random_distributed_scalar import (
    RandomDistributedScalarEncoder)

def runHotgym():
    #
    # ADD THIS
    #
    logger = stackable.TimeSeriesLogger() 
    
    valueEncoder = RandomDistributedScalarEncoder(resolution=0.88, seed=42)
    timestampEncoder = DateEncoder(timeOfDay=(21, 9.49, ))

    inputWidth = timestampEncoder.getWidth() + valueEncoder.getWidth()

    sp = SpatialPooler(**{
        "globalInhibition": True,
        "columnDimensions": [2048],
        "inputDimensions": [inputWidth],
        "potentialRadius": inputWidth,
        "numActiveColumnsPerInhArea": 40,
        "seed": 1956,
        "potentialPct": 0.8,
        "boostStrength": 0.0,
        "synPermActiveInc": 0.003,
        "synPermConnected": 0.2,
        "synPermInactiveDec": 0.0005,
    })

    tm = TemporalMemory(**{
        "activationThreshold": 20,
        "cellsPerColumn": 32,
        "columnDimensions": (2048,),
        "initialPermanence": 0.24,
        "maxSegmentsPerCell": 128,
        "maxSynapsesPerSegment": 128,
        "minThreshold": 13,
        "maxNewSynapseCount": 31,
        "permanenceDecrement": 0.008,
        "permanenceIncrement": 0.04,
        "seed": 1961,
    })

    #
    # ADD THIS
    #
    logger.startLoggingSegmentGrowth(tm.connections)

    inputPath = resource_filename(
        "nupic.datafiles", "extra/hotgym/rec-center-hourly.csv"
    )

    with open(inputPath, "rb") as inputFile:
        csvReader = csv.reader(inputFile)
        csvReader.next()
        csvReader.next()
        csvReader.next()

        encodedValue = np.zeros(valueEncoder.getWidth(), dtype=np.uint32)
        encodedTimestamp = np.zeros(timestampEncoder.getWidth(), dtype=np.uint32)
        spOutput = np.zeros(2048, dtype=np.float32)

        timestep = 0

        for timestampStr, consumptionStr in csvReader:

            if timestep % 500 == 0:
                print "t", timestep

            #
            # ADD THIS
            #
            logger.logTimestep()

            timestamp = datetime.datetime.strptime(timestampStr, "%m/%d/%y %H:%M")
            consumption = float(consumptionStr)

            timestampEncoder.encodeIntoArray(timestamp, encodedTimestamp)
            valueEncoder.encodeIntoArray(consumption, encodedValue)

            sensoryInput = np.concatenate((encodedTimestamp, encodedValue,))
            sp.compute(sensoryInput, True, spOutput)

            activeColumns = spOutput.nonzero()[0]

            #
            # ADD THIS
            # (Call this when the TM contains predictions for *this* timestep.)
            #
            logger.logColumnActivity(tm, activeColumns)
            logger.logSegmentActivity(tm, activeColumns)

            tm.compute(activeColumns)

            timestep += 1
            
    return logger
In [3]:
logger = runHotgym()
t 0
t 500
t 1000
t 1500
t 2000
t 2500
t 3000
t 3500
t 4000
In [4]:
stackable.insertColumnStatesAndSegmentLifetimes(logger.extract())

Record an external experiment

You don't have to move your experiment to a notebook to get these visualizations. Just use this logger from your experiment code, and specify an output file. Don't use logger.extract() if you're logging to a file.

with open("out.csv", "w") as fileOut:
    logger = stackable.TimeSeriesLogger(fileOut)

    # Continue the experiment.
In [9]:
with open("sequence_classification.csv", "r") as fileIn:
    stackable.insertColumnStatesAndSegmentLifetimes(fileIn.read())