Main Content

removeParameter

Remove parameter fromONNXParametersobject

    Description

    example

    params = removeParameter(params,name)removes the parameter specified bynamefrom theONNXParametersobjectparams.

    Examples

    collapse all

    Import a network saved in the ONNX format as a function and modify the network parameters.

    Import the pretrainedsimplenet3fc.onnxnetwork as a function.simplenet3fcis 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.onnxusingimportONNXFunction, which returns anONNXParametersobject 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_2to 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_1is 20, and the input size of the subsequent layerfc_3is also 20.

    Remove the parameters of layerfc_2by 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 inparamsso 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 operationdlconvfor layerfc_3toVars.fc_1.

    opensimplenetFcn

    The code below shows layersfc_1andfc_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

    collapse all

    Network parameters, specified as anONNXParametersobject.paramscontains the network parameters of the imported ONNX™ model.

    Name of the parameter, specified as a character vector or string scalar.

    Example:'conv2_W'

    Example:'conv2_Padding'

    Output Arguments

    collapse all

    Network parameters, returned as anONNXParametersobject.paramscontains the network parameters updated byremoveParameter.

    Version History

    Introduced in R2020b