%RN Ndemo.m %Demonstrate RNN clear all close all DATA1=[]; DATA2=[]; for i=1:1:100 t=sort(rand(1,3)); subplot(2,1,1) DATA1=[DATA1;[t(2) t(1) t(3)]]; plot([t(2) t(1) t(3)]) hold on subplot(2,1,2) DATA2=[DATA2;[t(1) t(3) t(2)]]; plot([t(1) t(3) t(2)]) hold on end DATATRAIN=[DATA1(1:1:50,:); DATA2(1:1:50,:)]; DATATEST=[DATA1(51:1:100,:); DATA2(51:1:100,:)]; TARGETTRAIN=[ones(1,50) zeros(1,50)]; TARGETTEST=[ones(1,50) zeros(1,50)]; W=randn(2,2)*1; V=randn(3,1)*1; JCOL=[]; LR=0.05; for j=1:1:5000 for i=1:1:size(DATATRAIN,1) %Forward out0=0; h11=logsig(out0*W(1,1)+DATATRAIN(i,1)*W(2,1)); h21=logsig(out0*W(1,2)+DATATRAIN(i,1)*W(2,2)); out1=logsig(h11*V(1)+h21*V(2)+V(3)); h12=logsig(out1*W(1,1)+DATATRAIN(i,2)*W(2,1)); h22=logsig(out1*W(1,2)+DATATRAIN(i,2)*W(2,2)); out2=logsig(h12*V(1)+h22*V(2)+V(3)); h13=logsig(out2*W(1,1)+DATATRAIN(i,3)*W(2,1)); h23=logsig(out2*W(1,2)+DATATRAIN(i,3)*W(2,2)); out3=logsig(h13*V(1)+h23*V(2)+V(3)); %Backward gradatout3=TARGETTRAIN(i)-out3; V(1)=V(1)+LR*gradatout3*h13; V(2)=V(1)+LR*gradatout3*h23; V(3)=V(3)+LR*gradatout3; W(1,1)=W(1,1)+LR*gradatout3*V(1)*h13*(1-h13)*out2; W(1,2)=W(1,2)+LR*gradatout3*V(2)*h23*(1-h23)*out2; W(2,1)=W(2,1)+LR*gradatout3*V(1)*h13*(1-h13)* DATATRAIN(i,3); W(2,2)=W(2,2)+LR*gradatout3*V(2)*h23*(1-h23)* DATATRAIN(i,3); gradatout2=gradatout3*(V(1)*h13*(1-h13)*W(1,1)+ V(2)*h23*(1-h23)*W(1,2))*out2*(1-out2); V(1)=V(1)+LR*gradatout2*h12; V(2)=V(1)+LR*gradatout2*h22; V(3)=V(3)+LR*gradatout2; W(1,1)=W(1,1)+LR*gradatout2*V(1)*h12*(1-h12)*out1; W(1,2)=W(1,2)+LR*gradatout2*V(2)*h22*(1-h22)*out1; W(2,1)=W(2,1)+LR*gradatout2*V(1)*h12*(1-h12)* DATATRAIN(i,2); W(2,2)=W(2,2)+LR*gradatout2*V(2)*h22*(1-h22)* DATATRAIN(i,2); gradatout1=gradatout2*(V(1)*h12*(1-h12)*W(1,1)+V(2)*h22*(1-h22)*W(1,2))*out1*(1-out1); V(1)=V(1)+LR*gradatout1*h11; V(2)=V(1)+LR*gradatout1*h21; V(3)=V(3)+LR*gradatout1; W(1,1)=W(1,1)+LR*gradatout1*V(1)*h11*(1-h11)*out0; W(1,2)=W(1,2)+LR*gradatout1*V(2)*h21*(1-h21)*out0; W(2,1)=W(2,1)+LR*gradatout1*V(1)*h11*(1-h11)* DATATRAIN(i,1); W(2,2)=W(2,2)+LR*gradatout1*V(2)*h21*(1-h21)* DATATRAIN(i,1); end EST=[]; for i=1:1:size(DATATRAIN,1) %Forward out0=0; h11=logsig(out0*W(1,1)+DATATRAIN(i,1)*W(2,1)); h21=logsig(out0*W(1,2)+DATATRAIN(i,1)*W(2,2)); out1=logsig(h11*V(1)+h21*V(2)+V(3)); h12=logsig(out1*W(1,1)+DATATRAIN(i,2)*W(2,1)); h22=logsig(out1*W(1,2)+DATATRAIN(i,2)*W(2,2)); out2=logsig(h12*V(1)+h22*V(2)+V(3)); h13=logsig(out2*W(1,1)+DATATRAIN(i,3)*W(2,1)); h23=logsig(out2*W(1,2)+DATATRAIN(i,3)*W(2,2)); out3=logsig(h13*V(1)+h23*V(2)+V(3)); EST=[EST out3]; end EST=EST'; JCOL=[JCOL -1*sum(sum(TARGETTRAIN.*log(EST')))]; end figure plot(JCOL) ESTTEST=[]; for i=1:1:size(DATATEST,1) %Forward out0=0; h11=logsig(out0*W(1,1)+DATATEST(i,1)*W(2,1)); h21=logsig(out0*W(1,2)+DATATEST(i,1)*W(2,2)); out1=logsig(h11*V(1)+h21*V(2)+V(3)); h12=logsig(out1*W(1,1)+DATATEST(i,2)*W(2,1)); h22=logsig(out1*W(1,2)+DATATEST(i,2)*W(2,2)); out2=logsig(h12*V(1)+h22*V(2)+V(3)); h13=logsig(out2*W(1,1)+DATATEST(i,3)*W(2,1)); h23=logsig(out2*W(1,2)+DATATEST(i,3)*W(2,2)); out3=logsig(h13*V(1)+h23*V(2)+V(3)); ESTTEST=[ESTTEST out3]; end figure subplot(2,1,1) plot(TARGETTEST,'r') subplot(2,1,2) plot(ESTTEST,'b')