Я должен реализовать алгоритм Кохонена в 4 * 4 измерения.Это работает, но у меня есть небольшая проблема, потому что я получаю эту ошибку:
######################################
## Initialize random matrix phi/psi ##
######################################
##Pour mac
setwd("/Users/amandinelecerfdefer/Desktop/Kohonen_MAP")
data_phipsi<-read.csv("/Users/amandinelecerfdefer/Desktop/Kohonen_MAP/pbA.txt.new.2.rep30.phipsi",h=F)
colnames(data_phipsi)<-c("phi1","psi2","phy2","psi3","phy3","psi4","phy4","psi5")
random_list<-list()
borne<-16
for(n in 1:borne){
random_list[[n]]<-floor(runif(8,min=-180,max=180))
}
# ##############Print random matrix#####
# for(n in 1:16){
# print(random_list[[n]])
# }
#creation of a matrix 4*4 representing the Kohonen map containing the vectors of the angles phi/psi
Kohonen_matrix<-matrix(random_list,ncol=sqrt(borne),nrow=sqrt(borne))
rownames(Kohonen_matrix)<-c('1','2','3','4')
colnames(Kohonen_matrix)<-c('1','2','3','4')
#############################################
##Function for distance calculation (RMSDA)##
#############################################
RMSDA<-function(data_phipsi,Kohonen_matrix)
{
difference<-data_phipsi-Kohonen_matrix
for(j in 1:length(difference)){
if (difference[j]< -180) {difference[j]=difference[j]+360}
if (difference[j]> +180) {difference[j]=difference[j]-360}
}
distance=mean(sqrt(difference^2))
return(distance)
}
#######################
##LEARNING FUNCTION ##
#######################
learning<-function(initial_rate,iteration,data_phipsi){
return(initial_rate/(1+(iteration/nrow(data_phipsi))))
}
##############################
## Program for Kohonen MAp ###
##############################
initial_rate=0.75 # Decrease with training time
initial_radius=2 # Decrease with training time
iteration=3
for(step in 1:iteration)
{
data_phipsi<-data_phipsi[sample(nrow(data_phipsi)),] # Sample vectors of training (samples of lines of the dataframe)
print(step) #Visualize where we are in loops
for(k_row in 1:nrow(data_phipsi))
{
#Update learn_rate and radius at each row of each iteration
learn_rate<-learning(initial_rate,((step-1)*nrow(data_phipsi))+k_row,data_phipsi)
learn_radius<-learning(initial_rate,((step-1)*nrow(data_phipsi))+k_row,data_phipsi)
#Find distance between each vectors of angles of Kohonen Map and the training vector
phipsi_RMSDA<-lapply(random_list, RMSDA, data_phipsi=data_phipsi[k_row,])
#Unlist and create a matrix of distances
vector_RMSDA<-unlist(phipsi_RMSDA)
matrix_RMSDA<-matrix(vector_RMSDA,sqrt(borne),sqrt(borne))
#Take the index of the winning neuron, it's the neuron that are more similar than the training vector
win_index<-which(matrix_RMSDA==min(matrix_RMSDA), arr.ind=TRUE)
current_index<-list(c(1,1),c(2,1),c(3,1), c(4,1), c(1,2),c(2,2),c(3,2), c(4,2),c(1,3),c(2,3),c(3,3), c(4,3), c(1,4),c(2,4),c(3,4), c(4,4), c(1,4),c(2,4),c(3,4), c(4,4))
#Update of angles of Kohonen Map vectors with the equation (be careful at number of parenthesis)
for (mlist in 1:borne)
{
distance<-as.numeric(dist(rbind(win_index[1,], current_index[[mlist]])))
random_list[[mlist]]<-(random_list[[mlist]]+ ( prot_phipsi[i_row,]-random_list[[mlist]])* (learn_rate*( exp (- ((distance)^2/(2*((learn_radius)^2)) )) ) ))
}
Kohonen_matrix<-matrix(random_list,sqrt(borne),sqrt(borne))
}
}
После запуска этого кода я получаю ошибку:
[1] 1 Ошибка вwin_index [1,]: индекс за пределами. Дополнительно: было 16 предупреждений (используйте предупреждения (), чтобы увидеть их)
Можете ли вы помочь мне решить эту проблему?
Спасибо,Спасибо.