def generateSymbol(self, matrix, maxIterations=20, movieFilename=None): self.matrix = matrix[:] self._matrixWidth = len(self.matrix) mainQuadrant = _Quadrant(quadrantWidth=self._matrixWidth, leftCoordinate=0, topCoordinate=0) self.initialEntropy = self.__calcEntropy(mainQuadrant) self._maxIterations = maxIterations self._imageFrame = 0 self._movieFilename = movieFilename if self._movieFilename is not None: self._imageFileNamePrefix = self._movieFilename exputils.writeMatrixImage(matrix=self.matrix, filename=self._imageFileNamePrefix + str(self._imageFrame).zfill(5) + ".png") self._imageFrame = self._imageFrame + 1 self.finalEntropy = self.__process(quadrant=mainQuadrant) if self._movieFilename is not None: exputils.generateMovie( pngFilenamePattern=self._imageFileNamePrefix + "*.png", outputFilename=self._movieFilename, fps=5)
def __process(self, quadrant): previousEntropy = self.initialEntropy iterationExecutions = 0 for i in xrange(0, self._maxIterations): print "main loop iteration", iterationExecutions newEntropy = self.__recursiveStep(level=0, quadrant=quadrant) if self._movieFilename is not None: exputils.writeMatrixImage(matrix=self.matrix, filename=self._imageFileNamePrefix + str(self._imageFrame).zfill(5) + ".png") self._imageFrame = self._imageFrame + 1 iterationExecutions = iterationExecutions + 1 if previousEntropy == newEntropy: break previousEntropy = newEntropy return newEntropy
def process(self, matrix, level=0, maxIterations=20): self.level = level self.subSymbolGenerators = [] self.matrix = matrix self.matrixWidth = len(matrix) self.globalId = getGlobalId() self.initialEntropy = exputils.calcEntropy( self.matrix.flatten())["entropy"] previousEntropy = -1 for x in xrange( 0, maxIterations ): # maxIterations passes over the data should get it to a stable entropy. Break out early if it stabilizes less than max exputils.writeMatrixImage( self.matrix, "sg_test_pic_" + str(x).zfill(5) + ".png") self.iterate() currentEntropy = exputils.calcEntropy(self.matrix.flatten()) if currentEntropy == previousEntropy: break else: previousEntropy = currentEntropy return self
baseValue = 1 else: baseValue = 0 base[x] = baseValue #--------------------- main loop ----------------------- print "starting main loop" for cycle in xrange(0, 200): #print "Flat input: " entropy = exputils.calcEntropy(flatInput) print str(cycle) + ": " + exputils.vectorToString(flatInput), "e = " + str( entropy["entropy"]) + " me = " + str(entropy["metricEntropy"]) unflattenedInput = exputils.unflattenArray(flatInput, inputWidth) if generateOutputMovie: exputils.writeMatrixImage( unflattenedInput, outputImagePrefix + str(cycle).zfill(5) + ".png") spatialPoolerOutput = numpy.zeros(shape=flatInputLength, dtype="int32") spatialPooler.compute(inputVector=flatInput, learn=True, activeArray=spatialPoolerOutput) #print "Spatial Pooler output from input: " #print exputils.vectorToString(spatialPoolerOutput) #print "\n" temporalPooler.compute(bottomUpInput=spatialPoolerOutput, enableLearn=True, computeInfOutput=True) predictedCells = temporalPooler.getPredictedState() #exputils.printPredictedCells(predictedCells) predictionVector = exputils.getPredictionVector(predictedCells)
#imageArray = exputils.getBitArrayFromBitmapFile("plus_sign_2.bmp") #squareImageArray = exputils.unflattenArray(imageArray, 512) inputWidth = 8192 input1 = exputils.getRandom2dBoolMatrix(inputWidth, inputWidth) # #for y in xrange(106, 406): # for x in xrange(226, 286): # input1[y, x] = 0 # #for y in xrange(226, 286): # for x in xrange(106, 406): # input1[y, x] = 0 # exputils.writeMatrixImage(input1, "input_1_raw.png") inputData = input1 previousEntropy = -1 for cycle in xrange(0, 20): currentEntropy = exputils.calcEntropy(inputData.flatten()) if previousEntropy != currentEntropy: previousEntropy = currentEntropy print "iteration", cycle, "initial entropy", currentEntropy mainMatrixAnalysis = MatrixAnalysis() mainMatrixAnalysis.process(matrix=inputData) print "iteration", cycle, "final entropy", mainMatrixAnalysis.finalEntropy exputils.writeMatrixImage( mainMatrixAnalysis.matrix, "input_1_red_" + str(cycle).zfill(5) + ".png")