我想训练和测试科霍宁网络,这是一种自组织映射(Self Organizing Maps)。
我的问题是每次运行代码时,尽管使用了每次不同的随机权重矩阵,输出的值总是相同,不是0000就是1111!
我的数据集是下面链接中的3个小文本文件:请注意,我首先使用训练数据中的样本来检查我的代码是否正确,然后再使用测试数据。
#==============================================================#Import necessary Libraries#---------------------------import randomimport numpy as npimport pandas as pdimport matplotlib.pyplot as pltfrom 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 alignedxc = np.array(control,dtype = float)xt = np.vstack((xp[0:2,:],xc[0:2,:]))#-------------------------------------------------------------#=========================# Initial Parameters:#=========================Alpha = 0.6 # Learning RatioW = np.random.random((2,650))# Weights random Array 2 Rows 650 Columnsiter = 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)#------------------------------------
以下是我使用的函数:
#==============================================================#Import necessary Libraries#---------------------------import matplotlib.pyplot as pltimport numpy as npimport 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))
,我手动初始化了权重,得到了正确的结果。