%% Set and Get L2 Regularization Factor of Nested dlnetwork Learnable Parameter
% Set and get the L2 regularization factor of a learnable parameter of a nested layer in a dlnetwork object.
% Create a dlnetwork object containing the custom layer residualBlockLayer attached to this example as a supporting file. To access this file, open this example as a Live Script.

inputSize = [224 224 3];
numFilters = 32;
numClasses = 5;

layers = [
    imageInputLayer(inputSize,'Normalization','none','Name','in')
    convolution2dLayer(7,numFilters,'Stride',2,'Padding','same','Name','conv')
    groupNormalizationLayer('all-channels','Name','gn')
    reluLayer('Name','relu')
    maxPooling2dLayer(3,'Stride',2,'Name','max')
    residualBlockLayer([56 56 numFilters],numFilters,'Name','res1')
    residualBlockLayer([56 56 numFilters],numFilters,'Name','res2')
    residualBlockLayer([56 56 numFilters],2*numFilters,'Stride',2,'IncludeSkipConvolution',true,'Name','res3')
    residualBlockLayer([28 28 2*numFilters],2*numFilters,'Name','res4')
    residualBlockLayer([28 28 2*numFilters],4*numFilters,'Stride',2,'IncludeSkipConvolution',true,'Name','res5')
    residualBlockLayer([14 14 4*numFilters],4*numFilters,'Name','res6')
    globalAveragePooling2dLayer('Name','gap')
    fullyConnectedLayer(numClasses,'Name','fc')
    softmaxLayer('Name','sm')];

lgraph = layerGraph(layers);
dlnet = dlnetwork(lgraph);

% The Learnables property of the dlnetwork object is a table that contains the learnable parameters of the network. The table includes parameters of nested layers in separate rows. View the learnable parameters of the layer "res1".

learnables = dlnet.Learnables;
idx = learnables.Layer == "res1";
learnables(idx,:)

% For the layer "res1", set the L2 regularization factor of the learnable parameter 'Weights' of the layer 'conv1' to 2 using the setL2Factor function.

factor = 2;
dlnet = setL2Factor(dlnet,'res1/Network/conv1/Weights',factor);

% Get the updated L2 regularization factor using the getL2Factor function.

factor = getL2Factor(dlnet,'res1/Network/conv1/Weights')

% Copyright 2020 The MathWorks, Inc.