Kohonen Feature Map in 3D (SOM)
Source code name: "neural_net_kohonen_map3D.py"
Programming language: Python
Topic: Artificial Intelligence/neural net
DMelt Version 1. Last modified: 05/09/2015. License: Pro
https://datamelt.org/code/cache/neural_net_kohonen_map3D_6260.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. 
# (c) Chekanov

from jhplot  import  *
from jhpro.nnet import *
from java.awt import *
from java.util import Random
import math

kfm=KohonenFeatureMap()

inputSize=200   # number of random points in 3D 
mapSizeX=4     # map size is 4x4
mapSizeY=4 
maxCycle=1000000
im = InputMatrix(inputSize, 3);
kfm.setMaxLearningCycles(maxCycle);
kfm.createMapLayer(mapSizeX, mapSizeY) # create a map in Y-Y
kfm.setStopArea(0.02)                   # stop learning here
kfm.setInitActivationArea(1)
kfm.setInitLearningRate(0.6)

c1=HPlot3D("Canvas",600,500)
c1.setRange(-100, 100, -100, 100, -100,100)
c1.visible()
pc=P2D("Input Data")
from jarray import array
inputX=[]
inputY=[]
inputZ=[]
rand = Random()
for i in range(inputSize):
     x=10*rand.nextGaussian() 
     y=15*rand.nextGaussian()
     z=20*rand.nextGaussian()
     if (i>100):
             x=10+10*rand.nextGaussian() 
             y=-20-30*rand.nextGaussian()
             z=50-10*rand.nextGaussian()
     pc.add(x,y,z)
     inputX.append(int(x))
     inputY.append(int(y))
     inputZ.append(int(z)) 

c1.draw(pc)         # draw data

px=array(inputX, 'i')  
py=array(inputY, 'i') 
pz=array(inputZ, 'i')
im.setInputValues(px,py,pz);
kfm.connectLayers(im);

# the result 
p2=P2D("SOM weights")
p2.setSymbolSize(5);
p2.setSymbolColor(Color.red);
 
i=0
while kfm.finishedLearning() == False:
    kfm.learn()
    if (i%50 ==0): 
             p2.clear()
             print "print  rate=",kfm.getLearningRate(), "Activation area=",kfm.getActivationArea(), "Elapsed time=",kfm.getElapsedTime()
             weights=kfm.getWeightValues()
             for j in range(mapSizeX*mapSizeX):
                       p2.add(weights[0][j], weights[1][j],weights[2][j])
             c1.draw(p2)
             c1.updateData()

    i=i+1
    
c1.update()


You see the box below because you did not login.