Want to add regularization (L2) in Resnet50 code
4 次查看(过去 30 天)
显示 更早的评论
Hi guys,
I am training my data using Resnet50 in CNN but data is overfit. I want to reduce overfitting. So I want to add Regularizatin L2. Can anybody tell me how to add L2 in my code. You can see my code below.
clear all
close all
imds = imageDatastore("E:\test\data", ...
'IncludeSubfolders',true,'LabelSource','foldernames');
[imdsTrain,imdsValidation] = splitEachLabel(imds,0.7,'randomize'); %70% for train 30% for test
net=resnet50; % for the first time,you have to download the package from Add-on explorer
%Replace Final Layers
numClasses = numel(categories(imdsTrain.Labels));
lgraph = layerGraph(net);
newFCLayer = fullyConnectedLayer(numClasses,'Name','new_fc','WeightLearnRateFactor',10,'BiasLearnRateFactor',10);
lgraph = replaceLayer(lgraph,'fc1000' ,newFCLayer);
newClassLayer = classificationLayer('Name','new_classoutput');
lgraph = replaceLayer(lgraph,'ClassificationLayer_predictions',newClassLayer);
%Train Network
inputSize = net.Layers(1).InputSize;
augimdsTrain = augmentedImageDatastore(inputSize(1:2),imdsTrain);
augimdsValidation = augmentedImageDatastore(inputSize(1:2),imdsValidation);
options = trainingOptions('sgdm', ...
'MiniBatchSize',10, ...
'MaxEpochs',20, ...
'InitialLearnRate',1e-3, ...
'Shuffle','every-epoch', ...
'ValidationData',augimdsValidation, ...
'ValidationFrequency',5, ...
'Verbose',false, ...
'Plots','training-progress');
trainedNet = trainNetwork(augimdsTrain,lgraph,options);
YPred = classify(trainedNet,augimdsValidation);
accuracy = mean(YPred == imdsValidation.Labels)
C = confusionmat(imdsValidation.Labels,YPred)
cm = confusionchart(imdsValidation.Labels,YPred);
cm.Title = 'Confusion Matrix for Validation Data';
cm.ColumnSummary = 'column-normalized';
cm.RowSummary = 'row-normalized';
0 个评论
采纳的回答
Srivardhan Gadila
2020-5-2
You can specify the L2 regularization factors for the weights and biases in Convolutional and Fully Connected Layers by specifying the BiasL2Factor and WeightL2Factor properties, respectively. trainNetwork then multiplies the L2 regularization factors that you specify by using trainingOptions with these factors.
For more informtion refer to setL2Factor, Solver Options, L2 Regularization, Set Up Parameters in Convolutional and Fully Connected Layers.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Deep Learning Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!