How capture the error value for each epoch?

8 views (last 30 days)
Caio
Caio on 20 Aug 2024
Edited: Caio on 22 Aug 2024
Hello, everyone!
I'm having a problem regarding my college project 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.

Accepted Answer

Caio
Caio on 22 Aug 2024
Edited: Caio on 22 Aug 2024

I managed to solve it using MATLAB's own resource

I trained the Neural Network model without the for loop, and captured the data from the info structure.
[net, info] = trainNetwork(XTrain, YTrain, layers, options);
disp(info);
Therefore, I selected ValidationRMSE e TrainingRMSE and transformed them into a column vector.
path = '...';
if ~exist(path, 'dir')
mkdir(path);
end
% TrainingRMMSE
trainingRMSE = info.TrainingRMSE;
trainingRMSETable = array2table(trainingRMSE(:), 'VariableNames', {'TrainingRMSE'});
% ValidationRMSE
validationRMSE = info.ValidationRMSE;
validationRMSETable = array2table(validationRMSE(:), 'VariableNames', {'ValidationRMSE'});
combinedTable = [trainingRMSETable, validationRMSETable];
Finally I saved the .xlsx file.
fileName = 'rmse.xlsx';
savePath = fullfile(path, fileName);
writetable(combinedTable, path);

More Answers (2)

Umar
Umar on 21 Aug 2024

Hi Caio ,

To address your query regarding, “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.”

Please see my response to your comments below.

I enhanced your existing code which involves modifying the training process to capture RMSE values at each epoch and then use MATLAB's built-in functions to export the data to an Excel file. So, first adjust the training loop so that RMSE values are calculated after each epoch which can be done by utilizing the trainNetwork function's output, which provides information about the training process. Then, define a function to calculate RMSE, which will be called after each epoch. Finally, use the writetable function to export the RMSE values to an Excel file. Here is example code snippet that incorporated all these steps.

% Sample Data
numEpochs = 10; % Define the number of epochs
XTrain = rand(100, 10); % Example training data (100 samples, 10 features)
YTrain = rand(100, 1); % Example target data (100 samples)
% Define your neural network layers and training options
layers = [ ...
  featureInputLayer(10)
  fullyConnectedLayer(1)
  regressionLayer];
options = trainingOptions('adam', ...
  'MaxEpochs', numEpochs, ...
  'Verbose', 0, ...
  'Plots', 'none');
% Initialize RMSE values array
rmseValues = zeros(numEpochs, 1);
% Training loop
for epoch = 1:numEpochs
  % Train the network
  [net, info] = trainNetwork(XTrain, YTrain, layers, options);
    % Predict using the trained network
    YPred = predict(net, XTrain);
    % Calculate RMSE
    rmseValues(epoch) = calculateRMSE(YTrain, YPred);
  end
% Create a table for RMSE values
rmseTable = array2table(rmseValues, 'VariableNames', {'RMSE'});
% Display the RMSE table
disp(rmseTable);
% Export RMSE values to an Excel file
writetable(rmseTable, 'RMSE_Values.xlsx');
% Function to calculate RMSE
function rmse = calculateRMSE(actual, predicted)
  rmse = sqrt(mean((actual - predicted).^2));
end

Please see attached results.

So, as you can see in the example code, I created random sample data for training. You should replace this with your actual dataset.The neural network is defined with a simple architecture suitable for regression tasks and the training options are set, including the optimizer and the maximum number of epochs. After each epoch, the predicted values are compared to the actual values to compute the RMSE using the calculateRMSE function and stored in a table. Afterwards, they are exported to an Excel file named RMSE_Values.xlsx. By following the provided code and explanations, you should be able to adapt it to your specific project needs. If you have any further questions or require additional modifications, feel free to ask!

  1 Comment
Caio
Caio on 22 Aug 2024
Thank you for responding. From your idea I can develop the code bellow that solves my problem. I did it in a simplified way using MATLAB's own resources.

Sign in to comment.


Shantanu Dixit
Shantanu Dixit on 21 Aug 2024
Edited: Shantanu Dixit on 21 Aug 2024
Hi Caio,
To save the rmse values to a .xlsx file after training the model use ‘writeTable’ as writetable(rmseTable,’rmseValues.xlsx’)
Refer to the following MathWorks documentation

Categories

Find more on Image Data Workflows in Help Center and File Exchange

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!