Neural network with multiple inputs and single output - how to change processing functions and how to plot results?

21 views (last 30 days)
Hello everyone! I would like to create a neural network with 6 input nodes. In the following I have created a simple code with the help of the neural network toolbox. I have a few questions regarding this code.
1. As I understood after reading several forum entries I have to create a matrix out of the 6 input vectors first, right?
2. How can I change the processing function in the hidden layer nodes? In the default settings the sigmoid function is used... I would like to change it to the tangid function...
3. How can I plot the results of the neural network. I would like to plot the original target data as well as the test data that are forecasted by the neural network in one diagram to show how well they compare... since I have multiple inputs this function of the neural network toolbox doesn't work...
% This is my code:
a=[1 2 3 4 5 6 7 8 9 10]'; b=[3 4 5 6 7 8 9 10 11 1]'; c=[1 4 2 5 3 6 4 8 9 10]'; d=[2 4 5 6 7 9 8 10 1 11]'; e=[1 6 3 5 4 7 8 9 10 11]'; f=[10 9 8 7 1 2 3 4 5 6 ]';
o=[50 51 52 54 58 60 65 66 69 70]';
% Create input matrix and output vector inputs=[a b c d e f]'; targets=o';
% Create a Fitting Network hiddenLayerSize = 4; 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)
figure, plotfit(net,inputs,targets)
Thank you so much!!

Accepted Answer

Greg Heath
Greg Heath on 19 Sep 2014
Edited: Greg Heath on 19 Sep 2014
1. Type
net = fitnet % NO SEMICOLON
You will see that there are defaults for all parameters.
2. Type
help fitnet % and/or doc fitnet
You will see that, if you do not get a bad set of random weights you can design a net with just 3 commands
>> [ x, t ] = simplefit_dataset;
>> [ net tr y e ] = train( fitnet, x, t ); % e = t - y
>> Rsquare = 1-mse( e )/var( t, 1 )
Rsquare =
0.99998 % fraction of target variance that is modeled by the net
Therefore, all you have to worry about is
1. Initializing the RNG so that you can intelligently search for a good set of random initial weights.
2. Finding the smallest number of hidden nodes that will yield a satisfactory design.
3. Therefore, my serious designs use a double for loop. The outer loop h = Hmin:dH:Hmax over No. of hidden nodes and i = 1:Ntrials over sets of initial weights.
4. For posted examples, search using
greg fitnet Ntrials
Hope this helps,
Thank you for formally accepting my answer
Greg
  3 Comments
Greg Heath
Greg Heath on 19 Sep 2014
If you want to know the meaning of any command or function use one or all of the following commands before posting the question
help rng
doc rng
type rng
MATLAB is a matrix based language. Most commands are matrix commands.
If you need help with multidimensional inputs and/or outputs, test on MATLAB example data
help nndatasets
doc nndatasets
If you get stuck, try searching the NEWSGROUP or ANSWERS with that dataset name. You can whittle the list down by including the searchword greg
Hope this helps.
Greg
amna batool
amna batool on 16 Mar 2019
Can you please tell me how to give three inputs in newff to generate a block using gensim having three input ports
my code is:
net10 = newff(p,t,[5 2],{'tansig' 'purelin'})
p=[6.697 6.4 6.281 6.217 6.177 6.150 6.130 6.101 5.121; 6.608 6.307 6.179 6.102 6.405 6.150 6.130 6.101 5.121;
6.604 6.303 6.175 6.098 6.040 5.987 5.929 5.715 4.519]
t=[ 10 20 30 40 50 70 80 90 100]
net10.trainParam.epochs = 200;
net10.trainParam.goal=0;
net10 = train(net10,p,t);
Y = sim(net10,p);
e=t-Y;
plot(p,t,p,Y,'o')
gensim(net10)

Sign in to comment.

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!