自组织地图运行不正常,始终与输出相同类

编程入门 行业动态 更新时间:2024-10-28 08:24:40
本文介绍了自组织地图运行不正常,始终与输出相同类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想训练和测试 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.

更多推荐

自组织地图运行不正常,始终与输出相同类

本文发布于:2023-11-30 17:56:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650984.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不正常   组织   地图

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!