Self Organizing Map isn't working perfectly, same class always as output

可紊 提交于 2020-04-16 05:14:12

问题


I want to train and test Kohonen network which is a kind of (Self Organizing Maps).

My problem is that I get all the outputs with same values either 0000 or 1111 each time even though I'm using random weights matrix which will differ each time I'm running the code!

My data-set is 3 tiny text files on the link below: note that I'm using samples from my train data first to check if my code is correct before to use the test data.

data-sets link

#==============================================================
#Import necessary Libraries
#---------------------------
import random
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from Kohonen_Funcs import Train,Test
#=============================================================
# Reading Data
#=============================================================
patient = pd.read_fwf('patient.txt', header = None, delimiter="\t",keep_default_na=False)
control = pd.read_fwf('control.txt', header = None, delimiter="\t",keep_default_na=False)
#-------------------------------------------------------------
test = np.loadtxt('test_dud_ten.txt', delimiter="\t",dtype = str,max_rows=4)
#xt = test[:,0:650].astype(float)
#-------------------------------------------------------------
#=============================================================
# convert Data into Arrays to deal with.
#=============================================================
xp = np.array(patient,dtype = float)
xp = np.roll(xp, 10,axis = 1) # shift data on time axis by 10 to be aligned

xc = np.array(control,dtype = float)
xt = np.vstack((xp[0:2,:],xc[0:2,:]))
#-------------------------------------------------------------
#=========================
# Initial Parameters:
#=========================
Alpha  = 0.6 # Learning Ratio
W = np.random.random((2,650))# Weights random Array 2 Rows 650 Columns
iter = 50 # Number of iterations 
#print(W,'\n')
#========================
# Training
#========================
W_Tr , t_used = Train(xp,xc,W,Alpha,iter)
#print(W_Tr)
#------------------------------------
#========================
# Testing
#========================
Result = Test(xt,W_Tr)
print(Result)
#------------------------------------

And here is The Functions that I'm using:

#==============================================================
#Import necessary Libraries
#---------------------------
import matplotlib.pyplot as plt
import numpy as np
import time
#=============================================================
def winner(dist):            # dist : 2 x 650 array
    D = np.sum(dist,axis=1)  # sum all values on time axis
    first_w  = D[0]
    second_w = D[1]
    if first_w < second_w: # if first w was closer (shorter distance)
        return 0 
    else:
        return 1 
#------------------------------------

#=============================================================
def Train(x1,x2,Wr,a,iterations):
    tic = time.time() # set a timer
    subjects_range = int(2*x1.shape[0]) # 20
    #--------------------------------------
    x1 = np.vstack((x1,x1)) # 20x650
    # Rearrange the array to make each group of 2 rows is similar
    x1 = x1[np.ix_([0,10,1,11,2,12,3,13,4,14,5,15,6,16,7,17,8,18,9,19])]
    #-------------------------------------------------------------------
    x2 = np.vstack((x2,x2)) # 20x650
    # Rearrange the array to make each group of 2 rows is similar
    x2 = x2[np.ix_([0,10,1,11,2,12,3,13,4,14,5,15,6,16,7,17,8,18,9,19])]
    #--------------------------------------
    Dist1 = Dist2 = np.zeros_like(Wr)
    for epoch in range(iterations):
        for subject in range(0,subjects_range,2):
            #-----------------( Dist : 2 x 20 )-----------------------
            # Patient subjects
            Dist1 = (Wr - x1[subject:subject+2,:])**2
            win1 = winner(Dist1)
            Wr[win1,:]+= a*(x1[subject,:]-Wr[win1,:]) # W1 = a * (X1-W1)
            #---------------------------------------------------------
            # Control subjects
            Dist2 = (Wr - x2[subject:subject+2,:])**2
            win2 = winner(Dist2)
            Wr[win2,:]+= a*(x2[subject,:]-Wr[win2,:]) # W2 = a * (X2-W2)
            #---------------------------------------------------------    
        a *= 0.5 # update Learning Ratio after each epoch
    #===============================
    toc = time.time() # reset the timer, and get time used
    t_used = toc - tic
    return Wr , t_used
#------------------------------------
#=============================================================
def Test(test,W):
    output = [] # list to store the output 
    subjects_range = int(2*test.shape[0]) # 8 
    xt = np.vstack((test,test)) # 8 x 650
    # Rearrange the array to make each group of 2 rows is similar
    xt = xt[np.ix_([0,4,1,5,2,6,3,7])]
    dist = np.zeros_like(xt) # 8 x 650
    for subject in range(0,subjects_range,2):
        # for each subject calculate distance
        dist[subject:subject+2,:] = (xt[subject:subject+2,:] - W)**2
        # for each subject get to which class it belongs
        win = winner(dist[subject:subject+2,:])
        print(subject,'win = ',win)
        output.append(win)
    return output

回答1:


The whole problem was in the weights, since they are initialized randomly, there is no guarantee that the result will be correct, instead of W = np.random.random((2,650)) I Initialized the weights manually, and got a correct results.



来源:https://stackoverflow.com/questions/60360517/self-organizing-map-isnt-working-perfectly-same-class-always-as-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!