Example #1
0
    def LoadDistalSynapses(self, layer, column, cell, iteration):
        timeStartDistalSynapsesCalc = time.time()
        tm = TemporalMemory()
        tm.loadFromFile(
            os.path.join(
                os.path.splitext(self.databaseFilePath)[0] + "_distalDump",
                "" + str(layer) + "_" + str(iteration) + ".dump"))

        reqCellID = int(column *
                        self.layers[layer].params['tm_cellsPerColumn'] + cell)

        print("requesting distals for cell:" + str(reqCellID))
        segments = self.getPresynapticCellsForCell(tm, reqCellID)

        segNo = 0
        for seg in segments:  # for each segment
            if cell not in self.layers[layer].distalSynapses.keys():
                self.layers[layer].distalSynapses[cell] = {}
            self.layers[layer].distalSynapses[cell][
                segNo] = seg  # add numpy array to the dict
            segNo += 1

        return len(segments) > 0  # true if we got something for this cell
Example #2
0
class Layer:
    def __init__(self,
                 din=(10, 10),
                 dout=(10, 10),
                 temporal=True,
                 setting=param.default_parameters):
        self.input_shape = din
        self.output_shape = dout
        self.temporal = temporal
        self.learn = True
        self.setting = AttrDict(setting)
        self.sp = SpatialPooler()
        self.tm = TemporalMemory() if temporal else None

    def compile(self):
        spParams = self.setting("sp")
        self.sp = SpatialPooler(
            inputDimensions=self.input_shape,
            columnDimensions=self.output_shape,
            potentialPct=spParams.potentialPct,
            potentialRadius=spParams.potentialRadius,
            globalInhibition=True if len(self.output_shape) == 1 else False,
            localAreaDensity=spParams.localAreaDensity,
            synPermInactiveDec=spParams.synPermInactiveDec,
            synPermActiveInc=spParams.synPermActiveInc,
            synPermConnected=spParams.synPermConnected,
            boostStrength=spParams.boostStrength,
            wrapAround=True,
        )
        if self.temporal:
            tmParams = self.setting("tm")
            self.tm = TemporalMemory(
                columnDimensions=self.output_shape,
                cellsPerColumn=tmParams.cellsPerColumn,
                activationThreshold=tmParams.activationThreshold,
                initialPermanence=tmParams.initialPerm,
                connectedPermanence=spParams.synPermConnected,
                minThreshold=tmParams.minThreshold,
                maxNewSynapseCount=tmParams.newSynapseCount,
                permanenceIncrement=tmParams.permanenceInc,
                permanenceDecrement=tmParams.permanenceDec,
                predictedSegmentDecrement=0.0,
                maxSegmentsPerCell=tmParams.maxSegmentsPerCell,
                maxSynapsesPerSegment=tmParams.maxSynapsesPerSegment)

    def forward(self, encoding):
        activeColumns = SDR(self.sp.getColumnDimensions())
        self.sp.compute(encoding, self.learn, activeColumns)

        predictedColumns = None
        if self.temporal:
            self.tm.compute(activeColumns, self.learn)
            self.tm.activateDendrites(self.learn)
            predictedColumnIndices = {
                self.tm.columnForCell(i)
                for i in self.tm.getPredictiveCells().sparse
            }
            predictedColumns = SDR(self.sp.getColumnDimensions())
            predictedColumns.sparse = list(predictedColumnIndices)
        return activeColumns, predictedColumns

    def train(self):
        self.learn = True

    def eval(self):
        self.learn = False

    def anomaly(self):
        return float(self.tm.anomaly) if self.temporal else None

    def reset(self):
        if self.temporal:
            self.tm.reset()

    def save(self, path):
        print('Saving Model...')
        print(str(self.sp))

        self.sp.saveToFile(param.sp_model.format(path))
        if self.temporal:
            print(str(self.tm))
            self.tm.saveToFile(param.tm_model.format(path))

    def load(self, path):
        print('Loading Model...')
        self.sp.loadFromFile(param.sp_model.format(path))
        print(str(self.sp))
        if self.temporal:
            self.tm.loadFromFile(param.tm_model.format(path))
            print(str(self.tm))