How to train the neural network?

Hi there, I am trying to create a time-series response and predict the future energy demand. But I am having a problem to train the network.
EnergyDemand = xlsread('C:\Users\user\Desktop\FYP\Datasheet.xlsx','Case Study Table 2','B4:B15');
Targets = xlsread('C:\Users\user\Desktop\FYP\Datasheet.xlsx','Case Study Table 2','C4:E15');
% define signal
inputs = EnergyDemand;
% plot signal
figure;
plot(input)
% Create a Pattern Recognition Network
hiddenLayerSize = 10;
net = patternnet(hiddenLayerSize);
% Set up Division of Data for Training, Validation, Testing
net.divideParam.trainRatio = .7;
net.divideParam.valRatio = .15;
net.divideParam.testRatio = .15;
%net1 = newff(inputs,Targets,1,{'tansig' 'purelin'},'traingdm');
%net = newff(x1,Target,1);
net = feedforwardnet(2);
net= train(net,inputs,Targets);
view(Net);
I keep on getting the error message Error using network/train (line 325) Inputs and targets have different numbers of samples.
Error in Case_Study (line 21) net= train(net,inputs,Targets);
I hope you are able to help me. Thanks in advance.

 Accepted Answer

You are way off base.
1. Time series functions are timedelaynet, narnet and narxnet.
2. Read the time series documentation and examples
3. See some of my examples by searching in NEWSGROUP and ANSWERS. Search using
greg timedelaynet
greg narnet
greg narxnet
HTH
Thank you for formally accepting my answer
Greg

5 Comments

I tried using the help function. and I tried something like this

EnergyDemandDatasetTarget = dataset('XLSFile','CaseStudyCol.xlsx');
load EnergyDemandDatasetCol;
T = EnergyDemandDatasetCol; 
net = narnet(1:2,10);
[Xs,Xi,Ai,Ts] = preparets(net,{},{},T);
net = train(net,X,T,Xi,Ai);
view(net)
Y = net(Xs,Xi,Ai);
plotresponse(T,Y)

But I keep getting error for T = EnergyDemandDatasetCol;

Sorry because I am a slow learner and thank you for helping.

1. Please move your response from an answer box to a comment box.
2. Run your code on a MATLAB narnet data set bfore trying it on your own data.
help nndatasets
3. Be sure to compute the autocorrelation function of T to determine the significant lags to use, (1:2) are just default values which may be very suboptimal.
4. Search
greg narnet

Hi Greg. Thanks for the help all these while. I tried running my data on narxnet as I am suppose to predict data then I modified it by a little.

I have an article for ANN for transport energy demand modelling.

I have Energy Demand as the target and 3 input, population, veh-km and GNP. However I am unable to create a multiple input, or rather I do not know how to. I seek you assistance in this.

From these values, I am suppose to train the network and predict the demand for the next 5 years.

% 1. Importing data
Data_Inputs=xlsread('C:UsersuserDesktopFYPCaseStudyCaseStudyCol.xlsx'); 
% Import file
Training_Set1=Data_Inputs(1:end,3);%specific training set
Target_Set=Data_Inputs(1:end,2); %specific target set
Input=Training_Set1'; %Convert to row
Target=Target_Set'; %Convert to row
X = con2seq(Input); %Convert to cell
T = con2seq(Target); %Convert to cell
tr = Data_Inputs(1:end,1);
% 2. Data preparation
N = 5; % Multi-step ahead prediction
% Input and target series are divided in two groups of data:
% 1st group: used to train the network
inputSeries  = X(1:end-N);
targetSeries = T(1:end-N);
inputSeriesVal  = X(end-N+1:end);
targetSeriesVal = T(end-N+1:end);
% Create a Nonlinear Autoregressive Network with External Input
delay = 2;
inputDelays = 1:2;
feedbackDelays = 1:2;
hiddenLayerSize = 2;
net = narxnet(inputDelays,feedbackDelays,hiddenLayerSize);
% Prepare the Data for Training and Simulation
% The function PREPARETS prepares timeseries data for a particular network,
% shifting time by the minimum amount to fill input states and layer states.
% Using PREPARETS allows you to keep your original time series data unchanged, while
% easily customizing it for networks with differing numbers of delays, with
% open loop or closed loop feedback modes.
%[Xs,Xi,Ai,Ts] = preparets(net,inputSeries,{},targetSeries);
[inputs,inputStates,layerStates,targets] = preparets(net,inputSeries,{},targetSeries);
% Setup Division of Data for Training, Validation, Testing
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
% Train the Network
net = train(net,inputs,targets,inputStates,layerStates);
% Test the Network
outputs = net(inputs,inputStates,layerStates);
errors = gsubtract(targets,outputs);
performance = perform(net,targets,outputs);
% View the Network
view(net)
% Plots
% Uncomment these lines to enable various plots.
%figure, plotperform(tr)
%figure, plottrainstate(tr)
%figure, plotregression(targets,outputs)
%figure, plotresponse(targets,outputs)
%figure, ploterrcorr(errors)
%figure, plotinerrcorr(inputs,errors)
% Closed Loop Network
% Use this network to do multi-step prediction.
 % The function CLOSELOOP replaces the feedback input with a direct
% connection from the outout layer.
netc = closeloop(net);
netc.name = [net.name ' - Closed Loop'];
view(netc)
[xc,xic,aic,tc] = preparets(netc,inputSeries,{},targetSeries);
yc = netc(xc,xic,aic);
closedLoopPerformance = perform(netc,tc,yc);
% Early Prediction Network
% For some applications it helps to get the prediction a timestep early.
% The original network returns predicted y(t+1) at the same time it is given y(t+1).
% For some applications such as decision making, it would help to have predicted
% y(t+1) once y(t) is available, but before the actual y(t+1) occurs.
% The network can be made to return its output a timestep early by removing one delay
% so that its minimal tap delay is now 0 instead of 1.  The new network returns the
% same outputs as the original network, but outputs are shifted left one timestep.
nets = removedelay(net);
nets.name = [net.name ' - Predict One Step Ahead'];
view(nets)
[xs,xis,ais,ts] = preparets(nets,inputSeries,{},targetSeries);
ys = nets(xs,xis,ais);
earlyPredictPerformance = perform(nets,ts,ys);
% 5. Multi-step ahead prediction
inputSeriesPred  = [inputSeries(end-delay+1:end),inputSeriesVal];
targetSeriesPred = [targetSeries(end-delay+1:end), con2seq(nan(1,N))];
[Xs,Xi,Ai,Ts] = preparets(netc,inputSeriesPred,{},targetSeriesPred);
yPred = netc(Xs,Xi,Ai);
perf = perform(net,yPred,targetSeriesVal);
figure;
plot([cell2mat(targetSeries),nan(1,N);
  nan(1,length(targetSeries)),cell2mat(yPred);
  nan(1,length(targetSeries)),cell2mat(targetSeriesVal)]')
legend('Original Targets','Network Predictions','Expected Outputs');

Did I define the inputs and targets correctly? How do you show the graph that presents the prediction data? Where does the regression graph appear after the network is being trained? And how do I know if it is a good result?

1. I gave you 4 distinct pieces of advice.
2. You followed none of them
3. Why should I continue to waste my time?
Hi Greg,
Thank you for your prompt reply.
I did tried to follow your advise. I tried running the preset datasets in the nndatasets. Then I tried running my own dataset using nnstart. after that I generated the codes and compare with the preset values. And I did search for greg narnet, there are so many and I kept on trying different codes to run. Sorry that I am a slow learner but I hope you will continue to advise me. And thank you for helping me.
Nur Safura

Sign in to comment.

More Answers (0)

Categories

Find more on Deep Learning Toolbox 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!