user2987710
user2987710

Reputation: 11

How to calculate data by constructed neural network toolbox?

I have a series of x and y data. For Example:

x=[1 2 4 5 7 8 9 18 29]
y=[4 7 11 18 35 42 67 100 110]

I have used Neural-Network toolbox of Matlab and have made a neural network model.(I have put my codes in the end of question) But I want to calculate corresponding values of below x.In other word,if:

x=[60 80 98 120]

then,I want to calculate corresponding y of this points in Matlab?(I know that I can do this calculation by simple regression.But I insist on doing this by neural network)

Can anyone help me?

x=[1 2 4 5 7 8 9 18 29]
y=[4 7 11 18 35 42 67 100 110]

%// Solve an Input-Output Fitting problem with a Neural Network
%// Script generated by NFTOOL
%// Created Wed Oct 15 00:18:47 PDT 2014
%//
%// This script assumes these variables are defined:
%//
%//   x - input data.
%//   y - target data.

inputs = x;
targets = y;

%// Create a Fitting Network
hiddenLayerSize = 10;
net = fitnet(hiddenLayerSize);

%// Choose Input and Output Pre/Post-Processing Functions
%// For a list of all processing functions type: help nnprocess
net.inputs{1}.processFcns = {'removeconstantrows','mapminmax'};
net.outputs{2}.processFcns = {'removeconstantrows','mapminmax'};


%// Setup Division of Data for Training, Validation, Testing
%// For a list of all data division functions type: help nndivide
net.divideFcn = 'dividerand';  %// Divide data randomly
net.divideMode = 'sample';  %// Divide up every sample
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;

%// For help on training function 'trainlm' type: help trainlm
%// For a list of all training functions type: help nntrain
net.trainFcn = 'trainlm';  %// Levenberg-Marquardt

%// Choose a Performance Function
    %// For a list of all performance functions type: help nnperformance
net.performFcn = 'mse';  %// Mean squared error

%// Choose Plot Functions
%// For a list of all plot functions type: help nnplot
net.plotFcns = {'plotperform','plottrainstate','ploterrhist', ...
  'plotregression', 'plotfit'};


%// Train the Network
[net,tr] = train(net,inputs,targets);

%// Test the Network
outputs = net(inputs);
errors = gsubtract(targets,outputs);
performance = perform(net,targets,outputs)

%// Recalculate Training, Validation and Test Performance
trainTargets = targets .* tr.trainMask{1};
valTargets = targets  .* tr.valMask{1};
testTargets = targets  .* tr.testMask{1};
trainPerformance = perform(net,trainTargets,outputs)
valPerformance = perform(net,valTargets,outputs)
testPerformance = perform(net,testTargets,outputs)

%// View the Network
view(net)

%// Plots
%// Uncomment these lines to enable various plots.
%//figure, plotperform(tr)
%//figure, plottrainstate(tr)
%//figure, plotfit(net,inputs,targets)
%//figure, plotregression(targets,outputs)
%//figure, ploterrhist(errors)

Upvotes: 0

Views: 851

Answers (3)

SACn
SACn

Reputation: 1924

Note that test range x=[60 80 98 120] is well outside training range x=[1 2 4 5 7 8 9 18 29]. So what we're trying to do here is to: Solve Extrapolation using ANN & Not using ANN for its purpose that is Classification.

In simple words for extrapolation use curve fitting like regression not ann or use ann with custom neurons. If ann is mandate than use entire data range to train x=[1 2 4 5 7 8 9 18 29 60 80 98 120] the network.

Upvotes: 0

tashuhka
tashuhka

Reputation: 5126

The answer of @Ander Biguri is the correct one. I just want to insist of two problems about your approach (this could be a comment and blabla, but I couldn't include a picture)

If you see your predicted points (red crosses), they look fine.

enter image description here

However, if you plot the predicted points for all the x-axis, you can see what the NN actually learnt from your data:

enter image description here

x_ = 0:0.1:120;
y_ = sim(net,x_);
figure;
hold on;
plot(x_,y_,'r.');
plot(x,y,'bo');
axis([0 100 0 300]);

You can spot two issues:

  1. The NN is overfitting because of the lack of data. Besides, the non-linear hyperplane of the NN gets crazy.
  2. The NN's cannot extrapolate. All the predicted value greater that x=29 have non-sense.

Upvotes: 1

Ander Biguri
Ander Biguri

Reputation: 35525

There is an specific function in maltab to simulate trained NN: sim

Its as easy as:

    sim(net,x);

ans =

  102.6437  102.6437  102.6437  102.6437

it will simulate the network given the inputs.

Upvotes: 2

Related Questions