Forecast for Reactive Power (HELP PLEASE!!!)

4 views (last 30 days)
hi everyone, I'm new and I've been working with neural networks for a few months. In particular, the work I am doing is making a Reactive Power forecast. At the moment, following a matlab example I have created the following LSTM network. But unfortunately it doesn't seem to work at all.
I kindly ask for your help, I have been working on it for at least two months without getting anything good. Could someone kindly help me? How can I improve my predictions? What networks could I use? I have already tried the Narx a closed cycle but also the poor ones. Another thing I would like to understand is, in the attached script how could I also insert information on the time and day of the week in input to the network? At the moment I give input only the time series of Reactive Power, how could I insert other inputs?
Please help me, I really need it right now.
I am attaching scripts and results.
load reattivo;
data = reattivo;
data = [data{:}];
figure
plot(data)
xlabel("Quarter Hour")
ylabel("Reattivo")
title("Serie Storica Potenza Reattiva")
%Partition data
%Il dataset di partenza viene suddiviso in training set (l'addestramento
%avviene sul 90% del dataset iniziale) e test set (restante 10%)
numTimeStepsTrain = floor(0.9*numel(data));
dataTrain = data(1:numTimeStepsTrain+1);
dataTest = data(numTimeStepsTrain+1:end);
%Standardize Data
%Per un migliore adattamento e, per evitare che l'addestramento diverga,
%i dati vengono standardizzati in modo che abbiano media zero e varianza
%unitaria. Al momento della previsione, è necessario standardizzare i dati
%del test set utilizzando gli stessi parametri del training set
mu = mean(dataTrain);
sig =std(dataTrain);
dataTrainStandardized = (dataTrain - mu)/sig;
%Prepare Prediction Responses
%Per prevedere i valori delle fasi temporali future di una sequenza, le
%risposte vengono specificate come sequenze di addestramento con i valori
%spostati di una fase temporale. Cioè, da ogni fase temporale della
%sequenza di input, la rete LSTM impara a prevedere il valore della fase
%temporale successiva. I predittori sono le sequenze di addestramento senza
%la fase temporale finale.
XTrain = dataTrainStandardized(1:end-1);
YTrain = dataTrainStandardized(2:end);
%Define LSTM Network Architeture
%Crea una rete di regressione LSTM, con un layer avente 200 unità nascoste
numFeatures = 1;
numResponses = 1;
numHiddenUnits = 200;
layers = [ ...
sequenceInputLayer(numFeatures)
lstmLayer(numHiddenUnits)
fullyConnectedLayer(numResponses)
regressionLayer];
%Specify the Training Options
%Il risolutore viene impostato su "adam" e addestrato per 250 epoche.
%Affinchè i gradienti non esplodano, la soglia del gradiente è impostata su
%1. La velocità di apprendimento iniziale è impostata a 0.005 e viene
%successivamente ridotta dopo 125 epoche moltiplicando per un fattore 0.2.
options = trainingOptions('adam',...
'MaxEpochs',250,...
'GradientThreshold',1,...
'InitialLearnRate',0.005,...
'LearnRateSchedule','piecewise',...
'LearnRateDropPeriod',125, ...
'LearnRateDropFactor',0.2, ...
'Verbose',0, ...
'Plots','training-progress');
%Train LSTM Network
%Addestra la rete LSTM con le opzioni di addestramento specificate
%utilizzando trainNetwork
net = trainNetwork(XTrain,YTrain,layers,options);
%Forecast Future Time Steps
%Per prevedere i valori di più fasi temporali in futuro, si utilizza la
%funzione ForecastAndUpdateState per prevedere le fasi temporali una alla
%volta e aggiornare lo stato della rete a ogni previsione. Per ogni
%previsione, si utilizza la previsione precedente come input per la
%funzione.
%I dati del test set vengono standardizzati utilizzando gli stessi
%parametri dei dati di training.
dataTestStandardized = (dataTest - mu) / sig;
XTest = dataTestStandardized(1:end-1);
%Per inizializzare los stato della rete, viene prima eseguita una
%previsione sui dati di addestramento XTrain. Successivamente, effettua la
%prima previsione utilizzando l'ultima fase temporale della risposta di
%addestramento YTrain (fine). Si esegue il ciclo delle previsioni rimanenti
%e si inserisce la previsione precedente in ForecastAndUpdateState.
%Per grandi quantità di dati, lunghe sequenze o reti di grandi dimensioni,
%le previsioni sulla GPU sono generalmente più veloci da calcolare rispetto
%alle previsioni sulla CPU. In caso contrario, le previsioni sulla CPU
%sono generalmente più veloci da calcolare. Per previsioni di passi a tempo
%singolo, utilizzare la CPU. Per utilizzare la CPU per la previsione,
%impostare l'opzione "ExecutionEnvironmet" di ForecastAndUpdateState su
%"cpu"
net = predictAndUpdateState(net,XTrain);
[net,YPred] = predictAndUpdateState(net,YTrain(end));
numTimeStepsTest = numel(XTest);
for i = 2:numTimeStepsTest
[net,YPred(:,i)] = predictAndUpdateState(net,YPred(:,i-1),'ExecutionEnvironment','cpu');
end
%Annullare la standardizzazione delle previsioni utilizzando i parametri
%calcolati in precedenza
YPred = sig*YPred + mu;
%Il grafico dell'avanzamento dell'addestramento riporta l'errore quadratico
%medio (RMSE) calcolato dai dati standardizzati. Calcola l'RMSE dalle
%previsioni non standardizzate.
YTest = dataTest(2:end);
rmse = sqrt(mean((YPred-YTest).^2))
%Tracciare le serie temporali di addestramento con i valori previsti
figure
plot(dataTrain(1:end-1))
hold on
idx = numTimeStepsTrain:(numTimeStepsTrain+numTimeStepsTest);
plot(idx,[data(numTimeStepsTrain) YPred],'.-')
hold off
xlabel("Quarter hour")
ylabel("Cases")
title("Forecast")
legend(["Observed" "Forecast"])
%Confronta i valori previsti con i dati del test set
figure
subplot(2,1,1)
plot(YTest)
hold on
plot(YPred,'.-')
hold off
legend(["Observed" "Forecast"])
ylabel("Cases")
title("Forecast")
subplot(2,1,2)
stem(YPred - YTest)
xlabel("Quarter hour")
ylabel("Error")
title("RMSE = " + rmse)
%Update Network State with Observed Values
%Se si ha accesso ai valori effettivi delle fasi temporali tra le
%previsioni, è possibile aggiornare lo stato della rete con i valori
%osservati invece dei valori previsti.
%Innanzitutto, la rete viene inizializzata. Per fare previsioni su una
%nuova sequenza si utilizza resetState. Il ripristino dello stato della
%rete impedisce alle previsioni precedenti di influenzare le previsioni sui
%nuovi dati. Reimpostare lo stato della rete, quindi inizializzare lo stato
%della rete prevedendo sui dati di addestramento.
net = resetState(net);
net = predictAndUpdateState(net,XTrain);
%Prevedere ogni passo temporale. Per ogni previsione, prevedere la fase
%temporale successiva utilizzando il valore osservato dalla fase temporale
%precedente.
YPred = [];
numTimeStepsTest = numel(XTest);
for i = 1:numTimeStepsTest
[net,YPred(:,i)] = predictAndUpdateState(net,XTest(:,i),'ExecutionEnvironment','cpu');
end
%Destandardizzare i dati utilizzando i parametri calcolati precedentemente
YPred = sig*YPred + mu;
%Calcolare l'RMSE
rmse = sqrt(mean((YPred-YTest).^2))
%Confronta i valori previsti con i dati del test.
figure
subplot(2,1,1)
plot(YTest)
hold on
plot(YPred,'.-')
hold off
legend(["Observed" "Predicted"])
ylabel("Cases")
title("Forecast with Updates")
subplot(2,1,2)
stem(YPred - YTest)
xlabel("Month")
ylabel("Error")
title("RMSE = " + rmse)

Answers (2)

Hiro Yoshino
Hiro Yoshino on 23 Dec 2020
It seems you just copied everything from the documentation and changed the input data.
I am just wondering if the training option is opimized for your data:
options = trainingOptions('adam',...
'MaxEpochs',250,...
'GradientThreshold',1,...
'InitialLearnRate',0.005,...
'LearnRateSchedule','piecewise',...
'LearnRateDropPeriod',125, ...
'LearnRateDropFactor',0.2, ...
'Verbose',0, ...
'Plots','training-progress');
Are you sure that the training has converged well?
If you are not sure if the trainng was successful you should consult someone knowledgeble or put the screen grab here.
I can give you some advice regarding to the option:
  1. InitialLearnRate
  2. Schedule
  3. DropPeriod
  4. DropFactor
  5. MaxEpochs
need optimizing. You can use "Experimental manager App" to tune them.
After that you can increase/decrease the number of hidden layer and also you can put more LSTM layers if you like.
  4 Comments
Giuseppe D'Amico
Giuseppe D'Amico on 23 Dec 2020
I installed the tool for machine learning but the experiment manager is not there
Hiro Yoshino
Hiro Yoshino on 24 Dec 2020
The experimental manager app is available from R2020a onward.

Sign in to comment.


Giuseppe D'Amico
Giuseppe D'Amico on 24 Dec 2020
ok thank you, I downloaded the experimental app, could you give me some advice on how to set the trainingoptions?
is the "adam" method okay?
  4 Comments
jialun chen
jialun chen on 1 Jan 2021
Hi Giuseppe, have you find any solution regarding to this? I am also very interested.
Giuseppe D'Amico
Giuseppe D'Amico on 1 Jan 2021
Unfortunately no, I don't think I understand how to use the experimental manager.
I tried to load the script I created into the setup function and replace the values of InitialLearnRate, schedule, etc. with the values set in the experiment but the result is that the output network has a straight line that does not follow in any way the real course of my problem.
I can't understand, is this problem due to an incorrect network configuration? Would anyone know how to help me?

Sign in to comment.

Categories

Find more on Sequence and Numeric Feature Data Workflows in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!