You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在MATLAB中将卷积神经网络恢复至验证精度最优的迭代状态

Automatically Revert to Best Validation Accuracy Model After Early Stopping in MATLAB trainNetwork

Great question! I’ve dealt with this exact frustration before—manual digging through auto-generated model files is such a hassle. Here’s how to set up MATLAB’s trainNetwork to automatically grab the best validation accuracy model after early stopping, no manual work required:

1. Use Built-in Best Model Saving (Simplest Approach)

MATLAB’s trainingOptions has a built-in flag to save the best-performing model based on validation metrics, paired with early stopping via ValidationPatience. Here’s the setup:

% Define your training options
opts = trainingOptions('adam', ...
    'ValidationData', valData, % Your prepped validation dataset
    'ValidationPatience', x, % Number of stagnant iterations to trigger early stop
    'SaveBestModel', true, % Critical: Auto-saves the model with best validation accuracy
    'CheckpointPath', './model_checkpoints', % Directory to store model files
    'Verbose', true);

% Train your shallow CNN
trainedNet = trainNetwork(trainData, layers, opts);

% After early stopping, load the OPTIMAL model automatically
bestNet = load(fullfile(opts.CheckpointPath, 'bestnet.mat')).net;

Key Notes:

  • When SaveBestModel is enabled, MATLAB only updates bestnet.mat in your CheckpointPath when validation accuracy improves—so this file is always your top-performing model.
  • No more sorting through random auto-generated checkpoint filenames; the best model is clearly labeled and ready to load.

2. Custom Callback for Fine-Grained Control (Optional)

If you need extra flexibility (like logging the exact iteration of the best model, or customizing save rules), use a validation callback to track and save the best model manually:

% Custom callback to monitor and save the best model
function trackBestModel(info)
    persistent bestAccuracy bestNet
    if isempty(bestAccuracy)
        bestAccuracy = 0;
    end
    % Update best model if current validation accuracy is better
    if info.ValidationAccuracy > bestAccuracy
        bestAccuracy = info.ValidationAccuracy;
        bestNet = info.Net;
        % Save to a custom-named file with extra metadata
        save('./custom_best_model.mat', 'bestNet', 'bestAccuracy');
        fprintf('Saved new best model with accuracy: %.2f%%\n', bestAccuracy);
    end
end

% Set up training options with the callback
opts = trainingOptions('adam', ...
    'ValidationData', valData, ...
    'ValidationPatience', x, ...
    'ValidationFrequency', 1, % Validate after every iteration
    'Callbacks', {@(info) trackBestModel(info)}, % Attach the custom logic
    'Verbose', true);

% Train and load your custom-saved best model
trainNetwork(trainData, layers, opts);
bestNet = load('./custom_best_model.mat').bestNet;

This approach lets you add extra logic (like writing logs to a text file) and avoids relying on MATLAB’s default naming conventions.

Alignment with Your Reference

This method implements the same early stopping logic from the paper you referenced—prioritizing the model with the best validation performance to avoid overfitting, all without manual file hunting.

内容的提问来源于stack exchange,提问作者S.MC.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:10:58