 |
Kohonen Feature Map in 2D (SOM)
Source code name: "neural_net_kohonen_map2D.py"
Programming language: Python
Topic: Artificial Intelligence/neural net
DMelt Version 1. Last modified: 10/30/2015. License: Pro
https://datamelt.org/code/cache/neural_net_kohonen_map2D_3658.py
To run this script using the DMelt IDE,
copy the above URL link to the menu [File]→[Read script from URL] of the DMelt IDE.
# In Kohonen Feature Map, neurons are organizing themselves according to certain input values.
# wse 4x4 neutrons and plot the results as line.
# (c) Chekanov
from jhplot import *
from jhpro.nnet import *
from java.util import Random
from java.awt import Color
import math
# make empty canvas in some range
c1 =HPlot("Canvas")
c1.visible()
c1.setLegend(0)
c1.setRange(0,100,0,150)
c1.setMarginLeft(70)
c1.setNameX("X")
c1.setNameY("Y")
p1= P1D("X-Y data")
rand = Random(10)
inputSize=100 # number of random points in 2D
for i in range(100):
x=i+10*rand.nextGaussian()
y=50+30*math.cos(0.2*x) + 10*rand.nextGaussian()
p1.add(x,y)
c1.draw(p1)
kfm=KohonenFeatureMap()
mapSizeX=4 # map size is 4x4 neurons
maxCycle=100000000
im = InputMatrix(inputSize, 2);
kfm.setMaxLearningCycles(maxCycle);
kfm.createMapLayer(mapSizeX,mapSizeX ) # create a map in X-X
kfm.setStopArea(0.01) # stop learning here
kfm.setInitActivationArea(1)
kfm.setInitLearningRate(0.6)
im.setInputXY(p1)
kfm.connectLayers(im);
i=0
# to represent outputs
p2=P1D("weights")
p2.setColor(Color.red)
p2.setStyle("l")
while kfm.finishedLearning() == False:
kfm.learn()
i=i+1
if (i%50 ==0):
weights=kfm.getWeightValues()
p2.clear(); c1.clearData()
print "print rate=",kfm.getLearningRate(), "Activation area=",kfm.getActivationArea(), "Elapsed time=",kfm.getElapsedTime()
for j in range(mapSizeX*mapSizeX):
p2.add(weights[0][j], weights[1][j])
p2.sort(0) # to draw a line, we should sort the result in X
c1.draw(p1)
c1.draw(p2)
print "Final activation area=",kfm.getStopArea()
p3=P1D("weights")
p3.setColor(Color.red)
p3.setStyle("l")
print p2
You see the box below because you did not login.