def iterate(self, level=0): if self.matrixWidth > 1: # we can further descend winnerMatrixAttributes = None for subMatrix in getSubMatrices(self.matrix): # this could be done in parallel while this parent process blocks until its sub matrices are done subSymbolGenerator = SymbolGenerator() result = subSymbolGenerator.process(matrix=subMatrix, level=level + 1) self.subSymbolGenerators.append(result) # the winner sub-matrix is the one with the least entropy # ???? in case of a tie, the first one in sequence is selected as the winner. This will add some bias. # ???? is there other information we can utilize to determine which of the n winners to select? if winnerMatrixAttributes is None or result.initialEntropy < winnerMatrixAttributes[ "initialEntropy"]: winnerMatrixAttributes = { "globalId": result.globalId, "matrix": result.matrix, "initialEntropy": result.initialEntropy } # xor winner with all peers, not itself as that would lose data for subSymbolGenerator in self.subSymbolGenerators: if subSymbolGenerator.globalId != winnerMatrixAttributes[ "globalId"]: subSymbolGenerator.matrix = xorMatrices( winnerMatrixAttributes["matrix"], subSymbolGenerator.matrix) subSymbolGenerator.finalEntropy = exputils.calcEntropy( subSymbolGenerator.matrix.flatten())["entropy"] # bundle matrices and replace own matrix newMatrices = [] for subSymbolGenerator in self.subSymbolGenerators: newMatrices.append(subSymbolGenerator.matrix) if len(newMatrices) != 0: self.matrix = bundleMatrices(newMatrices) self.finalEntropy = exputils.calcEntropy( self.matrix.flatten())["entropy"] else: self.finalEntropy = self.initialEntropy return self
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
def performBitArrayOperations(leftArray, rightArray): left = "" right = "" andResult = "" andResultArray = [] orResult = "" orResultArray = [] xorResult = "" xorResultArray = [] nandResult = "" nandResultArray = [] for x in xrange(0, len(leftArray)): left += str(leftArray[x]) right += str(rightArray[x]) if leftArray[x] == rightArray[x] and leftArray[x] == 1: andResult += "1" andResultArray.append(1) else: andResult += "0" andResultArray.append(0) if leftArray[x] == 1 or rightArray[x] == 1: orResult += "1" orResultArray.append(1) else: orResult += "0" orResultArray.append(0) if leftArray[x] == rightArray[x]: xorResult += "0" xorResultArray.append(0) else: xorResult += "1" xorResultArray.append(1) if leftArray[x] == rightArray[x] and leftArray[x] == 1: nandResult += "0" nandResultArray.append(0) else: nandResult += "1" nandResultArray.append(1) print(left, exputils.calcEntropy(leftArray)["entropy"], right, exputils.calcEntropy(rightArray)["entropy"], andResult, exputils.calcEntropy(andResultArray)["entropy"], orResult, exputils.calcEntropy(orResultArray)["entropy"], xorResult, exputils.calcEntropy(xorResultArray)["entropy"], nandResult, exputils.calcEntropy(nandResultArray)["entropy"])
result += "0" return result digits = 4 operands = [] for x in xrange(0, 2**digits): operands.append("{0:b}".format(x).zfill(digits)) for lop in operands: for rop in operands: xored = xor(lop, rop) lopPatternArray = map(int, list(lop)) ropPatternArray = map(int, list(rop)) xoredPatternArray = map(int, list(xored)) lopEntropy = exputils.calcEntropy(lopPatternArray)["entropy"] ropEntropy = exputils.calcEntropy(ropPatternArray)["entropy"] xoredEntropy = exputils.calcEntropy(xoredPatternArray)["entropy"] change = "same" if (xoredEntropy < lopEntropy): change = "dec" elif (xoredEntropy > lopEntropy): change = "inc" print "\"" + lop + "_" + rop + "\": \"" + change + "\"," #print lop, lopEntropy, rop, ropEntropy, xored, xoredEntropy, change #for x in xrange(0, len(fiveDigits)): # print exputils.calcEntropy(fiveDigits[x])["entropy"]
# simple bit flip for x in range(0, len(manipulator)): if manipulator[x] == 1: baseValue = base[x] if baseValue == 0: 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"
def process(self, matrix, level=0): self.level = level self.subMatrixAnalyses = [] self.matrix = matrix self.matrixWidth = len(matrix) self.globalId = getGlobalId() self.initialEntropy = exputils.calcEntropy( self.matrix.flatten())["entropy"] #print (" " * level), "processing globalId", self.globalId if self.matrixWidth > 1: # we can further descend winnerMatrixAttributes = None for subMatrix in getSubMatrices(self.matrix): # this could be done in parallel while this parent process blocks until its sub matrices are done subMatrixAnalysis = MatrixAnalysis() result = subMatrixAnalysis.process(matrix=subMatrix, level=level + 1) self.subMatrixAnalyses.append(result) if winnerMatrixAttributes is None or result.initialEntropy < winnerMatrixAttributes[ "initialEntropy"]: winnerMatrixAttributes = { "globalId": result.globalId, "matrix": result.matrix, "initialEntropy": result.initialEntropy } if winnerMatrixAttributes is None: winner = self.subMatrixAnalyses[0] winnerMatrixAttributes = { "globalId": result.globalId, "matrix": result.matrix, "initialEntropy": result.initialEntropy } #print "determined level", level, winnerMatrixAttributes["globalId"], " entropy:", winnerMatrixAttributes["entropy"] # xor winner with all peers, not self for subMatrixAnalysis in self.subMatrixAnalyses: # #print (" " * level), " applying xor to ", subMatrixAnalysis.globalId #if subMatrixAnalysis.globalId != winnerMatrixAttributes["globalId"]: subMatrixAnalysis.matrix = xorMatrices( winnerMatrixAttributes["matrix"], subMatrixAnalysis.matrix) subMatrixAnalysis.finalEntropy = exputils.calcEntropy( subMatrixAnalysis.matrix.flatten())["entropy"] # bundle matrices and replace own matrix newMatrices = [] for subMatrixAnalysis in self.subMatrixAnalyses: newMatrices.append(subMatrixAnalysis.matrix) if len(newMatrices) != 0: self.matrix = bundleMatrices(newMatrices) self.finalEntropy = exputils.calcEntropy( self.matrix.flatten())["entropy"] else: self.finalEntropy = self.initialEntropy return self
# #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") inputData = mainMatrixAnalysis.matrix ##inputData = exputils.getRandom2dBoolMatrix(inputWidth, inputWidth) # # #testPicOriginal = exputils.getRandom2dBoolMatrix(inputWidth, inputWidth)
def xorInputs(i1, i2): length = len(i1) outputData = numpy.zeros(shape=length, dtype="uint8") for x in xrange(0, len(i1)): if i1[x] != i2[x]: outputData[x] = 1 return outputData i3 = xorInputs(flati1, flati2) data = [flati1, flati2, i3] #--------------------- main loop ----------------------- print "starting main loop" for cycle in xrange(0, len(data)): d = data[cycle] entropy = exputils.calcEntropy(d) print str(cycle) + ": " + exputils.vectorToString(d), "e = " + str( entropy["entropy"]) + " me = " + str(entropy["metricEntropy"]) unflattenedInput = exputils.unflattenArray(d, inputWidth) if generateOutputMovie: exputils.writeMatrixImage( unflattenedInput, outputImagePrefix + str(cycle).zfill(5) + ".png") if generateOutputMovie: exputils.generateMovie(outputImageMask, outputImagePrefix + "movie.gif", fps=15)