Question: Please fill the blanks: % Only valid for one inputs, one output and Nn neurons in the hidden layer clearvars clc addpath('../Basic_blocks') % Main parameters
Please fill the blanks:
% Only valid for one inputs, one output and Nn neurons in the hidden layer clearvars clc addpath('../Basic_blocks')
% Main parameters mu=.1; % Step size Ns=5000000; % Number of samples Nh=10; % Number of neurons hidden layer Ni=2; % Number of inputs No=1; % Number of outputs
% Defining the input and the desired signals x1a=randn(2,Ns/4)*sqrt(.1)+kron([1.5;1.5],ones(1,Ns/4)); % Input class 1 x1b=randn(2,Ns/4)*sqrt(.1)+kron([-1.5;-1.5],ones(1,Ns/4)); % Input class 1 x2a=randn(2,Ns/4)*sqrt(.1)+kron([-1.5;1.5],ones(1,Ns/4)); % Input class 2 x2b=randn(2,Ns/4)*sqrt(.1)+kron([1.5;-1.5],ones(1,Ns/4)); % Input class 2
% For visualization plot(x1a(1,:),x1a(2,:),'r+','LineWidth',3),hold plot(x1b(1,:),x1b(2,:),'r+','LineWidth',3), plot(x2a(1,:),x2a(2,:),'b+','LineWidth',3), plot(x2b(1,:),x2b(2,:),'b+','LineWidth',3),hold off axis('square'),grid x=[x1a x1b x2a x2b]; r=randperm(Ns); x1=x(1,r); x2=x(2,r); x=[x1;x2]; d=r>(Ns/2);
% Defining the variables (weights and bias) W1=zeros(Nh,Ni,Ns+1); % Weights hidden layer W2=zeros(No,Nh,Ns+1); % Weights hidden layer W1(:,:,1)=rand(Nh,Ni);%W1(:,:,1)=[-.27;-.41]; W2(:,:,1)=rand(No,Nh);%W2(:,:,1)=[0.09 -.17]; b1=zeros(Nh,Ns+1); b1(:,1)=rand(Nh,1);%b1(:,1)=[-.48;-.13]; b2=zeros(No,Ns+1); b2(:,1)=rand(No,1);%b2(:,1)=0.48; tipo='logist'; error=zeros(1,Ns); out=zeros(1,Ns);
% Loop along the samples including the forward and backward steps for k=1:Ns y0=[x(:,k)]; [y1 y2 v1 v2]=forward(W1(:,:,k),W2(:,:,k),b1(:,k),b2(:,k),y0,tipo); e(k)=d(k)-y2;out(k)=y2; [delta2 delta1]=backward(W2(:,:,k),y1,y2,e(k),tipo); W2(:,:,k+1)=W2(:,:,k)+2*mu*delta2*y1'; b2(k+1)=b2(k)+mu*2*delta2; W1(:,:,k+1)=W1(:,:,k)+mu*2*delta1*y0'; b1(:,k+1)=b1(:,k)+mu*2*delta1; end W2_out=W2(:,:,Ns);b2_out=b2(Ns); W1_out=W1(:,:,Ns-1);b1_out=b1(:,Ns);
% How to present results in=-3:.1:3; [X Y]=meshgrid(in,in); out_func=zeros(size(X)); for k=1:length(in) for kk=1:length(in) y0=[X(k,kk);Y(k,kk)]; [y1 y2 v1 v2]=forward(W1_out,W2_out,b1_out,b2_out,y0,tipo); y2; if y2>0.5 plot(X(k,kk),Y(k,kk),'or','LineWidth',3) else plot(X(k,kk),Y(k,kk),'+b','LineWidth',3) end if k*kk==1,hold;end end end hold off,axis([-3 3 -3 3]), axis('square')
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
