Хочу добавить регуляризацию (L2) в matlab Resnet50 - PullRequest
0 голосов
/ 02 мая 2020

Я тренирую свои данные, используя Resnet50 в CNN, но данные перегоняются. Я хочу уменьшить переоснащение. Поэтому я хочу добавить Регуляризацию L2. Кто-нибудь может сказать мне, как добавить L2 в моем коде? Вы можете увидеть мой код ниже.

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';
...