cn

clear;clc
clear all


%%%%%%%%%%%%%
l1_num = 3;
l2_num = 5;
l3_num = 4;
l4_num = 3;

b1 = 0.2;
b2 = 0.1;
b3 = 0.2;

lr = 0.1;

%%%%%%%%%%%%%
train_data = [1 2 3; 40 50 60; 70 80 90];
train_label = [1 0 0; 0 1 0; 0 0 1];

W1 = rand(l2_num, l1_num);
W2 = rand(l3_num, l2_num);
W3 = rand(l4_num, l3_num);

for iteration=1:1000
    for p=1:3
        X = train_data(p, :)';
        
        %前向传播
        net_L2 = W1 * X + b1;
        sigmod_L2 = 1 / ( 1 + exp(-net_L2) );
        L2 = sigmod_L2';
        
        net_L3 = W2 * L2 + b2;
        sigmod_L3 = 1 / ( 1 + exp(-net_L3) );
        L3 = sigmod_L3';
        
        net_Y = W3 * L3 + b3;
        sigmod_Y = 1 / ( 1 + exp(-net_Y) );
        Y = sigmod_Y';
        
        %求解误差
        e1 = 1/2 * (Y(1) - train_label(p,1))^2;
        e2 = 1/2 * (Y(2) - train_label(p,2))^2;
        e3 = 1/2 * (Y(3) - train_label(p,3))^2;
        etotal = e1 + e2 + e3
        
        %复合偏导数
        pd1 = (Y(1) - train_label(p));
        pd2 = (Y(1) * (1 - Y(1)));
        pd3 = L3(1);
        pd = pd1*pd2*pd3;
        
        %%%%%%%%%%%%%%%%%
        for i=1:l4_num %3
            for j=1:l3_num %4
                
                pd1 = (Y(i) - train_label(p));
                pd2 = (Y(i) * (1 - Y(i)));
                pd3 = L3(j);
                pd = pd1*pd2*pd3;
                
                W3(i,j) = W3(i,j) - lr * pd;
            end
        end
        
        %%%%%%%%%%%%%%%%%
        for i=1:l3_num %3
            for j=1:l2_num
                
                pd1 = (L3(i) - train_label(p));
                pd2 = (L3(i) * (1 - L3(i)));
                pd3 = L2(j);
                pd = pd1*pd2*pd3;
                
                W2(i,j) = W2(i,j) - lr * pd;
            end
        end
        
        
        %%%%%%%%%%%%%%%%%
        for i=1:l2_num %3
            for j=1:l1_num
                
                pd1 = (L2(i) - train_label(p));
                pd2 = (L2(i) * (1 - L2(i)));
                pd3 = X(j);
                pd = pd1*pd2*pd3;
                
                W1(i,j) = W1(i,j) - lr * pd;
            end
        end
        
        
    end
end



猜你喜欢

转载自blog.csdn.net/zlf19910726/article/details/80428005
cn
今日推荐