multiple input single output neural network
7 views (last 30 days)
Show older comments
%%% I want to calulate Mean square error
clc ;
clear all;
load('input');
load('output');
% s=5 ; %neurons
x=input ;
y=output ;
S=8; %neurons
Nmax=100;
MinTraErr=0.01;
NORMG=0.0001;
umin=0.05;umax=1000;uscal=0.5;
inp= size(x,1);
out= size(y,1);
R=2;
n0=3;
k=1;
%RANDOMIZATION
relation = [x;y];
ind = 1:length(x);
a = ind(randperm(length(ind)));
shuffeled_vector = relation(:,a);
%NORMALIZATION
for i=1:size(x,1)
xn(i,:)=2*((x(i,:)-min(x(i,:)))/(max(x(i,:))-min(x(i,:)))-0.5);
xmin(i) = min(x(i,:));
xmax(i) = max(x(i,:));
end
yn=2*((y-min(y))/(max(y)-min(y))-0.5);
ymin = min(y);
ymax = max(y);
%SEPERATION
RATIO = 0.7; % Train-Test Ratio
train_data = shuffeled_vector(:,1:fix(length(x)*RATIO));
test_data = shuffeled_vector(:,fix(length(x)*RATIO)+1:end);
ti_vector = train_data(1:R,:);
y_vector = train_data(end,:);
ti_vector_test = test_data(1:R,:);
y_vector_test = test_data(end,:);
%Training
for p=1:10000 % p is the (number of data)/input
t(1,p)=x(1,p) ;
Yreal(1,p)=y(1,p);
end
Yreal=Yreal.';% transpose
t=t.'
b=size(t);
e=b(1);
R=b(2);
xmin=-50;xmax=50;detend=10^-6;
umin=0.05;umax=1000;uscal=0.5;
syms X [S*R+2*S+1,1] ; syms y;
h=(exp(y)-exp(-y))/(exp(y)+exp(-y)); %Activation Function
%Iteration number
maxiter=100; iter=1;
% X=randn(1,S*(R+2)+1);
bh=X(S*R+1:S*R+S,1);
wo=X((S*(R+1)+1):(S*(R+2)),1);
wo=transpose(wo);
bo = X(end);
bi=X(S*R+2*S+1,1);
wi=reshape(X(1:S*R),S,R);
J=vpa(ones(e,S*R+2*S+1));
%Jacobian
for i=1:e
%Model Outputs
sum=0;
for j=1:S
sum1=0;
for k=1:R
sum1=sum1+wi(j,k)*t(i,k);
end
A(j,1)=subs(h,y,sum1+bh(j));
sum=sum+wo(1,j)*A(j,1);
end
Ymodel(i,1)=sum+bi;
% jacobian matrix
for j=1:S*R
k=mod(j-1,S)+1;
m=fix((j-1)/S)+1;
J(i,j)=-(wo(1,k)*t(i,m))*(1-(A(k,1)^2));
xk(j,1)=wi(k,m);
end
for j=S*R+1:S*R+S
J(i,j)=-wo(1,j-S*R)*(1-(A(j-S*R,1)^2));
xk(j,1)=bh(j-S*R,1);
end
for j=S*R+S+1:S*R+2*S
J(i,j)=-A(j-S*R-S,1);
xk(j,1)=wo(1,j-S*R-S);
end
J(i,S*R+2*S+1)=-1;
xk(j+1,1)=bi(1,1);
end
xk=rand(S*R+2*S+1,1);
E=vpa(Yreal-Ymodel);
f=vpa(E.'*E);
loop1=1; Mu=1;
while loop1
iter=iter+1;
Jxk=double(subs(J,x,xk));
fxk=double(subs(f,x,xk));
Exk=double(subs(E,x,xk));
loop2=1;
while loop2
in=-inv(Jxk.'*Jxk+Mu*eye(size(Jxk,2)));
pk=(in*Jxk.'*Exk);
zk=xk+pk;
Ymodelzk=double(subs(Ymodel,x,zk));
% % while loop2
% % Jacob=-inv(Jxk.'*Jxk+u*eye(size(Jxk,2)));
% % pk=(Jacob*Jxk.'*Exk);
% % zk=xk+pk;
% % % Ymodelzk=double(subs(Ymodel,x,zk));
% % for i = 1:length(y_vector)
% % y_hat_initial = wo*tanh(wi*ti_vector(:,i)+bh)+bo;
% % y_hat(i) = y_hat_initial;
% % end
E=double(Yreal-Ymodelzk);
fzk=double(E.'*E);
if fzk<fxk
fx=subs(f,x,xk+x1.*pk);
xmin=-50;xmax=50;detend=10^-6;
[sk]= GoldenSectionMethodE(fx,xmax,xmin,detend)
xk=double(xk+sk.*pk)
u=u/ucale;
loop2=0;
else
u=u*uscale;
end
if u<umin && u>umax
loop1=0;
loop2=0;
end
end
fxk1=double(subs(f,x,xk));
NORMG=norm(sk*pk);
end
0 Comments
Answers (1)
Varun Sai Alaparthi
on 18 Jan 2023
Hello Ecem,
In order to better answer your query, could you provide me with some more details on what exactly you are looking for?
However, in case you are looking for a function to calculate mean squared error you can refer to this function ‘immse’.
Mse = immse(X,Y);
% Run this code fir calculating mse between X,Y matrices
If you have any further queries, please feel free to reply to my answer
Sincerely,
Varun
0 Comments
See Also
Categories
Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!