Example #1
0
    def expectNeighborhoodCoords(self, centerCoords, radius, dimensions, expected):
        centerIndex = indexFromCoordinates(centerCoords, dimensions)

        numIndices = 0

        for index, expectedIndex in zip(neighborhood(centerIndex, radius, dimensions), expected):
            numIndices += 1
            self.assertEquals(index, indexFromCoordinates(expectedIndex, dimensions))

        self.assertEquals(numIndices, len(expected))
Example #2
0
  def expectNeighborhoodCoords(self, centerCoords, radius, dimensions, expected):
    centerIndex = indexFromCoordinates(centerCoords, dimensions)

    numIndices = 0

    for index, expectedIndex in zip(neighborhood(centerIndex, radius,
                                                 dimensions),
                                    expected):
      numIndices += 1
      self.assertEqual(index, indexFromCoordinates(expectedIndex, dimensions))

    self.assertEqual(numIndices, len(expected))
def plotMovie(expName, numEpochs, killAt):
  # plot RFcenters and inputCoverage over training
  centerColumn = indexFromCoordinates((15, 15), (32, 32))
  deadCols = topology.wrappingNeighborhood(centerColumn, 5, (32, 32))

  for epoch in range(killAt, numEpochs):
    fig, axs = plt.subplots(2, 2)
    RFcenterInfo = np.load('results/RFcenters/{}/epoch_{}.npz'.format(expName, epoch))
    RFcenters = RFcenterInfo['arr_0']
    avgDistToCenter = RFcenterInfo['arr_1']
    axs[0, 1].scatter(RFcenters[:, 0], RFcenters[:, 1], s=4, c=[1,1,1])
    axs[0, 1].set_title('RF centers')
    axs[0, 1].set_xlim([-1, 33])
    axs[0, 1].set_ylim([-1, 33])
    axs[0, 1].set_aspect('equal')
    plt.axis('equal')

    # # plot input space coverage
    inputSpaceCoverage = np.load('results/InputCoverage/{}/epoch_{}.npz'.format(expName, epoch))
    inputSpaceCoverage = inputSpaceCoverage['arr_0']
    im1 = axs[0, 0].pcolor(inputSpaceCoverage, vmin=10, vmax=80)
    axs[0, 0].set_title('Input Space Coverage')
    axs[0, 0].set_xlim([-1, 33])
    axs[0, 0].set_ylim([-1, 33])
    axs[0, 0].set_aspect('equal')
    plt.axis('equal')

    boostFactors = np.load('results/boostFactors/{}/epoch_{}.npz'.format(expName, epoch))
    boostFactors = boostFactors['arr_0']
    # boostFactors[deadCols] = np.nan
    boostFactors = np.reshape(boostFactors, (32, 32))
    im2 = axs[1, 0].pcolor(boostFactors, vmin=0.5, vmax=1.5)
    axs[1, 0].set_title('Boost Factors')
    axs[1, 0].set_xlim([-1, 33])
    axs[1, 0].set_ylim([-1, 33])
    axs[1, 0].set_aspect('equal')
    plt.axis('equal')

    # fig.delaxes(axs[1, 0])
    cax = fig.add_axes([0.55, 0.4, 0.3, 0.05])
    fig.colorbar(im1, cax=cax, orientation='horizontal',
                 ticks=[0, 20, 40, 60, 80])
    cax.set_xlabel('input space coverage')

    cax = fig.add_axes([0.55, 0.2, 0.3, 0.05])
    fig.colorbar(im2, cax=cax, orientation='horizontal',
                 ticks=[0, .5, 1, 1.5, 2])
    cax.set_xlabel('boost factors')

    fig.delaxes(axs[1, 1])

    plt.savefig('figures/traumaMovie/{}_frame_{}.pdf'.format(expName, epoch))
    plt.close(fig)
Example #4
0
  def expectWrappingNeighborhoodIndices(self, centerCoords, radius, dimensions,
                                        expected):
    centerIndex = indexFromCoordinates(centerCoords, dimensions)

    numIndices = 0

    for index, expectedIndex in zip(wrappingNeighborhood(centerIndex, radius,
                                                         dimensions),
                                    expected):
      numIndices += 1
      self.assertEquals(index, expectedIndex)

    self.assertEquals(numIndices, len(expected))
Example #5
0
  def testIndexFromCoordinates(self):
    self.assertEqual(0, indexFromCoordinates((0,), (100,)))
    self.assertEqual(50, indexFromCoordinates((50,), (100,)))
    self.assertEqual(99, indexFromCoordinates((99,), (100,)))

    self.assertEqual(0, indexFromCoordinates((0, 0), (100, 80)))
    self.assertEqual(10, indexFromCoordinates((0, 10), (100, 80)))
    self.assertEqual(80, indexFromCoordinates((1, 0), (100, 80)))
    self.assertEqual(90, indexFromCoordinates((1, 10), (100, 80)))

    self.assertEqual(0, indexFromCoordinates((0, 0, 0), (100, 10, 8)))
    self.assertEqual(7, indexFromCoordinates((0, 0, 7), (100, 10, 8)))
    self.assertEqual(8, indexFromCoordinates((0, 1, 0), (100, 10, 8)))
    self.assertEqual(80, indexFromCoordinates((1, 0, 0), (100, 10, 8)))
    self.assertEqual(88, indexFromCoordinates((1, 1, 0), (100, 10, 8)))
    self.assertEqual(89, indexFromCoordinates((1, 1, 1), (100, 10, 8)))
Example #6
0
def runSPexperiments(expConfig):
    inputVectorType = expConfig.dataSet
    params = getSDRDataSetParams(expConfig.dataSet, int(expConfig.seed))
    expName = getExperimentName(expConfig)
    createDirectories(expName)

    sdrData = SDRDataSet(params)
    inputVectors = sdrData.getInputVectors()
    numInputVector, inputSize = inputVectors.shape

    plt.figure()
    plt.imshow(np.reshape(inputVectors[2], (params['nX'], params['nY'])),
               interpolation='nearest',
               cmap='gray')
    plt.savefig('figures/exampleInputs/{}'.format(expName))

    print
    print "Runnning experiment: {}".format(expName)
    print "Training Data Size {} Dimensions {}".format(numInputVector,
                                                       inputSize)

    spParams = getSpatialPoolerParams(params, expConfig)
    sp = createSpatialPooler(expConfig.spatialImp, spParams)

    if expConfig.topology == 1 and expConfig.spatialImp in ['faulty_sp', 'py']:
        initializeSPConnections(sp, potentialRaidus=10, initConnectionRadius=5)

    numColumns = np.prod(sp.getColumnDimensions())

    numTestInputs = int(numInputVector * 0.5)
    testInputs = inputVectors[:numTestInputs, :]

    connectedCounts = np.zeros((numColumns, ), dtype=uintType)
    boostFactors = np.zeros((numColumns, ), dtype=realDType)
    activeDutyCycle = np.zeros((numColumns, ), dtype=realDType)

    metrics = {
        'numConnectedSyn': [],
        'numNewSyn': [],
        'numRemoveSyn': [],
        'stability': [],
        'entropy': [],
        'maxEntropy': [],
        'sparsity': [],
        'noiseRobustness': [],
        'classification': [],
        'meanBoostFactor': [],
        'reconstructionError': [],
        'witnessError': []
    }

    connectedSyns = getConnectedSyns(sp)
    activeColumnsCurrentEpoch, dum = runSPOnBatch(sp, testInputs, learn=False)

    inspectSpatialPoolerStats(sp, inputVectors, expName + "beforeTraining")

    checkPoints = [
        0, expConfig.changeDataSetAt - 1, expConfig.changeDataSetAt,
        expConfig.numEpochs - 1
    ]

    epoch = 0
    while epoch < expConfig.numEpochs:
        print "training SP epoch {} ".format(epoch)
        if (expConfig.changeDataSetContinuously
                or epoch == expConfig.changeDataSetAt):
            params['seed'] = epoch
            sdrData.generateInputVectors(params)
            inputVectors = sdrData.getInputVectors()
            numInputVector, inputSize = inputVectors.shape
            testInputs = inputVectors[:numTestInputs, :]

        if expConfig.killInputsAfter > 0 and epoch > expConfig.killInputsAfter:
            if expConfig.topology == 1:
                inputSpaceDim = (params['nX'], params['nY'])
                centerColumn = indexFromCoordinates((15, 15), inputSpaceDim)
                deadInputs = topology.wrappingNeighborhood(
                    centerColumn, 5, inputSpaceDim)
            else:
                zombiePermutation = np.random.permutation(inputSize)
                deadInputs = zombiePermutation[:100]
            inputVectors[:, deadInputs] = 0

        if epoch == expConfig.killCellsAt:
            if expConfig.spatialImp in ['faulty_sp', 'monitored_faulty_sp']:
                if expConfig.topology == 1:
                    centerColumn = indexFromCoordinates((15, 15),
                                                        sp._columnDimensions)
                    sp.killCellRegion(centerColumn, 5)
                else:
                    sp.killCells(expConfig.killCellPrct)

        if expConfig.trackOverlapCurve:
            noiseLevelList, inputOverlapScore, outputOverlapScore = \
              calculateOverlapCurve(sp, testInputs)
            metrics['noiseRobustness'].append(
                np.trapz(np.flipud(np.mean(outputOverlapScore, 0)),
                         noiseLevelList))
            np.savez(
                './results/input_output_overlap/{}/epoch_{}'.format(
                    expName, epoch), noiseLevelList, inputOverlapScore,
                outputOverlapScore)

        if expConfig.classification:
            # classify SDRs with noise
            noiseLevelList = np.linspace(0, 1.0, 21)
            classification_accuracy = classificationAccuracyVsNoise(
                sp, testInputs, noiseLevelList)
            metrics['classification'].append(
                np.trapz(classification_accuracy, noiseLevelList))
            np.savez(
                './results/classification/{}/epoch_{}'.format(expName, epoch),
                noiseLevelList, classification_accuracy)

        # train SP here,
        # Learn is turned off at the first epoch to gather stats of untrained SP
        learn = False if epoch == 0 else True

        # randomize the presentation order of input vectors
        sdrOrders = np.random.permutation(np.arange(numInputVector))
        activeColumnsTrain, meanBoostFactors = runSPOnBatch(
            sp, inputVectors, learn, sdrOrders)
        # run SP on test dataset and compute metrics
        activeColumnsPreviousEpoch = copy.copy(activeColumnsCurrentEpoch)
        activeColumnsCurrentEpoch, dum = runSPOnBatch(sp,
                                                      testInputs,
                                                      learn=False)

        stability = calculateStability(activeColumnsCurrentEpoch,
                                       activeColumnsPreviousEpoch)
        if (expConfig.changeDataSetContinuously
                or epoch == expConfig.changeDataSetAt):
            stability = float('nan')
        metrics['stability'].append(stability)

        metrics['sparsity'].append(
            np.mean(np.mean(activeColumnsCurrentEpoch, 1)))

        metrics['entropy'].append(calculateEntropy(activeColumnsCurrentEpoch))

        # generate ideal SP outputs where all columns have the same activation prob.
        activeColumnsIdeal = np.random.rand(
            numInputVector, numColumns) > metrics['sparsity'][-1]
        metrics['maxEntropy'].append(calculateEntropy(activeColumnsIdeal))

        connectedSynsPreviousEpoch = copy.copy(connectedSyns)
        sp.getConnectedCounts(connectedCounts)
        connectedSyns = getConnectedSyns(sp)

        metrics['meanBoostFactor'].append(np.mean(meanBoostFactors))
        sp.getActiveDutyCycles(activeDutyCycle)

        metrics['numConnectedSyn'].append(np.sum(connectedCounts))

        numNewSynapses = connectedSyns - connectedSynsPreviousEpoch
        numNewSynapses[numNewSynapses < 0] = 0
        metrics['numNewSyn'].append(np.sum(numNewSynapses))

        numEliminatedSynapses = connectedSynsPreviousEpoch - connectedSyns
        numEliminatedSynapses[numEliminatedSynapses < 0] = 0
        metrics['numRemoveSyn'].append(np.sum(numEliminatedSynapses))

        metrics['reconstructionError'].append(
            reconstructionError(sp, testInputs, activeColumnsCurrentEpoch))

        metrics['witnessError'].append(
            witnessError(sp, testInputs, activeColumnsCurrentEpoch))

        print tabulate(metrics, headers="keys")

        if expConfig.checkRFCenters:
            # check distribution of RF centers, useful to monitor recovery from trauma
            RFcenters, avgDistToCenter = getRFCenters(sp,
                                                      params,
                                                      type='connected')
            if expConfig.spatialImp == 'faulty_sp':
                aliveColumns = sp.getAliveColumns()
            else:
                aliveColumns = np.arange(numColumns)
            fig = plotReceptiveFieldCenter(RFcenters[aliveColumns, :],
                                           connectedCounts[aliveColumns],
                                           (params['nX'], params['nY']))
            plt.savefig('figures/RFcenters/{}/epoch_{}.png'.format(
                expName, epoch))
            plt.close(fig)
            np.savez('results/RFcenters/{}/epoch_{}'.format(expName, epoch),
                     RFcenters, avgDistToCenter)

        if expConfig.checkInputSpaceCoverage:
            # check coverage of input space, useful to monitor recovery from trauma
            inputSpaceCoverage = calculateInputSpaceCoverage(sp)
            np.savez(
                'results/InputCoverage/{}/epoch_{}'.format(expName, epoch),
                inputSpaceCoverage, connectedCounts)

            plt.figure(2)
            plt.clf()
            plt.imshow(inputSpaceCoverage, interpolation='nearest', cmap="jet")
            plt.colorbar()
            plt.savefig('figures/InputCoverage/{}/epoch_{}.png'.format(
                expName, epoch))

        if expConfig.checkTestInput:
            RFcenters, avgDistToCenter = getRFCenters(sp,
                                                      params,
                                                      type='connected')
            inputIdx = 0
            outputColumns = np.zeros((numColumns, 1), dtype=uintType)
            sp.compute(testInputs[inputIdx, :], False, outputColumns)
            activeColumns = np.where(outputColumns > 0)[0]
            fig = plotReceptiveFieldCenter(RFcenters[aliveColumns, :],
                                           connectedCounts[aliveColumns],
                                           (params['nX'], params['nY']))
            plt.scatter(RFcenters[activeColumns, 0],
                        RFcenters[activeColumns, 1],
                        color='r')
            plt.savefig('figures/ResponseToTestInputs/{}/epoch_{}.png'.format(
                expName, epoch))

        if expConfig.saveBoostFactors:
            np.savez('results/boostFactors/{}/epoch_{}'.format(expName, epoch),
                     meanBoostFactors)

        if expConfig.showExampleRFs:
            fig = plotReceptiveFields2D(sp, params['nX'], params['nY'])
            plt.savefig('figures/exampleRFs/{}/epoch_{}'.format(
                expName, epoch))
            plt.close(fig)

        if epoch in checkPoints:
            # inspect SP again
            inspectSpatialPoolerStats(sp, inputVectors,
                                      expName + "epoch{}".format(epoch))

        epoch += 1

    # plot stats over training
    fileName = 'figures/network_stats_over_training_{}.pdf'.format(expName)
    plotSPstatsOverTime(metrics, fileName)

    metrics['expName'] = expName
    pickle.dump(metrics, open('results/traces/{}/trace'.format(expName), 'wb'))

    plotReceptiveFields2D(sp, params['nX'], params['nY'])
    inspectSpatialPoolerStats(sp, inputVectors,
                              inputVectorType + "afterTraining")

    plotExampleInputOutput(sp, inputVectors, expName + "final")
    return metrics, expName
Example #7
0
    def testIndexFromCoordinates(self):
        self.assertEquals(0, indexFromCoordinates((0,), (100,)))
        self.assertEquals(50, indexFromCoordinates((50,), (100,)))
        self.assertEquals(99, indexFromCoordinates((99,), (100,)))

        self.assertEquals(0, indexFromCoordinates((0, 0), (100, 80)))
        self.assertEquals(10, indexFromCoordinates((0, 10), (100, 80)))
        self.assertEquals(80, indexFromCoordinates((1, 0), (100, 80)))
        self.assertEquals(90, indexFromCoordinates((1, 10), (100, 80)))

        self.assertEquals(0, indexFromCoordinates((0, 0, 0), (100, 10, 8)))
        self.assertEquals(7, indexFromCoordinates((0, 0, 7), (100, 10, 8)))
        self.assertEquals(8, indexFromCoordinates((0, 1, 0), (100, 10, 8)))
        self.assertEquals(80, indexFromCoordinates((1, 0, 0), (100, 10, 8)))
        self.assertEquals(88, indexFromCoordinates((1, 1, 0), (100, 10, 8)))
        self.assertEquals(89, indexFromCoordinates((1, 1, 1), (100, 10, 8)))
Example #8
0
def runSPexperiments(expConfig):
  inputVectorType = expConfig.dataSet
  params = getSDRDataSetParams(expConfig.dataSet, int(expConfig.seed))
  expName = getExperimentName(expConfig)
  createDirectories(expName)

  sdrData = SDRDataSet(params)
  inputVectors = sdrData.getInputVectors()
  numInputVector, inputSize = inputVectors.shape

  plt.figure()
  plt.imshow(np.reshape(inputVectors[2], (params['nX'], params['nY'])),
             interpolation='nearest', cmap='gray')
  plt.savefig('figures/exampleInputs/{}'.format(expName))

  print
  print "Runnning experiment: {}".format(expName)
  print "Training Data Size {} Dimensions {}".format(numInputVector, inputSize)

  spParams = getSpatialPoolerParams(params, expConfig)
  sp = createSpatialPooler(expConfig.spatialImp, spParams)
  
  if expConfig.topology == 1 and  expConfig.spatialImp in ['faulty_sp', 'py']:
    initializeSPConnections(sp, potentialRaidus=10, initConnectionRadius=5)

  numColumns = np.prod(sp.getColumnDimensions())

  numTestInputs = int(numInputVector * 0.5)
  testInputs = inputVectors[:numTestInputs, :]

  connectedCounts = np.zeros((numColumns,), dtype=uintType)
  boostFactors = np.zeros((numColumns,), dtype=realDType)
  activeDutyCycle = np.zeros((numColumns,), dtype=realDType)

  metrics = {'numConnectedSyn': [],
             'numNewSyn': [],
             'numRemoveSyn': [],
             'stability': [],
             'entropy': [],
             'maxEntropy': [],
             'sparsity': [],
             'noiseRobustness': [],
             'classification': [],
             'meanBoostFactor': [],
             'reconstructionError': [],
             'witnessError': []}

  connectedSyns = getConnectedSyns(sp)
  activeColumnsCurrentEpoch, dum = runSPOnBatch(sp, testInputs, learn=False)

  inspectSpatialPoolerStats(sp, inputVectors, expName + "beforeTraining")

  checkPoints = [0, expConfig.changeDataSetAt - 1,
                 expConfig.changeDataSetAt, expConfig.numEpochs - 1]

  epoch = 0
  while epoch < expConfig.numEpochs:
    print "training SP epoch {} ".format(epoch)
    if (expConfig.changeDataSetContinuously or
            epoch == expConfig.changeDataSetAt):
      params['seed'] = epoch
      sdrData.generateInputVectors(params)
      inputVectors = sdrData.getInputVectors()
      numInputVector, inputSize = inputVectors.shape
      testInputs = inputVectors[:numTestInputs, :]

    if expConfig.killInputsAfter > 0 and epoch > expConfig.killInputsAfter:
      if expConfig.topology == 1:
        inputSpaceDim = (params['nX'], params['nY'])
        centerColumn = indexFromCoordinates((15, 15), inputSpaceDim)
        deadInputs = topology.wrappingNeighborhood(centerColumn, 5, inputSpaceDim)
      else:
        zombiePermutation = np.random.permutation(inputSize)
        deadInputs = zombiePermutation[:100]
      inputVectors[:, deadInputs] = 0

    if epoch == expConfig.killCellsAt:
      if expConfig.spatialImp in ['faulty_sp', 'monitored_faulty_sp']:
        if expConfig.topology == 1:
          centerColumn = indexFromCoordinates((15, 15), sp._columnDimensions)
          sp.killCellRegion(centerColumn, 5)
        else:
          sp.killCells(expConfig.killCellPrct)

    if expConfig.trackOverlapCurve:
      noiseLevelList, inputOverlapScore, outputOverlapScore = \
        calculateOverlapCurve(sp, testInputs)
      metrics['noiseRobustness'].append(
        np.trapz(np.flipud(np.mean(outputOverlapScore, 0)),
                 noiseLevelList))
      np.savez(
        './results/input_output_overlap/{}/epoch_{}'.format(expName, epoch),
        noiseLevelList, inputOverlapScore, outputOverlapScore)

    if expConfig.classification:
      # classify SDRs with noise
      noiseLevelList = np.linspace(0, 1.0, 21)
      classification_accuracy = classificationAccuracyVsNoise(
        sp, testInputs, noiseLevelList)
      metrics['classification'].append(
        np.trapz(classification_accuracy, noiseLevelList))
      np.savez('./results/classification/{}/epoch_{}'.format(expName, epoch),
               noiseLevelList, classification_accuracy)

    # train SP here,
    # Learn is turned off at the first epoch to gather stats of untrained SP
    learn = False if epoch == 0 else True

    # randomize the presentation order of input vectors
    sdrOrders = np.random.permutation(np.arange(numInputVector))
    activeColumnsTrain, meanBoostFactors = runSPOnBatch(sp, inputVectors, learn, sdrOrders)
    # run SP on test dataset and compute metrics
    activeColumnsPreviousEpoch = copy.copy(activeColumnsCurrentEpoch)
    activeColumnsCurrentEpoch, dum = runSPOnBatch(sp, testInputs, learn=False)

    stability = calculateStability(activeColumnsCurrentEpoch,
                                   activeColumnsPreviousEpoch)
    if (expConfig.changeDataSetContinuously or
            epoch == expConfig.changeDataSetAt):
      stability = float('nan')
    metrics['stability'].append(stability)

    metrics['sparsity'].append(np.mean(np.mean(activeColumnsCurrentEpoch, 1)))

    metrics['entropy'].append(calculateEntropy(activeColumnsCurrentEpoch))

    # generate ideal SP outputs where all columns have the same activation prob.
    activeColumnsIdeal = np.random.rand(numInputVector, numColumns) > metrics['sparsity'][-1]
    metrics['maxEntropy'].append(calculateEntropy(activeColumnsIdeal))

    connectedSynsPreviousEpoch = copy.copy(connectedSyns)
    sp.getConnectedCounts(connectedCounts)
    connectedSyns = getConnectedSyns(sp)

    metrics['meanBoostFactor'].append(np.mean(meanBoostFactors))
    sp.getActiveDutyCycles(activeDutyCycle)

    metrics['numConnectedSyn'].append(np.sum(connectedCounts))

    numNewSynapses = connectedSyns - connectedSynsPreviousEpoch
    numNewSynapses[numNewSynapses < 0] = 0
    metrics['numNewSyn'].append(np.sum(numNewSynapses))

    numEliminatedSynapses = connectedSynsPreviousEpoch - connectedSyns
    numEliminatedSynapses[numEliminatedSynapses < 0] = 0
    metrics['numRemoveSyn'].append(np.sum(numEliminatedSynapses))

    metrics['reconstructionError'].append(
      reconstructionError(sp, testInputs, activeColumnsCurrentEpoch))

    metrics['witnessError'].append(
      witnessError(sp, testInputs, activeColumnsCurrentEpoch))

    print tabulate(metrics, headers="keys")

    if expConfig.checkRFCenters:
      # check distribution of RF centers, useful to monitor recovery from trauma
      RFcenters, avgDistToCenter = getRFCenters(sp, params, type='connected')
      if expConfig.spatialImp == 'faulty_sp':
        aliveColumns = sp.getAliveColumns()
      else:
        aliveColumns = np.arange(numColumns)
      fig = plotReceptiveFieldCenter(RFcenters[aliveColumns, :],
                                     connectedCounts[aliveColumns],
                                     (params['nX'], params['nY']))
      plt.savefig('figures/RFcenters/{}/epoch_{}.png'.format(expName, epoch))
      plt.close(fig)
      np.savez('results/RFcenters/{}/epoch_{}'.format(expName, epoch),
               RFcenters, avgDistToCenter)

    if expConfig.checkInputSpaceCoverage:
      # check coverage of input space, useful to monitor recovery from trauma
      inputSpaceCoverage = calculateInputSpaceCoverage(sp)
      np.savez('results/InputCoverage/{}/epoch_{}'.format(expName, epoch),
               inputSpaceCoverage, connectedCounts)

      plt.figure(2)
      plt.clf()
      plt.imshow(inputSpaceCoverage, interpolation='nearest', cmap="jet")
      plt.colorbar()
      plt.savefig(
        'figures/InputCoverage/{}/epoch_{}.png'.format(expName, epoch))

    if expConfig.checkTestInput:
      RFcenters, avgDistToCenter = getRFCenters(sp, params, type='connected')
      inputIdx = 0
      outputColumns = np.zeros((numColumns, 1), dtype=uintType)
      sp.compute(testInputs[inputIdx, :], False, outputColumns)
      activeColumns = np.where(outputColumns > 0)[0]
      fig = plotReceptiveFieldCenter(RFcenters[aliveColumns, :],
                                     connectedCounts[aliveColumns],
                                     (params['nX'], params['nY']))
      plt.scatter(RFcenters[activeColumns, 0], RFcenters[activeColumns, 1],
                  color='r')
      plt.savefig(
        'figures/ResponseToTestInputs/{}/epoch_{}.png'.format(expName, epoch))

    if expConfig.saveBoostFactors:
      np.savez('results/boostFactors/{}/epoch_{}'.format(expName, epoch),
               meanBoostFactors)

    if expConfig.showExampleRFs:
      fig = plotReceptiveFields2D(sp, params['nX'], params['nY'])
      plt.savefig('figures/exampleRFs/{}/epoch_{}'.format(expName, epoch))
      plt.close(fig)

    if epoch in checkPoints:
      # inspect SP again
      inspectSpatialPoolerStats(sp, inputVectors, expName+"epoch{}".format(epoch))

    epoch += 1

  # plot stats over training
  fileName = 'figures/network_stats_over_training_{}.pdf'.format(expName)
  plotSPstatsOverTime(metrics, fileName)

  metrics['expName'] = expName
  pickle.dump(metrics, open('results/traces/{}/trace'.format(expName), 'wb'))

  plotReceptiveFields2D(sp, params['nX'], params['nY'])
  inspectSpatialPoolerStats(sp, inputVectors, inputVectorType + "afterTraining")

  plotExampleInputOutput(sp, inputVectors, expName + "final")
  return metrics, expName
Example #9
0
def plotMovie(expName, numEpochs, killAt):
    # plot RFcenters and inputCoverage over training
    centerColumn = indexFromCoordinates((15, 15), (32, 32))
    deadCols = topology.wrappingNeighborhood(centerColumn, 5, (32, 32))

    for epoch in range(killAt, numEpochs):
        fig, axs = plt.subplots(2, 2)
        RFcenterInfo = np.load('results/RFcenters/{}/epoch_{}.npz'.format(
            expName, epoch))
        RFcenters = RFcenterInfo['arr_0']
        avgDistToCenter = RFcenterInfo['arr_1']
        axs[0, 1].scatter(RFcenters[:, 0], RFcenters[:, 1], s=4, c=[1, 1, 1])
        axs[0, 1].set_title('RF centers')
        axs[0, 1].set_xlim([-1, 33])
        axs[0, 1].set_ylim([-1, 33])
        axs[0, 1].set_aspect('equal')
        plt.axis('equal')

        # # plot input space coverage
        inputSpaceCoverage = np.load(
            'results/InputCoverage/{}/epoch_{}.npz'.format(expName, epoch))
        inputSpaceCoverage = inputSpaceCoverage['arr_0']
        im1 = axs[0, 0].pcolor(inputSpaceCoverage, vmin=10, vmax=80)
        axs[0, 0].set_title('Input Space Coverage')
        axs[0, 0].set_xlim([-1, 33])
        axs[0, 0].set_ylim([-1, 33])
        axs[0, 0].set_aspect('equal')
        plt.axis('equal')

        boostFactors = np.load('results/boostFactors/{}/epoch_{}.npz'.format(
            expName, epoch))
        boostFactors = boostFactors['arr_0']
        # boostFactors[deadCols] = np.nan
        boostFactors = np.reshape(boostFactors, (32, 32))
        im2 = axs[1, 0].pcolor(boostFactors, vmin=0.5, vmax=1.5)
        axs[1, 0].set_title('Boost Factors')
        axs[1, 0].set_xlim([-1, 33])
        axs[1, 0].set_ylim([-1, 33])
        axs[1, 0].set_aspect('equal')
        plt.axis('equal')

        # fig.delaxes(axs[1, 0])
        cax = fig.add_axes([0.55, 0.4, 0.3, 0.05])
        fig.colorbar(im1,
                     cax=cax,
                     orientation='horizontal',
                     ticks=[0, 20, 40, 60, 80])
        cax.set_xlabel('input space coverage')

        cax = fig.add_axes([0.55, 0.2, 0.3, 0.05])
        fig.colorbar(im2,
                     cax=cax,
                     orientation='horizontal',
                     ticks=[0, .5, 1, 1.5, 2])
        cax.set_xlabel('boost factors')

        fig.delaxes(axs[1, 1])

        plt.savefig('figures/traumaMovie/{}_frame_{}.pdf'.format(
            expName, epoch))
        plt.close(fig)
            params['seed'] = epoch
            sdrData.generateInputVectors(params)
            inputVectors = sdrData.getInputVectors()
            numInputVector, inputSize = inputVectors.shape
            plt.figure(10)
            plt.clf()
            plt.imshow(np.reshape(np.sum(inputVectors, 0),
                                  (params['nX'], params['nY'])),
                       interpolation='nearest',
                       cmap='jet')
            plt.colorbar()
            plt.savefig('figures/avgInputs/{}/epoch_{}'.format(expName, epoch))

        if killInputsAfter > 0 and epoch > killInputsAfter:
            inputSpaceDim = (params['nX'], params['nY'])
            centerColumn = indexFromCoordinates((15, 15), inputSpaceDim)
            deadInputs = topology.wrappingNeighborhood(centerColumn, 5,
                                                       inputSpaceDim)
            inputVectors[:, deadInputs] = 0

        if epoch == killCellsAt:
            if spatialImp == "faulty_sp" or spatialImp == "monitored_faulty_sp":
                # sp.killCells(killCellPrct)
                centerColumn = indexFromCoordinates((15, 15),
                                                    sp._columnDimensions)
                sp.killCellRegion(centerColumn, 5)

        if trackOverlapCurveOverTraining:
            noiseLevelList, inputOverlapScore, outputOverlapScore = \
              calculateOverlapCurve(sp, inputVectors[:20, :])
            noiseRobustnessTrace.append(