nlssest function help: How do I extract neural state space training losses to plot. MATLAB generates an automatic plot (neural state space); I want it in a different way

2 views (last 30 days)
I am using the nlssest function to train my neural network. MATLAB generates an automatic plot, which is okay; however, I want to replot that information in a way I can easily describe. However, I could not find a way to save the training loss (error) data. I checked online but did not find that.
Could you assist me. ribe.

Answers (1)

Umar
Umar on 8 Nov 2024

Hi @Bay Jay,

To save the training loss data generated by the nlssest function in MATLAB, you can utilize the TrainingOptions structure to specify the output of the training process. The nlssest function does not directly provide a way to save the training loss data, but you can achieve this by using a custom training function that captures the loss at each iteration. You will need to create a custom training function that records the training loss at each iteration. This function will be passed to the nlssest function.

function [lossHistory] = customTrainingFunction(~, state)
  persistent lossData;
  if isempty(lossData)
      lossData = [];
  end
    if isequal(state.Event, 'iteration')
        lossData = [lossData; state.TrainingLoss]; % Capture the training loss
    end
    lossHistory = lossData; % Return the loss history
  end

Then, set up the training options to use your custom training function.

% Define your model and data
% For example, let's assume you have a simple model and data
data = rand(100, 1); % Example data
model = nlarx(data, [1 1]); % Example model
% Set training options
options = nlssestOptions('MaxIter', 100, 'PlotLossFcn', 
@customTrainingFunction);

Now, train your model using the nlssest function while capturing the training loss data.

% Train the model
[trainedModel, lossHistory] = nlssest(model, data, options);

After training, save the lossHistory variable to a file or plot it as needed.

% Save the loss history to a .mat file
save('lossHistory.mat', 'lossHistory');
% Plot the training loss
figure;
plot(lossHistory);
title('Training Loss Over Iterations');
xlabel('Iteration');
ylabel('Loss');
grid on;

Note: nlarx requires System Identification Toolbox.

Hope this helps.

If you have any further questions or need additional assistance, feel free to ask!

Community Treasure Hunt

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

Start Hunting!