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

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 Comments

Hi there, can you maybe post some code with what you tried to do already?
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

Sign in to comment.

 Accepted Answer

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 Comments

I understand. Thank you for the prompt response.
However, I am having the problem of denormalizing the predicted value from the nssmodel. Once the training is complete and when I try to evaluate the system, nssmodel yield different dxdt value compared to the true dxdt. Even though the training and validation with normalized values showed good results. Do you have suggestions?
Thank you.
The suggestion I posted is for how to "de-normalize" predicted values from nssmodel. Have you tried that already? If you are still having issues, then it would be best to contact tech support so that they can take a look at your code and data for a deeper dive into the problem.
I am not sure how I can apply it to the nssmodel. The previous code, for dataset "Tdata" is the dataset created for the training and validation purposes, which I obtained from first-principle model.
I can quickly de-normalize the data using your suggestion. However, my problem is with generated data from nssmodel (not from Tdata). After the training, following this code:
% Train the neural state-space model
tic
nssModel = nlssest(Expts,nssModel,StateOpt)
toc
Matlab releases the nssModel that includes Reports of the training, StateNetwork and more, as follows:
When I try to evaluate the nssmodel with the following code, the results are still under normalized value. I am not sure how to implement the de-normalize code by taking out the predicted data from the nssModel. Do you have suggestions? Thank you.
x0=randn(9,1);
u0=randn(3,1);
dxdt1 = evaluate(nssModel,x0,u0)
truthdxdt = StatFcn(x0,u0)
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

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!