setL2Factor - Set L2 regularization factor of layer
learnable parameter - MATLAB ([original](https://www.mathworks.com/help/deeplearning/ref/nnet.cnn.layer.layer.setl2factor.html)) ([raw](?raw))
Set L2 regularization factor of layer learnable parameter
Syntax
Description
[layerUpdated](#mw%5Fdd743bc2-4cac-4541-9cdd-ac817d9a12f9%5Fsep%5Fmw%5Ff8685e87-8be2-4736-ad2c-16c7ab507509) = setL2Factor([layer](#d126e236600),[parameterName](#d126e236614),[factor](#d126e236626))
sets the L2 regularization factor of the parameter with the name parameterName
inlayer
to factor
.
For built-in layers, you can set the_L2_ regularization factor directly by using the corresponding property. For example, for aconvolution2dLayer
layer, the syntax layer = setL2Factor(layer,'Weights',factor)
is equivalent tolayer.WeightL2Factor = factor
.
[layerUpdated](#mw%5Fdd743bc2-4cac-4541-9cdd-ac817d9a12f9%5Fsep%5Fmw%5Ff8685e87-8be2-4736-ad2c-16c7ab507509) = setL2Factor([layer](#d126e236600),[parameterPath](#mw%5Fdd743bc2-4cac-4541-9cdd-ac817d9a12f9%5Fsep%5Fmw%5Fbb76b081-6fd1-4a89-9ec7-fd4394ad67fb),[factor](#d126e236626))
sets the L2 regularization factor of the parameter specified by the path parameterPath
. Use this syntax when the layer is a networkLayer or when the parameter is in adlnetwork
object in a custom layer.
[netUpdated](#mw%5Fdd743bc2-4cac-4541-9cdd-ac817d9a12f9%5Fsep%5Fmw%5Fc00b29df-6f5d-42ad-a5bc-9c6599af47ac) = setL2Factor([net](#mw%5Fdd743bc2-4cac-4541-9cdd-ac817d9a12f9%5Fsep%5Fmw%5F3ffb42b3-6af7-4c22-82b3-4fb0f6a399f2),[layerName](#mw%5Fdd743bc2-4cac-4541-9cdd-ac817d9a12f9%5Fsep%5Fmw%5F5e8103c3-6a84-4c6d-a782-b16533603493),[parameterName](#d126e236614),[factor](#d126e236626))
sets the L2 regularization factor of the parameter with the name parameterName
in the layer with name layerName
for the specified dlnetwork
object.
[netUpdated](#mw%5Fdd743bc2-4cac-4541-9cdd-ac817d9a12f9%5Fsep%5Fmw%5Fc00b29df-6f5d-42ad-a5bc-9c6599af47ac) = setL2Factor([net](#mw%5Fdd743bc2-4cac-4541-9cdd-ac817d9a12f9%5Fsep%5Fmw%5F3ffb42b3-6af7-4c22-82b3-4fb0f6a399f2),[parameterPath](#mw%5Fdd743bc2-4cac-4541-9cdd-ac817d9a12f9%5Fsep%5Fmw%5Fbb76b081-6fd1-4a89-9ec7-fd4394ad67fb),[factor](#d126e236626))
sets the L2 regularization factor of the parameter specified by the path parameterPath
. Use this syntax when the parameter is in a networkLayer or when the parameter is in adlnetwork
object in a custom layer.
Examples
Set and Get L2 Regularization Factor of Learnable Parameter
Set and get the L2 regularization factor of a learnable parameter of a layer.
Create a layer array containing the custom layer sreluLayer
, attached to this example as a supporting file. To access this layer, open this example as a live script.
Create a layer array including a custom layer sreluLayer
.
layers = [ imageInputLayer([28 28 1]) convolution2dLayer(5,20) batchNormalizationLayer sreluLayer fullyConnectedLayer(10) softmaxLayer];
Set the L2 regularization factor of the LeftSlope
learnable parameter of the sreluLayer
to 2.
layers(4) = setL2Factor(layers(4),"LeftSlope",2);
View the updated L2 regularization factor.
factor = getL2Factor(layers(4),"LeftSlope")
Set and Get L2 Regularization Factor of Custom Nested Layer Learnable Parameter
Set and get the L2 regularization factor of a learnable parameter of a custom nested layer defined using network composition.
Create a residual block layer using the custom layer residualBlockLayer
attached to this example as a supporting file. To access this file, open this example as a Live Script.
numFilters = 64; layer = residualBlockLayer(numFilters)
layer = residualBlockLayer with properties:
Name: ''
Learnable Parameters Network: [1x1 dlnetwork]
State Parameters Network: [1x1 dlnetwork]
Use properties method to see a list of all properties.
View the layers of the nested network.
ans = 7x1 Layer array with layers:
1 'conv_1' 2-D Convolution 64 3x3 convolutions with stride [1 1] and padding 'same'
2 'batchnorm_1' Batch Normalization Batch normalization
3 'relu_1' ReLU ReLU
4 'conv_2' 2-D Convolution 64 3x3 convolutions with stride [1 1] and padding 'same'
5 'batchnorm_2' Batch Normalization Batch normalization
6 'add' Addition Element-wise addition of 2 inputs
7 'relu_2' ReLU ReLU
Set the L2 regularization factor of the learnable parameter 'Weights'
of the layer 'conv_1'
to 2 using the setL2Factor
function.
factor = 2; layer = setL2Factor(layer,'Network/conv_1/Weights',factor);
Get the updated L2 regularization factor using the getL2Factor
function.
factor = getL2Factor(layer,'Network/conv_1/Weights')
Set and Get L2 Regularization Factor of dlnetwork
Learnable Parameter
Set and get the L2 regularization factor of a learnable parameter of a dlnetwork
object.
Specify the layers of the classification branch and add them to the network.
net = dlnetwork; layers = [ imageInputLayer([28 28 1],'Normalization','none','Name','in') convolution2dLayer(5,20,'Name','conv') batchNormalizationLayer('Name','bn') reluLayer('Name','relu') fullyConnectedLayer(10,'Name','fc') softmaxLayer('Name','sm')];
net = addLayers(net,layers);
Set the L2 regularization factor of the 'Weights'
learnable parameter of the convolution layer to 2 using the setL2Factor
function.
factor = 2; net = setL2Factor(net,'conv','Weights',factor);
Get the updated L2 regularization factor using the getL2Factor
function.
factor = getL2Factor(net,'conv','Weights')
Set and Get L2 Regularization Factor of Nested Layer Learnable Parameter
Create an array of layers containing an lstmLayer
with 100 hidden units and a dropoutLayer
with a dropout probability of 0.2.
layers = [lstmLayer(100,OutputMode="sequence",Name="lstm") dropoutLayer(0.2,Name="dropout")];
Create a network layer containing these layers.
lstmDropoutLayer = networkLayer(layers,Name="lstmDropout");
Use the network layer to build a network.
layers = [sequenceInputLayer(3) lstmDropoutLayer lstmDropoutLayer fullyConnectedLayer(10) softmaxLayer];
Create a dlnetwork
object. You can also create a dlnetwork
object by training the network using the trainnet function.
Set the L2 regularization factor of the InputWeights
learnable parameter of the LSTM layer in the first network layer to 2 using the setL2Factor
function.
factor = 2; net = setL2Factor(net,"lstmDropout_1/lstm/InputWeights",factor);
Get the updated L2 regularization factor using the getL2Factor
function.
factor = getL2Factor(net,"lstmDropout_1/lstm/InputWeights")
Set and Get L2 Regularization Factor of Custom Nested dlnetwork
Learnable Parameter
Set and get the L2 regularization factor of a learnable parameter of a custom nested layer defined using network composition 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(numFilters,'Name','res1') residualBlockLayer(numFilters,'Name','res2') residualBlockLayer(2numFilters,'Stride',2,'IncludeSkipConvolution',true,'Name','res3') residualBlockLayer(2numFilters,'Name','res4') residualBlockLayer(4numFilters,'Stride',2,'IncludeSkipConvolution',true,'Name','res5') residualBlockLayer(4numFilters,'Name','res6') globalAveragePooling2dLayer('Name','gap') fullyConnectedLayer(numClasses,'Name','fc') softmaxLayer('Name','sm')];
dlnet = dlnetwork(layers);
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,:)
ans=8×3 table
Layer Parameter Value
______ ____________________________ ___________________
"res1" "Network/conv_1/Weights" {3x3x32x32 dlarray}
"res1" "Network/conv_1/Bias" {1x1x32 dlarray}
"res1" "Network/batchnorm_1/Offset" {1x1x32 dlarray}
"res1" "Network/batchnorm_1/Scale" {1x1x32 dlarray}
"res1" "Network/conv_2/Weights" {3x3x32x32 dlarray}
"res1" "Network/conv_2/Bias" {1x1x32 dlarray}
"res1" "Network/batchnorm_2/Offset" {1x1x32 dlarray}
"res1" "Network/batchnorm_2/Scale" {1x1x32 dlarray}
For the layer "res1"
, set the L2 regularization factor of the learnable parameter 'Weights'
of the layer 'conv_1'
to 2 using the setL2Factor
function.
factor = 2; dlnet = setL2Factor(dlnet,'res1/Network/conv_1/Weights',factor);
Get the updated L2 regularization factor using the getL2Factor
function.
factor = getL2Factor(dlnet,'res1/Network/conv_1/Weights')
Input Arguments
layer
— Input layer
scalar Layer
object
Input layer, specified as a scalar Layer
object.
parameterName
— Parameter name
character vector | string scalar
Parameter name, specified as a character vector or a string scalar.
factor
— L2 regularization factor
nonnegative scalar
L2 regularization factor for the parameter, specified as a nonnegative scalar.
The software multiplies this factor with the global_L2_ regularization factor to determine the L2 regularization factor for the specified parameter. For example, iffactor
is 2
, then the_L2_ regularization for the specified parameter is twice the global_L2_ regularization factor. You can specify the global L2 regularization factor using the trainingOptions function.
Example: 2
parameterPath
— Path to parameter in nested layer
string scalar | character vector
Path to parameter in nested layer, specified as a string scalar or a character vector. A nested layer can be a layer within a networkLayer or a custom layer that itself defines a neural network as a learnable parameter.
If the input to setL2Factor
is a layer, then:
- If the nested layer is in a network layer, the parameter path has the form
"nestedLayerName/parameterName"
wherenestedlayerName
is the name of the nested layer inside the network layer, andparameterName
is the name of the parameter. If there are multiple levels of nested layers, then specify the path using the formnestedLayerName1/.../nestedLayerNameN/parameterName
. - If the nested layer is a custom layer that itself defines a neural network as a learnable parameter, the parameter path has the form
"propertyName/layerName/parameterName"
wherepropertyName
is the name of the property containing adlnetwork
object,layerName
is the name of the layer in thedlnetwork
object, andparameterName
is the name of the parameter. If there are multiple levels of nested layers, then specify the path using the form"propertyName1/layerName1/.../propertyNameN/layerNameN/parameterName"
.
If the input to setL2Factor
is a dlnetwork
object and the desired parameter is in a nested layer, then:
- If the nested layer is in a network layer, the parameter path has the form
"networkLayerName/nestedLayerName/parameterName"
wherenetworkLayerName
is the name of the network layer,nestedlayerName
is the name of the nested layer inside the network layer, andparameterName
is the name of the parameter. If there are multiple levels of nested layers, then specify the path using the form"networkLayerName1/.../networkLayerNameN/nestedLayerName/parameterName"
. - If the nested layer is a custom layer that itself defines a neural network as a learnable parameter, the parameter path has the form
"customLayerName1/propertyName/layerName/parameterName"
, wherelayerName1
is the name of the layer in the inputdlnetwork
object,propertyName
is the name of the property of the layer containing adlnetwork
object,layerName
is the name of the layer in thedlnetwork
object, andparameterName
is the name of the parameter. If there are multiple levels of nested layers, then specify the path using the form"customLayerName1/propertyName1/.../customLayerNameN/propertyNameN/layerName/parameterName"
.
Data Types: char
| string
net
— Neural network
dlnetwork
object
Neural network, specified as a dlnetwork object.
layerName
— Layer name
string scalar | character vector
Layer name, specified as a string scalar or a character vector.
Data Types: char
| string
Output Arguments
layerUpdated
— Updated layer
Layer
object
Updated layer, returned as a Layer
.
netUpdated
— Updated network
dlnetwork
object
Updated network, returned as a dlnetwork.
Version History
Introduced in R2017b
R2024a: Specify Path to Parameter in Network Layer
Specify the path to a parameter in a networkLayer using the parameterPath
argument.