📄 hw_without.m
字号:
%% HW of neural network
randn('state',0);
s=randn(250,8)*(1.5^0.5); %% Distribute the 1000 point in the coordinate system.
class4=[3+s(:,1),3+s(:,2)];
class3=[3+s(:,3),(-3)+s(:,4)];
class2=[-3+s(:,5),(-3)+s(:,6)];
class1=[(-3)+s(:,7),3+s(:,8)];
plot(class4(:,1),class4(:,2),'rx'); hold on;
plot(class3(:,1),class3(:,2),'g*');
plot(class2(:,1),class2(:,2),'bpentagram');
plot(class1(:,1),class1(:,2),'ko');
h=legend('class1','class2','class3','class4',-1);
set(h,'box','off')
%%-------------------- training without shuffling
trainings=[class4(1:10,1),class4(1:10,2),ones(10,1);class3(1:10,1),class3(1:10,2),ones(10,1);...
class2(1:10,1),class2(1:10,2),ones(10,1);class1(1:10,1),class1(1:10,2),ones(10,1)];
A=[0;0;0;1]; B=[0;0;1;0]; C=[0;1;0;0]; D=[1;0;0;0];
t1=repmat(A,[1 10]); t2=repmat(B,[1 10]); t3=repmat(C,[1 10]); t4=repmat(D,[1 10]);...
t5=repmat(A,[1 240]); t6=repmat(B,[1 240]); t7=repmat(C,[1 240]); t8=repmat(D,[1 240]);
T=[t1,t2,t3,t4]; T2=[t5,t6,t7,t8];
tests=[class4(11:end,1),class4(11:end,2),ones(240,1);class3(11:end,1),class3(11:end,2),ones(240,1);...
class2(11:end,1),class2(11:end,2),ones(240,1);class1(11:end,1),class2(11:end,2),ones(240,1)];
a=0.001; wold=2*ones(4,3);
w=ones(4,3);
epoch=100;
k=0;
dw=zeros(4*epoch,3);
xw=zeros(4,40);
for k=1:epoch
for i=1:40
w=w+a*(T(:,i)-w*trainings(i,:)')*trainings(i,:);
xw(:,i)=w*trainings(i,:)';
end
dw(k)=norm(w-wold);
wold=w;
k=k+1;
if norm(w-wold)>1e-5
break
end
end
figure,plot(dw(1:20),'-r','linewidth',2);
xlabel('number of epochs'); ylabel('change of weights'); title('learning curve without shuffling')
xw=round(xw);
count1=0;
for y=1:40
if xw(:,y)==T(:,y)
count1=count1+1;
end
end
cw=zeros(4,960);
cw=w*tests';
cw=round(cw);
count2=0;
for c=1:960
if cw(:,c)==T2(:,c)
count2=count2+1;
end
end
ar1=count1/40
ar2=count2/960
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -