demo httm
Post on 10-Oct-2014
60 Views
Preview:
TRANSCRIPT
Demo mạng Neural
Lê Trung Kiên ĐKTĐ2 K53 Page 1
% Demo view(net) % New Network Functions
net = perceptron; view(net); pause
net = linearlayer; view(net); pause
% ------------------------------------------- net = cascadeforwardnet; view(net); pause
Demo mạng Neural
Lê Trung Kiên ĐKTĐ2 K53 Page 2
net = feedforwardnet; view(net); pause
net = feedforwardnet([5 3]); view(net); pause
% -------------------------------------------------------
net = timedelaynet(0:3,[5 4]); view(net); pause
Demo mạng Neural
Lê Trung Kiên ĐKTĐ2 K53 Page 3
net = distdelaynet({0:2,1:2,1:3},[5 4]); view(net); pause
% ------------------------------------------------------- net = layrecnet(1:3,[5 4]); view(net); pause
net = elmannet; view(net); pause
net = elmannet(1:2,[5 3]); view(net); pause
Demo mạng Neural
Lê Trung Kiên ĐKTĐ2 K53 Page 4
% ------------------------------------------ net = narnet; view(net); pause
net =
net = narxnet; view(net); pause
net = closeloop(net); view(net)
Demo mạng Neural
Lê Trung Kiên ĐKTĐ2 K53 Page 5
function demo_network
net = network; net.numInputs = 2; net.numLayers = 3; % Connections net.biasConnect = [1 0 1]'; net.inputConnect = [1 0;1 1;0 0]; net.layerConnect = [0 0 0;0 0 0;1 1 1]; net.outputConnect= [0 0 1]; % Input range net.inputs{1}.range = [0 10;0 10]; net.inputs{2}.range = [-2 2;-2 2;-2 2;-2 2;-2 2]; % Layer size,transferFcn and initFcn net.layers{1}.size = 4; net.layers{1}.transferFcn = 'tansig'; net.layers{1}.initFcn = 'initnw'; net.layers{2}.size = 3; net.layers{2}.transferFcn = 'logsig'; net.layers{2}.initFcn = 'initnw'; net.layers{3}.size = 1; net.layers{3}.transferFcn = 'purelin'; net.layers{3}.initFcn = 'initnw'; % TapDelayLines for inputWeights and layerWeights net.inputWeights{1,1}.delays = [0 1] ; net.inputWeights{2,2}.delays = 1 ; net.layerWeights{3,3}.delays = 1 ; % Network Functions net.initFcn = 'initlay'; net.performFcn = 'mse'; net.trainFcn = 'trainlm'; % Initialize network net = init(net); % Train network P = {[0;0],[2;.5];[2;-2;1;0;1],[-1;-1;1;0;1]}; T = {1 -1}; net.trainParam.epochs = 100; net.trainParam.goal = 1e-10; net = train(net,P,T); view(net) Y = sim(net,P); display(Y)
Demo mạng Neural
Lê Trung Kiên ĐKTĐ2 K53 Page 6
% Demo : Perceptron
p = [-5 2 -5 5 5 0 8 10 10 -2 -5 8 -5 -9 0 -10]; t = [1 1 1 1 0 0 0 0]; net = newp(minmax(p),1); plotpv(p,t); grid pause net=perceptron; net=configure(net,p,t); view(net) pause
net.iw{1,1} = [-2 -1]; net.b{1} = 0; h = plotpc(net.iw{1,1},net.b{1}); pause
e=1; while (sse(e)) [net,a,e] = adapt(net,p,t); h = plotpc(net.iw{1,1},net.b{1},h); pause drawnow end
% ------------------------------------------------------------------------ % Demo Adaline % Adaptive Filter FIR
A = [1 zeros(1,20)]; B = 10*rand(1,21); H = tf(B,A,-1);% tin hieu so ko biet thoi gian trich mau set(H,'variable','z^-1');% bieu dien H theo Z^-1 display(H) [y,t] = impulse(H);% gan [y,t] cho dap ung xung cua H plot(t,y) pause
P = zeros(size(t')); P(1) = 1; P = con2seq(P); % P:impulse input (chuyen qua dang phu thuoc thoi gian cell) T = con2seq(y');% T:impulse response net = linearlayer(0:20);%tre dau vao net.biases{1}.learnParam.lr = 0; net = train(net,P,T); display(net.iw{1,1}) display(net.b{1})
Y = net(P); plotresponse(T,Y)
Demo mạng Neural
Lê Trung Kiên ĐKTĐ2 K53 Page 7
% ------------------------------------------------------------------------ % Demo Adaline % Predictive filter
t = 0:0.05:5; t1 = t+0.05; P = exp(-0.3*t).*sin(4*pi*t); T = exp(-0.3*t1).*sin(4*pi*t1); plot(t,[P' T']) title({'Data for Training :';'Input:Blue Target:Green'}) P = con2seq(P); T = con2seq(T); pause
net = linearlayer(1:6,0.1); view(net) [Ps,Pi,~,Ts] = preparets(net,P,T); net = train(net,Ps,Ts,Pi); Y = net(Ps,Pi); plot([cell2mat(Ps)' cell2mat(Y)' cell2mat(Ts)']) title({'Training :';'Input:Blue Output:Green Target:Red'}) pause
net2 = removedelay(net); view(net2) [Ps,Pi,~,Ts] = preparets(net2,P,T); Y = net2(Ps,Pi); plot([cell2mat(Ps)' cell2mat(Y)' cell2mat(Ts)']) title({'Remove Delay :';'Input:Blue Output:Green Target:Red'}) pause
% Check Predictive Filter t = 5.05:0.05:10; t1 = t+0.05; Pchk = exp(-0.3*t).*sin(4*pi*t); Tchk = exp(-0.3*t1).*sin(4*pi*t1); Pchk = con2seq(Pchk); Tchk = con2seq(Tchk); [Ps,Pi,~,Ts] = preparets(net2,Pchk,Tchk); Y = net2(Ps,Pi); plot([cell2mat(Ps)' cell2mat(Y)' cell2mat(Ts)']) title({'Checking :';'Input:blue Output:Green Target:red'}) figure plotresponse(Ts,Y)
% ------------------------------------------------------------------------ % Demo : Backpropagation Feed Forward Network (newff)
[P,T] = simplefit_dataset; net=newff(P,T,20); net.divideFcn = ''; net=train(net,P,T); Y = net(P); plot(P',[T' Y' (T-Y)']) legend({'Target','Output','Error'})
Demo mạng Neural
Lê Trung Kiên ĐKTĐ2 K53 Page 8
% ------------------------------------------------------------------------ % Demo feedforwardnet % Backpropagation Neural Network (feedforwardnet)
[X,Y] = meshgrid(-2:.2:2,-2:.2:2);% dau vao x y Z = X.*exp(-X.^2 -Y.^2);% dau ra surf(X,Y,Z)% ve 3D pause
p1 = X(:)';% dau vao 1 p2 = Y(:)';% dau vao 2 T = Z(:)';% target P = [p1;p2]; net = feedforwardnet(60);%mang feed forward 60 noron(cau truc ben trong) net = configure(net,P,T);% Dat cau hinh mang view(net) %-------------------------- net.trainParam.epochs=500;% mac dinh 500 lan, bt 1000 lan net.trainParam.goal=1e-8;% muc tieu sai so khoang 1e-8 net.trainParam.min_grad=1e-10;% gradient toi thieu % neu khong khai bao => lay mac dinh net=train(net,P,T); a = sim(net,P);%???? [r,c] = size(-2:.2:2); A = reshape(a,[c,c]); figure surf(X,Y,A) pause figure surf(X,Y,Z-A)% mat cong sai so
Demo mạng Neural
Lê Trung Kiên ĐKTĐ2 K53 Page 9
% Demo Static and Dynamic
Networks
P = {0 0 1 1 1 1 0 0 0 0 0 0}; stem(cell2mat(P))
net = linearlayer; net.inputs{1}.size = 1; net.layers{1}.dimensions = 1; net.biasConnect = 0; net.iw{1,1} = 2; view(net) Y = net(P); figure stem(cell2mat(Y))
net = linearlayer(0:3); net.inputs{1}.size = 1; net.layers{1}.dimensions = 1; net.biasConnect = 0; net.iw{1,1} = [1 1 1 1]; view(net) Y = net(P); figure stem(cell2mat(Y))
net = narxnet(0,1,[],'closed'); net.inputs{1}.size = 1; net.layers{1}.dimensions = 1; net.biasConnect = 0; net.iw{1,1} = 1; net.lw{1,1} = .8; view(net) Y = net(P); figure stem(cell2mat(Y))
Demo mạng Neural
Lê Trung Kiên ĐKTĐ2 K53 Page 10
% Demo timedelaynet % Predict a time-series, given its
past values % One-Step-Ahead Prediction
%y = simplenar_dataset; %y = laser_dataset; %y = y(200:400); [x,y] = simpleseries_dataset; net = timedelaynet(1:10,10); %Pi = y(1:2); %Ps = y(3:end); %Ts = y(3:end);
[Ps,Pi,~,Ts] = preparets(net,y,y); net.divideFcn = ''; %net.trainParam.epochs = 500; net.trainParam.min_grad = 1e-10; net = train(net,Ps,Ts,Pi); Y = net(Ps,Pi); plotresponse(Ts,Y)
net1 = removedelay(net); view(net);view(net1) [Ps,Pi] = preparets(net1,y); Y = net1(Ps,Pi); figure title('One-Step-Ahead Prediction') plot([cell2mat(Ps)' cell2mat(Y)']) legend({'Input','Output'}) pause axis([0 30 -.5 .5])
Demo mạng Neural
Lê Trung Kiên ĐKTĐ2 K53 Page 11
% Demo narnet % Predict a time-series, given its
past values % One-Step-Ahead Prediction
T = simplenar_dataset; net = narnet(1:2,10); [Xs,Xi,~,Ts]=preparets(net,{},{},T)
; %feedback target net = train(net,Xs,Ts,Xi); view(net) Y = net(Xs,Xi); plotresponse(Ts,Y) pause
net1 = removedelay(net); view(net1) [Xs,Xi,~,Ts]=preparets(net1,{},{},T
); Y = net1(Xs,Xi);
figure title('One-Step-Ahead Prediction') plot([cell2mat(Xs)' cell2mat(Y)']) legend({'Input','Output'}) pause axis([0 30 .2 1])
Demo mạng Neural
Lê Trung Kiên ĐKTĐ2 K53 Page 12
% Demo narxnet % Magnet Levitation dynamic system. % Predict the dynamic behavior of % a magnet(y),levitated by a control
current(u).
load magdata u = mapminmax(u); y = mapminmax(y); u = u(1:1000); y = y(1:1000); plot([u' y']) title('Magnet Levitation Data :') legend({'Input','Output'}) pause
% Open loop AR net : % 2 inputs : x(t) and associated
feedback y(t) % Delays : ID1=1:2 , ID2=1:2 % 1 output : y(t) net = narxnet(1:2,1:2,10); net.divideFcn = ''; net.trainParam.min_grad = 1e-10; u = con2seq(u); y = con2seq(y); [P,Pi,~,T] = preparets(net,u,{},y); net = train(net,P,T,Pi); view(net) Yp = net(P,Pi); plot([cell2mat(Yp)' cell2mat(T)']) legend({'Output','Target'}) pause
Demo mạng Neural
Lê Trung Kiên ĐKTĐ2 K53 Page 13
% Closed loop AR net : % 1 input : x(t) % Delays : ID=1:2 , LD=1:2 % 1 output : y(t) net1 = closeloop(net); view(net1) [P1,Pi1,Ai1,T1] =
preparets(net1,u,{},y); Yp1 = net1(P1,Pi1,Ai1); plot([cell2mat(Yp1)' cell2mat(T1)']) legend({'Output','Target'}) pause
% Train the closed loop net : net1.trainParam.epochs = 100; net1 = train(net1,P1,T1,Pi1,Ai1); Yp1 = net1(P1,Pi1,Ai1); plot([cell2mat(Yp1)' cell2mat(T1)']) legend({'Output','Target'}) pause
% Remove Input Delays : % 1 input : x(t) % Delays : ID=0:1 , LD=1:2 % 1 output : y(t+1) net2 = removedelay(net1); % Remove n=1
delay view(net2) [P2,Pi2,Ai2,T2] =
preparets(net2,u,{},y); Yp2 = net2(P2,Pi2,Ai2); y = y(1:998); plot([cell2mat(P2)' cell2mat(y)'
cell2mat(Yp2)']) legend({'Input x(t)','Output
y(t)','Output y(t+1)'})
top related