BP网络练习

利用BP算法及Sigmoid函数,研究函数的逼近问题:

f(x)=1/x,  1<=x<=100;

BP神经网络:

clc;close all;clear;
N = 7;
P = rand(1,N);
P = P*100
T = P.^(-1)
w1 = rand(1,2);
w2 = rand(2,1);
E = 0;
Eta = 0.5;
epoch = 0;
while (epoch==0||0.5*(1/N)*E>0.00001)  
    E = 0;
    for n=1:N
            v1 = (w1')*P(n);                 
            y1 = (exp(-1*v1)+1).^(-1);
            v2 = (w2')*y1;
            O = (exp(-1*v2)+1).^(-1);
            e=T(n)-O;
            E=E+0.5*(e.^2);
            Delta2 = e*O*(1-O);
            w2 = w2+Eta*Delta2*y1;
            Delta1(1,1) = y1(1,1)*(1-y1(1,1))*([1 1]*(Delta2*w2));
            Delta1(2,1) = y1(2,1)*(1-y1(2,1))*([1 1]*(Delta2*w2));
            w1(1,1) = w1(1,1)+Eta*Delta1(1,1)*P(n);
            w1(1,2) = w1(1,2)+Eta*Delta1(2,1)*P(n);
            epoch = epoch+1;
    end
end
e
E
epoch
w1
w2

仿真:

function bpsim(w1,w2)
P = 0:1:100;
T = P.^(-1);
v1 = (w1')*P;                 
y1 = (exp(-1*v1)+1).^(-1);
v2 = (w2')*y1;
O = (exp(-1*v2)+1).^(-1);
hold;
plot(P,T,'r');
plot(P,O,'b');

仿真结果:

P =

   33.1665   15.2234   34.8008   12.1658   88.4153    9.4278   93.0041

T =

    0.0302    0.0657    0.0287    0.0822    0.0113    0.1061    0.0108

e =

   -0.0080

E =

  1.4000e-004

epoch =

      958895

w1 =

    0.0456   -0.3060

w2 =

   -4.0149
    3.8677


阅读更多
个人分类: MATLAB 神经网络
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭