Esempio n. 1
0
  def _getTopDownMapping(self):
    """ Return the interal _topDownMappingM matrix used for handling the
    bucketInfo() and topDownCompute() methods. This is a matrix, one row per
    category (bucket) where each row contains the encoded output for that
    category.
    """

    # Do we need to build up our reverse mapping table?
    if self._topDownMappingM is None:

      # The input scalar value corresponding to each possible output encoding
      if self.periodic:
        self._topDownValues = numpy.arange(self.minval + self.resolution / 2.0,
                                           self.maxval,
                                           self.resolution)
      else:
        #Number of values is (max-min)/resolutions
        self._topDownValues = numpy.arange(self.minval,
                                           self.maxval + self.resolution / 2.0,
                                           self.resolution)

      # Each row represents an encoded output pattern
      numCategories = len(self._topDownValues)
      self._topDownMappingM = SM32(numCategories, self.n)

      outputSpace = numpy.zeros(self.n, dtype=GetNTAReal())
      for i in xrange(numCategories):
        value = self._topDownValues[i]
        value = max(value, self.minval)
        value = min(value, self.maxval)
        self.encodeIntoArray(value, outputSpace, learn=False)
        self._topDownMappingM.setRowFromDense(i, outputSpace)

    return self._topDownMappingM
Esempio n. 2
0
def _analyzeTrainedNet(net, options):
    """ Analyze the trained network and return a dict containing auxiliary
  information that will be used when analyzing each inference run.

  Parameter:
  -----------------------------------------------------------
  net:              The trained network.
  options:          dictionary of post-processing options.

  retval:           Dictionary of auxiliary information about this network,
                      like the set of learned coincidences from the SP, etc.
  """

    print "\n-------------------------------------------------------------------"
    print "Analyzing the trained network..."

    # ---------------------------------------------------------------------------
    # Get the objects we need from the network
    sp = net.regions['level1'].getSelf()._sfdr
    encoder = net.regions['sensor'].getSelf().encoder

    if sp:
        # Reqired?
        sp._updateInhibitionObj()

        # ---------------------------------------------------------------------------
        # Get the learned coincidences for each column into cm.
        cm = SM32(0, sp.inputShape[0] * sp.inputShape[1])

        outputSize = sp.coincidencesShape[0] * sp.coincidencesShape[1]
        for i in xrange(outputSize):
            denseRow = sp.getLearnedCmRowAsDenseArray(i)
            cm.addRow(denseRow)

        # ===========================================================================
        # Print out the results
        print "\nSP Learning stats:"
        pprint.pprint(sp.getLearningStats())

    else:
        cm = None
        # sp output size is sp input size (if no sp)
        outputSize = net.regions['level1'].getInputData('bottomUpIn').shape[0]
    # ---------------------------------------------------------------------------
    # Parameters for multi-step prediction analysis
    nMultiStepPrediction = net.regions['level1'].getParameter(
        "nMultiStepPrediction")
    burnIn = net.regions['level1'].getParameter("burnIn")

    # ===========================================================================
    # Return info
    return dict(
        net=net,
        sp=sp,
        cm=cm,
        encoder=encoder,
        outputSize=outputSize,
        nMultiStepPrediction=nMultiStepPrediction,
        burnIn=burnIn,
    )
Esempio n. 3
0
    def testCastMode(self):
        """Test for an obscure error that is fixed by the -castmode flag to swig.

    This code will throw an exception if the error exists.
    """
        hist = SM32(5, 10)
        t = numpy.array([0, 0, 1, 0, 1, 0, 0, 1, 0, 1], dtype='float32')

        hist.setRowFromDense(0, t)
        hist.setRowFromDense(1, t)
        self.assertSequenceEqual(tuple(hist.getRow(1)),
                                 (0, 0, 1, 0, 1, 0, 0, 1, 0, 1))
Esempio n. 4
0
  def test_construction(self):
    a = self.Matrix.__class__(4)

    if a.nRows() != 0 or a.nCols() != 4:
      error('constructor 1')

    b = self.Matrix.__class__(a)
    if b.nRows() != 0 or b.nCols() != 4:
      error('constructor 2A')

    if (a.toDense() != b.toDense()).any():
      error('constructor 2B')

    m = _RGEN.randint(1,10)
    n = _RGEN.randint(5,10)
    a = self.Matrix.__class__(n)
    x = _RGEN.randint(0,2,(m,n))
    x[m//2] = numpy.zeros((n))

    for i in range(m):
      a.appendSparseRow(numpy.where(x[i] > 0)[0].tolist())

    b = self.Matrix.__class__(a)
    if (a.toDense() != b.toDense()).any():
      error('copy constructor')

    c = self.Matrix.__class__(x)
    if (c.toDense() != x).any():
      error('constructor from numpy array')

    s = c.toCSR()
    d = self.Matrix.__class__(s)
    if (d.toDense() != x).any():
      error('constructor from csr string')

    # Test construction from a SM
    a = _RGEN.randint(0,10,(3,4))
    a[2] = 0
    a[:,3] = 0
    a = SM32(a)
    b = SM_01_32_32(a)
    a = a.toDense()
    w = numpy.where(a > 0)
    a[w] = 1
    if (a != b.toDense()).any():
      error('construction from SM')
Esempio n. 5
0
    def _getTopDownMapping(self):
        """ Return the interal _topDownMappingM matrix used for handling the
    bucketInfo() and topDownCompute() methods. This is a matrix, one row per
    category (bucket) where each row contains the encoded output for that
    category.
    """

        if self._topDownMappingM is None:
            self._topDownMappingM = SM32(self._numBins, self.n)

            outputSpace = numpy.zeros(self.n, dtype=GetNTAReal())

            for i in xrange(self._numBins):
                outputSpace[:] = 0.0
                outputSpace[i:i + self.w] = 1.0
                self._topDownMappingM.setRowFromDense(i, outputSpace)

        return self._topDownMappingM
Esempio n. 6
0
  def _getTopDownMapping(self):
    """ Return the interal _topDownMappingM matrix used for handling the
    bucketInfo() and topDownCompute() methods. This is a matrix, one row per
    category (bucket) where each row contains the encoded output for that
    category.
    """

    # -------------------------------------------------------------------------
    # Do we need to build up our reverse mapping table?
    if self._topDownMappingM is None:

      # Each row represents an encoded output pattern
      self._topDownMappingM = SM32(self.ncategories, self.n)

      outputSpace = numpy.zeros(self.n, dtype=GetNTAReal())
      for i in range(self.ncategories):
        self.encodeIntoArray(self.categories[i], outputSpace)
        self._topDownMappingM.setRowFromDense(i, outputSpace)

    return self._topDownMappingM