Prepare dataset for Neural State Space to be used as StateFcn in nlmpc

38 vues (au cours des 30 derniers jours)
Hello,
I am trying to use the neural networks using the Neural State Space Models in MATLAB to be used as a state function in nonlinear mpc toolbox. During the training and validation process I want to use normalize data of the dataset to yield a generalizable data. However, I am not sure how to denormalize the data once the training and validation have been conducted. Can anyone one help me with this?
Thank you in advance.
  3 commentaires
Saskia Putri
Saskia Putri le 27 Nov 2023
Yes, I have posted it. Thank you.
Saskia Putri
Saskia Putri le 27 Nov 2023
Attached is the code. I have timetable data with data length of 120,000. 9 Outputs and 3 Inputs.
clc
clear
load TTdata.mat
%%
TDataN = normalize(TTdata);
%%
% Split the data into estimation (first 60000 data points) and validation (remaining data points) portions.
eData = TDataN(1:6e5,:); % portion used for estimation
vData = TDataN(6e5+1:end,:); % portion used for validation
% Downsample the training data. data length (6000)
eDataD = idresamp(eData,[1 100]);
vDataD = idresamp(vData,[1 100]);
%%
eDataD.Properties.TimeStep
height(eDataD)
%% Model Training
% Designating the input and output signals from the list of variables in the eData timetable.
Inputs = ["Icpl", "Ippl","MVopt"];
Output = ["V0", "Isga", "Isgb", "Iba", "Ibb", "Isca", "Iscb", "Vsca", "Vscb"];
%% Create a neural state-space model by using the idNeuralStateSpace constructor.
% Define a neural state-space model
nx = 9; % number of states = number of outputs
nssModel = idNeuralStateSpace(nx,NumInputs=3);
nssModel.InputName = Inputs;
nssModel.OutputName = Output;
% Configure the state network f()
nssModel.StateNetwork = createMLPNetwork(nssModel,"state", ...
LayerSizes=[128 128], ...
WeightsInitializer="glorot", ...
BiasInitializer="ones", ...
Activations='tanh');
%%
% Next, prepare the data and the training algorithm options.
% The data has already been split, downsampled and normalized.
% You now create multiple data experiments for training by splitting the dataset eDataD into overlapping segments.
% Doing so effectively reduces the prediction horizon from the original data length (6000) to the length of the individual segments.
% This reduction can sometimes lead to more generalizable results.
predictionStep = 20; % length of each data segment
numExperiment = height(eDataD) - predictionStep;
Expts = cell(1, numExperiment);
for i = 1:numExperiment
Expts{i} = eDataD(i:i+predictionStep,:);
if i>1
% set the row time of each segment to be identical; this is a requirement for training a
% neural state-space model with multiple data experiments
Expts{i}.Properties.RowTimes = Expts{1}.Properties.RowTimes;
end
end
%%
% Use the nssTrainingOptions command to create the set of training options. Pick Adam as the training solver. Set the maximum number of training epochs and the data interpolation method.
StateOpt = nssTrainingOptions('adam');
StateOpt.MaxEpochs = 300;
% StateOpt.LearnRate =0.001;
% StateOpt.MiniBatchSize = 300;
% Train the neural state-space model
tic
nssModel = nlssest(Expts,nssModel,StateOpt)
toc

Connectez-vous pour commenter.

Réponse acceptée

Arkadiy Turevskiy
Arkadiy Turevskiy le 27 Nov 2023
Thanks for posting the code.
To de-normalize the data you need to save mean and standard deviation data used for normalization.
[TdataN,C,S]=normalize(Tdata);
% now train neural state space, use it to predict normalized data PdataN
% using sim
% Now you can "de-normalize"
Pdata=PdataN.*S+C;
HTH
Arkadiy
  5 commentaires
Arkadiy Turevskiy
Arkadiy Turevskiy le 30 Nov 2023
Hi,
In your case it looks like the outputs (same as states) are the last 9 columns of TTdata, right?
So the bias and standard deviation info you need are in the last 9 columns of C and S in my code snippet in the answer.
Take those and use to denormalize your state derivatives/states/outputs as needed.
[TTdataN,C,S]=normalize(TTdata);
% your code to train neural state space model goes here
% you compute state derivatives dxdt1 as in your code above
% Now you can "de-normalize" state derivaties
% The code below assumes TTdata has 12 columns, the first 3 columns are
% inputs, and the last 9 are states/outputs
Cstate=C(4:length(C));
Sstate=S(4:length(S));
dxdt1_denormalized=dxdt1.*Sstate+Cstate;
Hth
Saskia Putri
Saskia Putri le 4 Déc 2023
I understand. Thank you.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Sequence and Numeric Feature Data Workflows dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by