Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我想在MatlabResNet50中添加正则化(L2)_Matlab_Machine Learning_Image Processing_Deep Learning_Conv Neural Network - Fatal编程技术网

我想在MatlabResNet50中添加正则化(L2)

我想在MatlabResNet50中添加正则化(L2),matlab,machine-learning,image-processing,deep-learning,conv-neural-network,Matlab,Machine Learning,Image Processing,Deep Learning,Conv Neural Network,我正在使用CNN中的Resnet50训练我的数据,但数据拟合过度。我想减少过度装修。所以我想添加正则化L2。有人能告诉我如何在代码中添加L2吗?你可以在下面看到我的代码 clear all close all imds = imageDatastore("E:\test\data", ... 'IncludeSubfolders',true,'LabelSource','foldernames'); [imdsTrain,imdsValidation] = splitEachLab

我正在使用CNN中的Resnet50训练我的数据,但数据拟合过度。我想减少过度装修。所以我想添加正则化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';

在ResNet50 CNN模型上实现L2正则化

resnet_base = ResNet50(weights='imagenet', include_top=False, input_shape=(224,224,3))
alpha = 0.00002
for layer in resnet_base.layers:
  if isinstance(layer, keras.layers.Conv2D) or isinstance(layer, keras.layers.Dense):
    layer.add_loss(keras.regularizers.l2(alpha)(layer.kernel))
  if hasattr(layer, 'bias_regularizer') and layer.use_bias:
    layer.add_loss(keras.regularizers.l2(alpha)(layer.bias))

希望这有帮助

在ResNet50 CNN模型上实现L2正则化

resnet_base = ResNet50(weights='imagenet', include_top=False, input_shape=(224,224,3))
alpha = 0.00002
for layer in resnet_base.layers:
  if isinstance(layer, keras.layers.Conv2D) or isinstance(layer, keras.layers.Dense):
    layer.add_loss(keras.regularizers.l2(alpha)(layer.kernel))
  if hasattr(layer, 'bias_regularizer') and layer.use_bias:
    layer.add_loss(keras.regularizers.l2(alpha)(layer.bias))

希望这有帮助

下一次,请花一分钟来看看如何正确格式化您的代码(这次为您完成了)。非常感谢您的编辑。下次我会处理好的。你能回答我的问题吗?下一次,请花一分钟看看如何正确格式化你的代码(这次为你完成了)。非常感谢你的编辑。下次我会处理好的。你能回答我的问题吗。