maltab 人工智能程序代业辅导讲解、辅导
- 首页 >> Matlab编程clc
clear
c1 = textread('剪刀.txt');%1
c2 = textread('石头.txt');%2
c3 = textread('布.txt');%3
c4 = textread('测试剪刀.txt');
data(1:500,:)=c1(21:520,1:9);
data(501:1000,:)=c2(21:520,1:9);
data(1001:1500,:)=c3(21:520,1:9);
data(1501:2000,:)=c4(21:520,1:9);
input=data(:,2:9);
output1 =data(:,1);
for i=1:2000
switch output1(i)
case 1
output(i,:)=[1 0 0 ];
case 2
output(i,:)=[0 1 0 ];
case 3
output(i,:)=[0 0 1 ];
end
end
input_train=input(1:1500,:)';
output_train=output(1:1500,:)';
input_test=input(1501:2000,:)';
output_test=output(1501:2000,:)';
[inputn,inputps]=mapminmax(input_train);
innum=8;
midnum=9;
outnum=3;
w1=rands(midnum,innum);
b1=rands(midnum,1);
w2=rands(midnum,outnum);
b2=rands(outnum,1);
w2_1=w2;w2_2=w2_1;
w1_1=w1;w1_2=w1_1;
b1_1=b1;b1_2=b1_1;
b2_1=b2;b2_2=b2_1;
xite=0.1
alfa=0.01;
for ii=1:10
E(ii)=0;
for i=1:1:1500
x=inputn(:,i);
for j=1:1:midnum
I(j)=inputn(:,i)'*w1(j,:)'+b1(j);
Iout(j)=1/(1+exp(-I(j)));
end
yn=w2'*Iout'+b2;
e=output_train(:,i)-yn;
E(ii)=E(ii)+sum(abs(e));
dw2=e*Iout;
db2=e';
for j=1:1:midnum
S=1/(1+exp(-I(j)));
FI(j)=S*(1-S);
end
for k=1:1:innum
for j=1:1:midnum
dw1(k,j)=FI(j)*x(k)*(e(1)*w2(j,1)+e(2)*w2(j,2)+e(3)*w2(j,3));
db1(j)=FI(j)*(e(1)*w2(j,1)+e(2)*w2(j,2)+e(3)*w2(j,3));
end
end
w1=w1_1+xite*dw1';
b1=b1_1+xite*db1';
w2=w2_1+xite*dw2';
b2=b2_1+xite*db2';
w1_2=w1_1;w1_1=w1;
w2_2=w2_1;w2_1=w2;
b1_2=b1_1;b1_1=b1;
b2_2=b2_1;b2_1=b2;
end
end
inputn_test=mapminmax('apply',input_test,inputps);
for ii=1:1
for i=1:500%1500
%隐含层输出
for j=1:1:midnum
I(j)=inputn_test(:,i)'*w1(j,:)'+b1(j);
Iout(j)=1/(1+exp(-I(j)));
end
fore(:,i)=w2'*Iout'+b2;
end
end
sum = 0;
for i=1:500
output_fore(i)=find(fore(:,i)==max(fore(:,i)));
sum = sum+output_fore(i);
end
result_judge = sum/500;