Exemplo n.º 1
0
  def testGetMethods(self):
    """check if the get-Methodes return correct values"""
    # first create instance of Tm with some parameters
    tm = TM(
        columnDimensions=(parameters1["sp"]["columnCount"],),
        cellsPerColumn=parameters1["tm"]["cellsPerColumn"],
        activationThreshold=parameters1["tm"]["activationThreshold"],
        initialPermanence=parameters1["tm"]["initialPerm"],
        connectedPermanence=parameters1["sp"]["synPermConnected"],
        minThreshold=parameters1["tm"]["minThreshold"],
        maxNewSynapseCount=parameters1["tm"]["newSynapseCount"],
        permanenceIncrement=parameters1["tm"]["permanenceInc"],
        permanenceDecrement=parameters1["tm"]["permanenceDec"],
        predictedSegmentDecrement=0.0,
        maxSegmentsPerCell=parameters1["tm"]["maxSegmentsPerCell"],
        maxSynapsesPerSegment=parameters1["tm"]["maxSynapsesPerSegment"],
        checkInputs = True
    )

    # second call each function to get the values
    columnDimension = tm.getColumnDimensions()
    cellsPerColumn = tm.getCellsPerColumn()
    activationThreshold = tm.getActivationThreshold()
    initialPermanence = tm.getInitialPermanence()
    connectedPermanence = tm.getConnectedPermanence()
    minThreshold = tm.getMinThreshold()
    maxNewSynapseCount = tm.getMaxNewSynapseCount()
    permanenceIncrement = tm.getPermanenceIncrement()
    permanenceDecrement = tm.getPermanenceDecrement()
    predictedSegmentDecrement = tm.getPredictedSegmentDecrement()
    maxSegmentsPerCell = tm.getMaxSegmentsPerCell()
    maxSynapsesPerSegment = tm.getMaxSynapsesPerSegment()
    checkInputs = tm.getCheckInputs()

    # third and final, compare the input parameters with the parameters from the get-Methods
    # floating point numbers maybe not 100 % equal...
    self.assertEqual([parameters1["sp"]["columnCount"]], columnDimension, "using method (getColumnDimension) failed")
    self.assertEqual(parameters1["tm"]["cellsPerColumn"], cellsPerColumn, "using method (getCellsPerColumn) failed")
    self.assertEqual(parameters1["tm"]["activationThreshold"], activationThreshold, "using (getActivationThreshold) failed")
    self.assertAlmostEqual(parameters1["sp"]["synPermConnected"], connectedPermanence, msg="using method (getConnectedPermanence) failed")
    self.assertEqual(parameters1["tm"]["minThreshold"], minThreshold, "using method (getMinThreshold) failed")
    self.assertEqual(parameters1["tm"]["newSynapseCount"], maxNewSynapseCount, "using method (getMaxNewSynapseCount) failed")
    self.assertAlmostEqual(parameters1["tm"]["permanenceInc"], permanenceIncrement, msg="using method (getPermanenceIncrement) failed")
    self.assertAlmostEqual(parameters1["tm"]["permanenceDec"], permanenceDecrement, msg="using method (getPermanenceDecrement) failed")
    self.assertAlmostEqual(0.0, predictedSegmentDecrement, msg="using Methode (getPredictedSegmentDecrement) failed")
    self.assertEqual(parameters1["tm"]["maxSegmentsPerCell"], maxSegmentsPerCell, "using Method (getMaxSegmentsPerCell) failed")
    self.assertEqual(parameters1["tm"]["maxSynapsesPerSegment"], maxSynapsesPerSegment, "using Method (getMaxSynapsesPerSegment) failed")
    self.assertEqual(True, checkInputs, "using Method (getCheckInputs) failed")
    for epoch in range(10):

        # if run without the reset, eventually get the following error
        # RuntimeError: Exception: SDRClassifier.cpp(228)
        #   message: CHECK FAILED: "recordNum >= lastRecordNum"
        #   The record number must increase monotonically.
        predict.reset()
        # need this for predictions later
        pred_actCells = []
        # anamoly_forall = []
        for count, i in enumerate(range(len(train_set))):

            # encode the current integer
            rdseSDR = rdseEncoder.encode(train_set[i])
            # create an SDR for SP output
            activeColumns = SDR( dimensions = tm.getColumnDimensions()[0] )

            # convert the SDR to SP
            # this is optional if the output from the encoder is
            # already a sparse binary representation
            # otherwise this step may be skipped as seen in
            # tutorials online
            sp.compute(rdseSDR, True, activeColumns)
            tm.compute(activeColumns, learn=True)
            tm.activateDendrites(True)
            tm_actCells = tm.getActiveCells()
            pred_actCells.append(tm_actCells)
            # anamoly_forall.append(tm.anomaly)

            label = int(train_set[i])
            # this is a neural network being trained to