How do I get the correct output from a fitnet artificial neural network?

8 views (last 30 days)
I'm getting started with building an artificial neural network and testing it with a simple example (see code below).
I'm trying to get the artificial neural network (using fitnet) to predict that the function is y=a*5+b.*c+7*c. The neural network is successfully trained. However, when I try using the function:
y = b2 + LW*tanh(b1+IW*x)
followed by a reverse mapminmax, I do not obtain the correct value.
In the code below, I have tried defining
X = [0.5, 0.5, 0.5]
The output is (y2 is obtained by using the reverse mapminmax, y1 is using sim(net,x), and y1compare is the actual output target):
y2 = 9.5759
y1 = 6.2795
y1compare = 6.2500
My entire code is below:
clc
clear all
a=rand(1,1000);
b=rand(1,1000);
c=rand(1,1000);
n=rand(1,1000)*0.05;
y=a*5+b.*c+7*c+n;
I=[a; b; c];
O=y;
% Create network for curve fitting
hiddenLayerSize = 10; % Number of intermediate network neurons
net = fitnet(hiddenLayerSize);
% Setting the pre and post process data
net.inputs{1}.processFcns = {'removeconstantrows','mapminmax'};
net.outputs{2}.processFcns = {'removeconstantrows','mapminmax'};
% Setting the sample size
net.divideFcn = 'dividerand'; % Split random data
net.divideMode = 'sample';
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
net=train(net,I,O);
% syms p q r real
% X = [p,q,r]';
X = [0.5 0.5 0.5]';
b1 = net.b{1}
b2 = net.b{2}
IW = net.IW{1,1};
LW = net.LW{2,1};
[tn, tsettings] = mapminmax(O);
tsettings
yn = b2 + LW * tanh(b2 + (IW * X))
y2 = mapminmax.reverse(yn,tsettings)
y1 = sim(net,X)
y1compare = X(1)*5+X(2).*X(3)+7*X(3)
Thanks in advance!
  1 Comment
Jeffrey Hung
Jeffrey Hung on 23 Sep 2016
Found a code that works!
clc
clear all
% Create network for curve fitting
hiddenLayerSize = 4; % Number of intermediate network neurons
net = fitnet(hiddenLayerSize);
WB = getwb(net); % Only net.b{1} = zeros(10,1)is defined
rng(4151945); % Initialize the RNG so that results can be duplicated
M = [1:1:10];
M = [M,M,M,M,M].*rand();
M = [M,M].*rand();
M = [M,M,M,M,M].*10;
M = [M,M].*10;
a=M.*rand().*2^rand()+5*rand()-5*rand();
b=M.*rand().*2^rand()+5*rand()-5*rand();
c=M.*rand().*2^rand()+5*rand()-5*rand();
n=rand(1,1000)*0.05;
y = 5*a + b.*c + 7*c + n;
x=[a; b; c];
t=y;
% Setting the sample size
net.divideFcn = 'dividerand'; % Split random data
net.divideMode = 'sample';
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
net = train(net,x,t);
[xn, xsettings] = mapminmax(x);
[tn, tsettings] = mapminmax(t);
% syms p q r real
% X = [p,q,r]';
X = [22,25,21]'
x000 = mapminmax('apply',X,xsettings);
b1 = net.b{1};
b2 = net.b{2};
IW = net.IW{1,1};
LW = net.LW{2,1};
yn = b2 + LW * tanh(b1 + (IW * x000))
y2 = mapminmax.reverse(yn,tsettings)
y1 = sim(net,X)
y1compare = 5*X(1) + X(2)*X(3) + 7*X(3)

Sign in to comment.

Accepted Answer

Greg Heath
Greg Heath on 23 Sep 2016
Edited: Greg Heath on 9 Nov 2016
1. Very often you have to use several choices of the random initial weights in order to get a good answer.
2. You forgot to take into account the input normalization.
Hope this helps.
Thank you for formally accepting my answer
Greg

More Answers (0)

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!