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

16 ビュー (過去 30 日間)
Bay Jay
Bay Jay 2024 年 11 月 8 日 9:21
回答済み: Umar 2024 年 11 月 8 日 13:06
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.
  2 件のコメント
Walter Roberson
Walter Roberson 2024 年 11 月 8 日 9:27
To avoid the plot, create nssTrainingADAM, nssTrainingSGDM, nssTrainingRMSProp, or nssTrainingLBFGS object in which PlotLossFcn is set to false.
Bay Jay
Bay Jay 2024 年 11 月 8 日 9:48
編集済み: Bay Jay 2024 年 11 月 8 日 9:49
I actually want the plot. But I want the data of the plot saved and plotted my own way.

サインインしてコメントする。

回答 (1 件)

Umar
Umar 2024 年 11 月 8 日 13:06

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!

Translated by