clear
%Receive input
firstLayer_Neural_Num=input('This is the 1st layer.How many neurals do you want to use? ');
secondLayer_Neural_Num=input('This is the 2nd layer.How many neurals do you want to use? ');
thirdLayer_Neural_Num=input('This is the 3rd layer.How many neurals do you want to use? ');
%Initialize W and V
V=rands(firstLayer_Neural_Num,secondLayer_Neural_Num)-0.5;
W=rands(secondLayer_Neural_Num,thirdLayer_Neural_Num)-0.5;
V_copy=V;
W_copy=W;
%Initialize X and Y
X=[0,0;0,1;1,0];
Y=[0.6;1;1];
[x_rows x_cols]=size(X);
[y_rows y_cols]=size(Y);
sampNum=x_rows;
outputMension=y_cols;
%Initialize Temperature
Temperature=10;
%Begin to train
while Temperature>0.9
for sampIndex=1:sampNum
pre_energy=return_energy(X(sampIndex,:),Y(sampIndex,:),V,W);
%initialize p and r
flag=1;
%pre_adjustWeight is a 1*4 matrix,which means[state,row,col,value]
%state means if it comes from V ,the state equals 0. Also,if it
% comes from W, the state equals 1.
while flag==1
pre_adjustWeight=generate_randWeight(V,W);
adjust_rows=pre_adjustWeight(2);
adjust_cols=pre_adjustWeight(3);
adjust_val=pre_adjustWeight(4);
%Adjust pre_adjustWeight
Delta_weight=adjust_randWeight(Temperature);
if pre_adjustWeight(1)==0
V_copy(adjust_rows,adjust_cols)=adjust_val+Delta_weight;
else
W_copy(adjust_rows,adjust_cols)=adjust_val+Delta_weight;
end
pro_energy=return_energy(X,Y,V_copy,W_copy);
Delta_energy=pro_energy-pre_energy;
if Delta_energy>0
r=rand(1);
p=Temperature/(Temperature^2+pro_energy^2);
if p
flag=0;
end
end
end
if pre_adjustWeight(1)==0
V(adjust_rows,adjust_cols)=adjust_val+Delta_weight;
else
W(adjust_rows,adjust_cols)=adjust_val+Delta_weight;
end
end
Temperature=0.9*Temperature;
end