output in neural network

53 views (last 30 days)
niranjan sane
niranjan sane on 10 Apr 2012
hello all, i have a query i have written a code in matlab which forecast daily water demand but whenever i run the code it gives different results how to fix the results in a particular band also tell me if my code is correct. plz do rep if and tell me if i have to make any corrections in the code i have tried using learning function as traindgm instead of trainlm which learning function is better in both?????
% two inputs neurons as average humidity and water demand of previous day and one neuron in output layer as weekly water demand.
% t=forecasted water demand
% s=actual water demand
data=xlsread('data2003-2010modified.xls');
%Traing data-7years
%Input
%humidity
P(:,1)=data(1:2555,5);
%Previous waterdemand
P(1,2)=data(1,8)-1;
P(2:2555,2)=data(1:2554,8);
%Output water demand
T=data(1:2555,8);
%Testing data-1 year
%humidity
a(:,1)=data(2556:end,5);
%Previous day waterdemand
a(:,2)=data(2555:end-1,8);
%output for comparison
s=data(2556:end,8);
%Input data preprocessing
[pn,minp,maxp,tn,mint,maxt]=premnmx(P',T');
[an,mina,maxa,sn,mins,maxs]=premnmx(a',s');
%Create neural network
net=newff(minmax(pn),[10 1],{'tansig','purelin'},'trainlm');
net.trainParam.epochs=3000;
net.trainParam.lr=0.3;
net.trainParam.mc=0.6;
%Train neural network
net=train (net,pn,tn);
%Simulate neural network with test samples
y=sim(net,an);
%Post Processing
t=postmnmx(y',mins,maxs);
%Plot between Actual and predicted values
figure;
%Plot between Actual and predicted values
figure;
plot(t,'red');
hold on;
% Current plot held
plot(s,'blue');
xlabel('Samples');
ylabel('Water Demand(MLD)');
title('Comparison between neural network output and actual water demand')
legend('forecasted water demand','Actual water Demand');
hold off;
%Plot Regression
figure;
plotregression(t,s);

Answers (1)

Eike
Eike on 10 Apr 2012
1. I am unable to find any documentation on the function newff and what it does. I would be pleased if someone could show me the relevant parts of the documentation...?
2. It is correct for NNs to produce different output for the same input if they are trained separately. This is due to the fact that the initial weights of the neurons are chosen randomly each time. Therefore, the output of your script will be a different NN each time you run it. I think there is a possibility to assign specific initial weights, though.
3. In your code, you do the input shifting by hand. There are tools provided by the NN toolbox to ease that process. Normally, you would do something like the following:
% external input series: humidity
externalInputSeries = tonndata(data(1:end, 5), false, false);
% target (and feedback input!) series: water demand
targetSeries = tonndata(data(1:end, 8), false, false);
% the water demand of the previous day is used as an input
feedbackDelay = 1;
% the average humidity of the previous day is used as an external input
inputDelay = 1;
% use 10 Neurons in the network
hiddenLayerSize = 10;
% create a NARX net
net = narxnet(inputDelays, feedbackDelays, hiddenLayerSize);
% Prepare the Data for Training and Simulation
[inputs,inputStates,layerStates,targets] = preparets(net,inputSeries,{},targetSeries);
% 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 = 'time'; % Divide up every value
net.divideParam.trainRatio = 70/100;
net.divideParam.valRatio = 15/100;
net.divideParam.testRatio = 15/100;
% Choose a Training Function
% For a list of all training functions type: help nntrain
net.trainFcn = 'trainlm'; % Levenberg-Marquardt
% Train the Network
[net,tr] = train(net,inputs,targets,inputStates,layerStates);
% % Test the Network
outputs = net(inputs,inputStates,layerStates);
% show a graphical representation of the NN
view(net);
I do not guarantee that all of the above code is correct, but I've recently solved a similar problem and the above approach worked for me. In my opinion it is nicer than yours since it automates all handling of the test data set.
You can get some inspiration on what is possible by using the graphical NN tool and creating code from it.
Greetings and good luck,
Eike

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by