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=1000000; % Number of samples Nh=2; % Number of neurons hidden layer Ni=2; % Number of inputs No=1; % Number of outputs
% Defining the input and the desired signals x1=randi([0 1],1,Ns+1); % Input signal 1 x2=randi([0 1],1,Ns+1); % Input signal 2 x=[x1;x2]; d=xor(x1,x2); % Desired signal
% Defining the variables (weights and bias) W1=zeros(Nh,Ni,Ns+1); % Weights hidden layer W2=zeros(No,Nh,Ns+1); % Weights outut layer W1(:,:,1)=rand(Nh,Ni); % Initialization W2(:,:,1)=rand(No,Nh); % Iitialization b1=zeros(Nh,Ns+1); % Bias hidden layer b1(:,1)=rand(Nh,1); % Initialization b2=zeros(No,Ns+1); % Bias output layer b2(:,1)=rand(No,1); % Initialization tipo='linear'; % Output non linearity error=zeros(1,Ns); % Error signal out=zeros(1,Ns); % Output signal
% 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=0:.01:1; [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([-.2 1.2 -.2 1.2]), axis('square')
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
