removeParameter
Remove parameter fromONNXParameters
object
Description
params = removeParameter(
removes the parameter specified byparams
,name
)name
from theONNXParameters
objectparams
.
Examples
Remove Parameters from Imported ONNX Model Function
Import a network saved in the ONNX format as a function and modify the network parameters.
Import the pretrainedsimplenet3fc.onnx
network as a function.simplenet3fc
is a simple convolutional neural network trained on digit image data. For more information on how to create a network similar tosimplenet3fc
, seeCreate Simple Image Classification Network.
Importsimplenet3fc.onnx
usingimportONNXFunction
, which returns anONNXParameters
object that contains the network parameters. The function also creates a new model function in the current folder that contains the network architecture. Specify the name of the model function assimplenetFcn
.
params = importONNXFunction('simplenet3fc.onnx','simplenetFcn');
A function containing the imported ONNX network has been saved to the file simplenetFcn.m. To learn how to use this function, type: help simplenetFcn.
Display the parameters that are updated during training (params.Learnables
) and the parameters that remain unchanged during training (params.Nonlearnables
).
params.Learnables
ans =struct with fields:imageinput_Mean: [1×1 dlarray] conv_W: [5×5×1×20 dlarray] conv_B: [20×1 dlarray] batchnorm_scale: [20×1 dlarray] batchnorm_B: [20×1 dlarray] fc_1_W: [24×24×20×20 dlarray] fc_1_B: [20×1 dlarray] fc_2_W: [1×1×20×20 dlarray] fc_2_B: [20×1 dlarray] fc_3_W: [1×1×20×10 dlarray] fc_3_B: [10×1 dlarray]
params.Nonlearnables
ans =struct with fields:ConvStride1004: [2×1 dlarray] ConvDilationFactor1005: [2×1 dlarray] ConvPadding1006: [4×1 dlarray] ConvStride1007: [2×1 dlarray] ConvDilationFactor1008: [2×1 dlarray] ConvPadding1009: [4×1 dlarray] ConvStride1010: [2×1 dlarray] ConvDilationFactor1011: [2×1 dlarray] ConvPadding1012: [4×1 dlarray] ConvStride1013: [2×1 dlarray] ConvDilationFactor1014: [2×1 dlarray] ConvPadding1015: [4×1 dlarray]
The network has parameters that represent three fully connected layers. To see the parameters of the convolutional layersfc_1
,fc_2
, andfc_3
, open the model functionsimplenetFcn
.
opensimplenetFcn
Scroll down to the layer definitions in the functionsimplenetFcn
. The code below shows the definitions for layersfc_1
,fc_2
, andfc_3
.
% Conv:(重量、偏见、跨步、dilationFactor、填充,dataFormat, NumDims.fc_1] = prepareConvArgs(Vars.fc_1_W, Vars.fc_1_B, Vars.ConvStride1007, Vars.ConvDilationFactor1008, Vars.ConvPadding1009, 1, NumDims.relu1001, NumDims.fc_1_W); Vars.fc_1 = dlconv(Vars.relu1001, weights, bias,'Stride', stride,'DilationFactor', dilationFactor,'Padding', padding,'DataFormat', dataFormat);% Conv:(重量、偏见、跨步、dilationFactor、填充,dataFormat, NumDims.fc_2] = prepareConvArgs(Vars.fc_2_W, Vars.fc_2_B, Vars.ConvStride1010, Vars.ConvDilationFactor1011, Vars.ConvPadding1012, 1, NumDims.fc_1, NumDims.fc_2_W); Vars.fc_2 = dlconv(Vars.fc_1, weights, bias,'Stride', stride,'DilationFactor', dilationFactor,'Padding', padding,'DataFormat', dataFormat);% Conv:(重量、偏见、跨步、dilationFactor、填充,dataFormat, NumDims.fc_3] = prepareConvArgs(Vars.fc_3_W, Vars.fc_3_B, Vars.ConvStride1013, Vars.ConvDilationFactor1014, Vars.ConvPadding1015, 1, NumDims.fc_2, NumDims.fc_3_W); Vars.fc_3 = dlconv(Vars.fc_2, weights, bias,'Stride', stride,'DilationFactor', dilationFactor,'Padding', padding,'DataFormat', dataFormat);
You can remove the parameters of the fully connected layerfc_2
to reduce computational complexity. Check the output dimensions of the previous layer and the input dimensions of the subsequent layer before removing a middle layer fromparams
. In this case, the output size of the previous layerfc_1
is 20, and the input size of the subsequent layerfc_3
is also 20.
Remove the parameters of layerfc_2
by usingremoveParameter
.
params = removeParameter(params,'fc_2_B'); params = removeParameter(params,'fc_2_W'); params = removeParameter(params,'ConvStride1010'); params = removeParameter(params,'ConvDilationFactor1011'); params = removeParameter(params,'ConvPadding1012');
Display the updated learnable and nonlearnable parameters.
params.Learnables
ans =struct with fields:imageinput_Mean: [1×1 dlarray] conv_W: [5×5×1×20 dlarray] conv_B: [20×1 dlarray] batchnorm_scale: [20×1 dlarray] batchnorm_B: [20×1 dlarray] fc_1_W: [24×24×20×20 dlarray] fc_1_B: [20×1 dlarray] fc_3_W: [1×1×20×10 dlarray] fc_3_B: [10×1 dlarray]
params.Nonlearnables
ans =struct with fields:ConvStride1004: [2×1 dlarray] ConvDilationFactor1005: [2×1 dlarray] ConvPadding1006: [4×1 dlarray] ConvStride1007: [2×1 dlarray] ConvDilationFactor1008: [2×1 dlarray] ConvPadding1009: [4×1 dlarray] ConvStride1013: [2×1 dlarray] ConvDilationFactor1014: [2×1 dlarray] ConvPadding1015: [4×1 dlarray]
Modify the architecture of the model function to reflect the changes inparams
so you can use the network for prediction with the new parameters or retrain the network. Open the model functionsimplenetFcn
. Then, remove the fully connected layerfc_2
, and change the input data of the convolution operationdlconv
for layerfc_3
toVars.fc_1
.
opensimplenetFcn
The code below shows layersfc_1
andfc_3
.
% Conv:(重量、偏见、跨步、dilationFactor、填充,dataFormat, NumDims.fc_1] = prepareConvArgs(Vars.fc_1_W, Vars.fc_1_B, Vars.ConvStride1007, Vars.ConvDilationFactor1008, Vars.ConvPadding1009, 1, NumDims.relu1001, NumDims.fc_1_W); Vars.fc_1 = dlconv(Vars.relu1001, weights, bias,'Stride', stride,'DilationFactor', dilationFactor,'Padding', padding,'DataFormat', dataFormat);% Conv:(重量、偏见、跨步、dilationFactor、填充,dataFormat, NumDims.fc_3] = prepareConvArgs(Vars.fc_3_W, Vars.fc_3_B, Vars.ConvStride1013, Vars.ConvDilationFactor1014, Vars.ConvPadding1015, 1, NumDims.fc_2, NumDims.fc_3_W); Vars.fc_3 = dlconv(Vars.fc_1, weights, bias,'Stride', stride,'DilationFactor', dilationFactor,'Padding', padding,'DataFormat', dataFormat);
Input Arguments
params
—Network parameters
ONNXParameters
object
Network parameters, specified as anONNXParameters
object.params
contains the network parameters of the imported ONNX™ model.
name
—Name of parameter
character vector|string scalar
Name of the parameter, specified as a character vector or string scalar.
Example:'conv2_W'
Example:'conv2_Padding'
Output Arguments
params
— Network parameters
ONNXParameters
object
Network parameters, returned as anONNXParameters
object.params
contains the network parameters updated byremoveParameter
.
Version History
See Also
Apri esempio
如果dispone di una versione modificata di questo esempio. Desideri aprire questo esempio con le tue modifiche?
Comando MATLAB
Hai fatto clic su un collegamento che corrisponde a questo comando MATLAB:
Esegui il comando inserendolo nella finestra di comando MATLAB. I browser web non supportano i comandi MATLAB.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select:.
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina(Español)
- Canada(English)
- United States(English)
Europe
- Belgium(English)
- Denmark(English)
- Deutschland(Deutsch)
- España(Español)
- Finland(English)
- France(Français)
- Ireland(English)
- Italia(Italiano)
- Luxembourg(English)
- Netherlands(English)
- Norway(English)
- Österreich(Deutsch)
- Portugal(English)
- Sweden(English)
- Switzerland
- United Kingdom(English)