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 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)
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()
import pylab import numpy as np from scipy import random from pybrain.structure.modules import KohonenMap from random import randint kohonendata = [ l.strip().replace(',', '.').split('^')[2:4] for l in open('data/banks.dat').readlines() ] som = KohonenMap(len(kohonendata[0]), 66) pylab.ion() p = pylab.plot(som.neurons[:, :, 0].flatten(), som.neurons[:, :, 1].flatten(), 's') l = len(kohonendata) for i in range(250000): # one forward and one backward (training) pass k = randint(0, l - 1) som.activate(kohonendata[k]) som.backward() # plot every 100th step if i % 100 == 0: p[0].set_data(som.neurons[:, :, 0].flatten(), som.neurons[:, :, 1].flatten()) pylab.draw() pylab.savefig('kohonen.png') # =CONCATENATE("[";C2;",";D2;",";E2;",";F2;",";G2;",";H2;",";I2;",";J2;",";K2;",";"]")
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)