Caio Nicácio
Caio Nicácio

Reputation: 1

How capture the error value for each epoch in MATLAB?

everyone! I'm having a problem regarding my Deep Learning project, in the image regression task, and I would like to know how I could obtain the RMSE value at each epoch of my model's execution, and export in a .xlsx archive.

rmseValues = zeros(numEpochs, 1);
for epoch = 1:numEpochs
    [net, info] = trainNetwork(XTrain, YTrain, layers, options);
    YPred = predict(net, XTrain);
    rmseValues(epoch) = calculateRMSE(YTrain, YPred);
end
rmseTable = array2table(rmseValues, 'VariableNames', {'RMSE'});
disp(rmseTable)

However, due to the nature of the for loop, training is performed at the same rate as the number of epochs defined. I would therefore like to know if there is a function that allows me, for example, to obtain the array of execution data.

The motivation is that I would like to use this data for another project.

I want to export the data in a .xlsx or .csv archive but I wanna discover the approach to obtain.

Upvotes: 0

Views: 34

Answers (1)

lvand
lvand

Reputation: 375

To capture the RMSE value at each epoch during the training process in MATLAB, you can use a custom training loop or modify the training options so that it has custom functions that calculate the RMSE at each epoch.

Using a custom training loow allows you to have control over the training processes. To do this you need to:

  • Initialize your variables (so you can store your RMSE values)
  • Train the network manually, updating your network parameters and calculate RMSE
  • Save your RMSE values to CSV file

Heres an example that I would recommend:

% Initialize variables
numEpochs = 50;  % Define your number of epochs
rmseValues = zeros(numEpochs, 1);

% Custom training loop
for epoch = 1:numEpochs
    % Perform a training step
    [net, info] = trainNetwork(XTrain, YTrain, layers, options);
    
    % Make predictions
    YPred = predict(net, XTrain);
    
    % Calculate RMSE
    rmseValues(epoch) = sqrt(mean((YTrain - YPred).^2));
    
    % Optionally, display the RMSE value for this epoch
    fprintf('Epoch %d: RMSE = %.4f\n', epoch, rmseValues(epoch));
end

% Convert to table and save to .xlsx or .csv
rmseTable = array2table(rmseValues, 'VariableNames', {'RMSE'});
writetable(rmseTable, 'RMSE_per_epoch.xlsx');  % Save as .xlsx
% Alternatively, you can use writetable(rmseTable, 'RMSE_per_epoch.csv'); for CSV

If that isn't your forte or preference, you can use trainingOptions with a custom outputFcn which captures the RMSE. The approach for this is as follows:

  • Define custom output functions
  • Set your trainingOptions
  • Export your data

Heres a coding outline for assistance:

% Initialize a variable to store RMSE values
rmseValues = [];

% Define the custom output function
function stop = myOutputFcn(info)
    global rmseValues;
    if info.State == 'iteration'
        YPred = predict(info.TrainingAccuracy); % Adjust for your needs
        rmse = sqrt(mean((info.TrainingLoss - YPred).^2));  % Replace TrainingLoss with your targets
        rmseValues = [rmseValues; rmse];
    end
    stop = false;  % Continue training
end

% Set training options with custom output function
options = trainingOptions('sgdm', ...
    'MaxEpochs', numEpochs, ...
    'OutputFcn', @myOutputFcn);

% Train the network
net = trainNetwork(XTrain, YTrain, layers, options);

% Export RMSE values to .xlsx or .csv
rmseTable = array2table(rmseValues, 'VariableNames', {'RMSE'});
writetable(rmseTable, 'RMSE_per_epoch.xlsx');  % Save as .xlsx

This approach will log RMSE and export data alike option 1. In my opinion, the first option is the better one and I would recommend you stick to that one.

Upvotes: 0

Related Questions