自组织地图运行不正常,始终与输出相同类 [英] Self Organizing Map isn't working perfectly, same class always as output
问题描述
我想训练和测试 Kohonen网络,这是一种(自组织地图).
I want to train and test Kohonen network which is a kind of (Self Organizing Maps).
我的问题是,即使我使用随机权重矩阵,每次获得的输出值都相同,无论是0000还是1111,每次运行代码时都会不同!
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!
我的数据集是下面链接上的3个小文本文件:请注意,在使用测试数据之前,我首先使用了火车数据中的样本来检查我的代码是否正确.
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.
#==============================================================
#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
推荐答案
整个问题出在权重上,因为它们是随机初始化的,所以不能保证结果是正确的,而不是W = np.random.random((2,650))
我初始化了手动称重,并获得正确的结果.
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.
这篇关于自组织地图运行不正常,始终与输出相同类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!