def kohonen(): som = KohonenMap(2, 10) pylab.ion() p = pylab.plot(som.neurons[:, :, 0].flatten(), som.neurons[:, :, 1].flatten(), 's') for i in range(25000): # one forward and one backward (training) pass som.activate(random.random(2)) som.backward() # plot every 100th step if i % 100 == 0: p[0].set_data(som.neurons[:, :, 0].flatten(), som.neurons[:, :, 1].flatten()) pylab.draw()
def categorize(categories, data, words): nnodes = sqrt(categories) # make a kohonen map som = KohonenMap(len(data[1])-1, nnodes) # train the network for i in range(25000): # one forward and one backward (training) pass on a random line som.activate(data[random.randint(1, len(data)-1)][1:]) som.backward() # run on the data if not words: for point in data[1:-1]: #find the category for a point of data print(point[0] + ',' + str(som.activate(point[1:]))) #make wordlist output similar to ICA for use in GAs if words: results = [[] for i in range(categories)] for point in data[1:-1]: # find the cluster for this word and add in the word's data result = som.activate(point[1:]) results[int(result[0]*nnodes + result[1])].append(point) # print out the clusters for i in range(NWORDS): # TODO: This is super inefficient for cluster in results: if len(cluster) == 0: sys.stdout.write('EMPTY') continue for j in range(len(cluster)): for k in range(1, len(cluster[j])): cluster[j][k] = float(cluster[j][k]) tempCluster = sorted(cluster, key=lambda point: -sum(point[1:])) sys.stdout.write(str(tempCluster[i%len(cluster)][0]) + ',') sys.stdout.write('\n')
def aldohonen(size, iterations, training_input, refresh_rate, callback): som = KohonenMap(3, size) #just changing initial neurons weight from 0~1 to 0~255 for i in som.neurons: for j in i: for k in range(len(j)): j[k] *= 255 training_size = len(training_input) - 1 for i in range(iterations): # one forward and one backward (training) pass som.activate(training_input[random.randint(0, training_size)]) som.backward() #just draw som in our Qt View if (i % refresh_rate == 0): image = som2image(som, size) callback(image, i) else: callback(None, i) callback(som2image(som, size), iterations - 1)
for i in range(numPatValid): patternValidTarget[i] = patternValid[i, 0] for i in range(numPatTest): patternTestTarget[i] = patternTest[i, 0] counterOut = 0 while(counterOut < 10): neuronas = 10 #Crear y entrenar el mapa autoorganizado som = KohonenMap(numColsTrain-1, neuronas) #Entrenar el mapa for i in range(numPatTrain): som.activate(patternTrainInput[i]) som.backward() #Crear el dataset de entrenamiento de backprop con resultados del mapa input = np.zeros([numPatTrain,neuronas**2]) distMatrix = np.zeros([neuronas, neuronas]) for i in range(numPatTrain): tmp = som.activate(patternTrainInput[i]) distMatrix[tmp[0], tmp[1]] += 1 inputTMP = np.zeros(neuronas**2) inputTMP[(neuronas*tmp[0]) + tmp[1]] = 1.0 input[i] = inputTMP kohonenDS = SupervisedDataSet(neuronas**2, 1) for i in range(numPatTrain): kohonenDS.addSample(input[i], patternTrainTarget[i])
def run(): # init the SquareMap object (which initiates pygame) if imgFlag == 0: # if its a grayscale similarity map use a small grid square size squaremap = SquareMap(gridSize=(somOutputs, somOutputs), gridSquareSize=(5, 5)) else: squaremap = SquareMap(gridSize=(somOutputs, somOutputs)) squaremap.initMap() # Used to manage how fast the screen updates #clock = pygame.time.Clock() fullscreen = False #Loop until the user presses the escape button (or otherwise quits). done = False while done == False: # Limit to 15 frames per second #clock.tick(15) ########################### # Key events for quitting # # & setting fullscreen # ########################### for event in pygame.event.get(): if event.type == QUIT: done = True if squaremap.keyPressed(K_ESCAPE): done = True # toggle fullscreen and cursor on/off elif squaremap.keyPressed(K_f): if not fullscreen: squaremap.fullScreen(True) fullscreen = True else: squaremap.fullScreen(False) fullscreen = False elif squaremap.keyPressed(K_SPACE): # load and place the images squaremap.placeImages() # this is only for testing ############################### # Database and SOM operations # ############################### # get data from DB (all the rows) rows = getData() imgPath = "" # initialize SOM object som = KohonenMap(somInputs, somOutputs, name="SquareMap", outputFullMap=False) # set the learning rate (should this be a command line arg?) som.learningrate = learningRate # Do SOM operations here # do as many iterations as there is DB entries/images #======================================================================= # for i in range(rows): # # 8 feature vectors # data = [rows[i][1], rows[i][2], rows[i][3], rows[i][4], rows[i],[5], rows[i][6], rows[i][7], rows[i][8]] # # one forward and one backward (training) pass # som.activate(data) # feature vectors go here # som.backward() # training #======================================================================= for i in range(100): som.activate(random.random(somInputs)) som.backward() #=================================================================== # # print & display every 10th step # if i % 1 == 0: # print som.neurons # print "======================" #=================================================================== # send data to update the square map # if images we selected at the command line do an image similarity mapping #otherwise do a grayscale 'blobs' similarity map if imgFlag == 0: squaremap.createSimilarityMap(som.neurons) else: #send the current img file path and the coords of the winner neuron along w/SOM's output imgPath = rows[i][10] squaremap.placeImages(som.neurons, imgPath, som.winner)
################################################## # Example for Kohonen Map # # Clusters random 2D coordinates in range [0,1] # with a Kohonen Map of 5x5 neurons. # # Note: you need pylab to show the results ################################################## __author__ = 'Thomas Rueckstiess, [email protected]' import pylab from scipy import random from pybrain.structure.modules import KohonenMap som = KohonenMap(2, 5) pylab.ion() p = pylab.plot(som.neurons[:, :, 0].flatten(), som.neurons[:, :, 1].flatten(), 's') for i in range(25000): # one forward and one backward (training) pass som.activate(random.random(2)) som.backward() # plot every 100th step if i % 100 == 0: p[0].set_data(som.neurons[:, :, 0].flatten(), som.neurons[:, :, 1].flatten()) pylab.draw()
def run(): # init the HexagonMap object (which initiates pygame) hexmap = HexagonMap(gridSize=(somOutputs, somOutputs)) hexmap.initMap() # Used to manage how fast the screen updates #clock = pygame.time.Clock() fullscreen = False #Loop until the user presses the escape button (or otherwise quits). done = False while done == False: # Limit to 15 frames per second #clock.tick(15) ########################### # Key events for quitting # # & setting fullscreen # ########################### for event in pygame.event.get(): # User did something if event.type == QUIT: # If user clicked close done = True # Flag that we are done so we exit this loop if hexmap.keyPressed(K_ESCAPE): done = True # toggle fullscreen and cursor on/off elif hexmap.keyPressed(K_f): if not fullscreen: hexmap.fullScreen(True) fullscreen = True else: hexmap.fullScreen(False) fullscreen = False elif hexmap.keyPressed(K_SPACE): hexmap.createSimilarityMap() # this is only for testing ############################### # Database and SOM operations # ############################### # get data from DB (all the rows) rows = getData() # initialize SOM object som = KohonenMap(somInputs, somOutputs, name="HexagonMap", outputFullMap=False) # set the learning rate (should this be a command line arg?) som.learningrate = learningRate # Do SOM operations here # do as many iterations as there is DB entries/images #======================================================================= # for i in range(rows): # # 8 feature vectors # data = [rows[i][1], rows[i][2], rows[i][3], rows[i][4], rows[i],[5], rows[i][6], rows[i][7], rows[i][8]] # # one forward and one backward (training) pass # som.activate(data) # feature vectors go here # som.backward() # training #======================================================================= for i in range(100): som.activate(random.random(somInputs)) som.backward() #=================================================================== # # print & display every 10th step # if i % 10 == 0: # print som.neurons # print "======================" #=================================================================== # send data to update the hex map hexmap.createSimilarityMap(som.neurons)