⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 project1.m

📁 backpropagation algortihm for fisher data
💻 M
字号:
close all;
clear all;
load fisher.mat
trellisplot2(x,y,4)
x;
y'
[m,n]=size(x);
d=size(y);
r1=1;
r2=1;
r3=1;
meanx(1,1)=mean(x(1,:));
meanx(1,2)=mean(x(2,:));
meanx(1,3)=mean(x(3,:));
meanx(1,4)=mean(x(4,:));
%%%%%%%%%%%%%%%%%%%%%%%Standarising%%%%%%%%%%%%%%%%%55555
 for i=1:150
    stdx(1,i)=(x(1,i)-meanx(1,1))/sqrt(var(x(1,:)));
    stdx(2,i)=(x(2,i)-meanx(1,2))/sqrt(var(x(2,:)));
    stdx(3,i)=(x(3,i)-meanx(1,3))/sqrt(var(x(3,:)));
    stdx(4,i)=(x(4,i)-meanx(1,4))/sqrt(var(x(4,:)));
 end
 figure, trellisplot2(stdx,y,4)
%%%%%%%%%%%%%%%%%SVD and PC scores%%%%%%%%%%%%%%%%%
[u,d,v]=svd(stdx, 0);
pcastdx= u'*stdx
v1=var(pcastdx(1,:))
v2=var(pcastdx(2,:))
v3=var(pcastdx(3,:))
v4=var(pcastdx(4,:))
total_pca_var = v1+v2+v3+v4;

%%%%%%%%%%%%%%%%%%%%%%%%% %VAF %%%%%%%%%%%%%%%%%%%%%%
for i=1:4
VAF_pca(i) = var(pcastdx(i,:))/total_pca_var;
VAF_orig(i) = var(stdx(i,:))/(var(stdx(1,:))+var(stdx(2,:))+var(stdx(3,:))+var(stdx(4,:)));
end
figure,plot(VAF_pca)
hold on;
plot(VAF_orig,'red')
hold off;

%%%%%%%%%%%%%%%neural network design for training%%%%%%%%%%%%%%%%%%%%
Wnet1 = unifrnd(-0.5, 0.5, 2, 3)
Wnet2 = unifrnd(-0.57,0.57,3,3)
jw=0;
newjw = 0
tk=1.2;
newWnet1=Wnet1;
newWnet2=Wnet2;
Wjo=[0.5 0.45 -0.5];
% Wjo = [0.5 0.2 0]
Wko=[0.5 0.5 0.5];
eta=0.1;
traindiff=.2;
ji=0;
while (traindiff < 0.9)
    %ji=ji+1;
    iy=0;
    ji=ji+1;
    for j=1:50
        iy=iy+1;
        i = ceil(unifrnd(0,150));
        realclass(iy)=y(i);
        %realclass(iy)=y(j);
        % if(newjw-jw > 0.4)
        jw(ji)=newjw;
        ji=ji+1;
        Wnet1=newWnet1;
        Wnet2=newWnet2;
        inputx = [pcastdx(1,i) pcastdx(2,i)];
        gj(1) = (Wnet1(1,1)*pcastdx(1,i))+(Wnet1(2,1)*pcastdx(2,i))+Wjo(1);
        gj(2) = (Wnet1(1,2)*pcastdx(1,i))+(Wnet1(2,2)*pcastdx(2,i))+Wjo(2);
        gj(3) = (Wnet1(1,3)*pcastdx(1,i))+(Wnet1(2,3)*pcastdx(2,i))+Wjo(3);
        for ia=1:3
            gjactive(ia) = 1.716*tanh(2*gj(ia)/3);
        end
        gk(1) = (Wnet2(1,1)*gjactive(1))+(Wnet2(2,1)*gjactive(2))+(Wnet2(3,1)*gjactive(3))+Wko(1);
        gk(2) = (Wnet2(1,2)*gjactive(1))+(Wnet2(2,2)*gjactive(2))+(Wnet2(3,2)*gjactive(3))+Wko(2);
        gk(3) = (Wnet2(1,3)*gjactive(1))+(Wnet2(2,3)*gjactive(2))+(Wnet2(3,3)*gjactive(3))+Wko(3);
        for ia=1:3
            Zk(ia) = 1.716*tanh(2*gk(ia)/3);
        end
        for ia=1:3
            newWnet2(:,ia)= Wnet2(:,ia) + eta*((tk-Zk(y(i))))*(1-(tanh((2/3)*gk(ia))*tanh((2/3)*gk(ia))))*1.716*2*gjactive'/3;
        end
        for ia=1:3
            temp1=0;
            for ja=1:3
                temp1 =temp1+ eta*(Wnet2(ia,ja)*((tk-Zk(y(i))))*(1-(tanh((2/3)*gk(ja))*tanh((2/3)*gk(ja))))*1.716*(2/3));
            end
            newWnet1(:,ia)= Wnet1(:,ia) + temp1*((1-(tanh((2/3)*gj(ia))*tanh((2/3)*gj(ia)))))*1.716*(2/3)*inputx';
        end 
        newjw = 0.5*((tk-Zk(y(i)))^2);
        
        if(Zk(1)>0 && Zk(2)<0 && Zk(3)<0)
            trainclass(iy)=1;
        elseif(Zk(3)>0 && Zk(2)<0 && Zk(1)<0)
            trainclass(iy)=3;
        else
            trainclass(iy)=2;
        end
        
    end
    
    match =0;
    for pr =1:iy
        if (realclass(pr)==trainclass(pr))
            match=match+1;
        end
    end
    match;
    iy;
    traindiff = match/iy
end
figure,plot(jw);

%%%%%%%%%%%%%%%%%%%%%%%%Testing entire data WITH 2 PC scores %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
gjactive=[0 0 0];
Zk=[0 0 0];
for i=1:150
    gj(1) = (Wnet1(1,1)*pcastdx(1,i))+(Wnet1(2,1)*pcastdx(2,i))+Wjo(1);
    gj(2) = (Wnet1(1,2)*pcastdx(1,i))+(Wnet1(2,2)*pcastdx(2,i))+Wjo(2);
    gj(3) = (Wnet1(1,3)*pcastdx(1,i))+(Wnet1(2,3)*pcastdx(2,i))+Wjo(3);
    for ia=1:3
    gjactive(ia) = 1.716*tanh(2*gj(ia)/3);
    end
    gk(1) = (Wnet2(1,1)*gjactive(1))+(Wnet2(2,1)*gjactive(2))+(Wnet2(3,1)*gjactive(3))+Wko(1);
    gk(2) = (Wnet2(1,2)*gjactive(1))+(Wnet2(2,2)*gjactive(2))+(Wnet2(3,2)*gjactive(3))+Wko(2);
    gk(3) = (Wnet2(1,3)*gjactive(1))+(Wnet2(2,3)*gjactive(2))+(Wnet2(3,3)*gjactive(3))+Wko(3);
    for ia=1:3
    Zk(ia) = 1.716*tanh(2*gk(ia)/3);
    end
    if(Zk(1)>0 && Zk(2)<Zk(1) && Zk(3)<Zk(1))
        newy(i)=1;
    elseif(Zk(3)>0 && Zk(1)<=0 && Zk(2)<=0)
        newy(i)=3;
    else(Zk(2)>0)
        newy(i)=2;
    end
end

diff=0;
for i=1:150
    if (newy(i)==y(i))
        diff = diff+(1/150);
    else
        newy(i)=4;
    end
end
diff
xtz=[y newy']
newpcax(1,:)=pcastdx(1,:);
newpcax(2,:)=pcastdx(2,:);
y1=0;
y2=0;
y3=0;
y4=0;
%%%%%%%%%%scatter plot with decission boundaries and trellisplot for test results%%%%%%%%%%%%
figure,scatter(pcastdx(1,:),pcastdx(2,:),7,newy')
slope1 = -(Wnet1(1,1)/Wnet1(2,1));
C1 = -(Wjo(1)/Wnet1(2,1));
y = slope1*pcastdx(1,:) + C1;
line1 = line(pcastdx(1,:),y);
slope2 = -(Wnet1(1,2)/Wnet1(2,2));
C2 = -(Wjo(2)/Wnet1(2,2));
y = slope2*pcastdx(1,:) + C2;
line2 = line(pcastdx(1,:),y);
slope3 = -(Wnet2(1,3)/Wnet2(2,3));
C3 = -(Wjo(3)/Wnet2(2,3));
y = slope3*pcastdx(1,:) + C3;
line3 = line(pcastdx(1,:),y);
figure,trellisplot2(newpcax,newy',2)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -