How capture the error value for each epoch?

6 次查看(过去 30 天)
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.

采纳的回答

Caio
Caio 2024-8-22
编辑:Caio 2024-8-22

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);

更多回答(2 个)

Umar
Umar 2024-8-21

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 个评论
Caio
Caio 2024-8-22
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.

请先登录,再进行评论。


Shantanu Dixit
Shantanu Dixit 2024-8-21
编辑:Shantanu Dixit 2024-8-21
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

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by