Main Content

removeParameter

Remove parameter from ONNXParameters object

Since R2020b

    Description

    params = removeParameter(params,name) removes the parameter specified by name from the ONNXParameters object params.

    example

    Examples

    collapse all

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

    Import the pretrained simplenet3fc.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 to simplenet3fc, see Create Simple Image Classification Network.

    Import simplenet3fc.onnx using importONNXFunction, which returns an ONNXParameters 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 as simplenetFcn.

    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 layers fc_1, fc_2, and fc_3, open the model function simplenetFcn.

    open simplenetFcn

    Scroll down to the layer definitions in the function simplenetFcn. The code below shows the definitions for layers fc_1, fc_2, and fc_3.

    % Conv:
    [weights, bias, stride, dilationFactor, padding, 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:
    [weights, bias, stride, dilationFactor, padding, 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:
    [weights, bias, stride, dilationFactor, padding, 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 layer fc_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 from params. In this case, the output size of the previous layer fc_1 is 20, and the input size of the subsequent layer fc_3 is also 20.

    Remove the parameters of layer fc_2 by using removeParameter.

    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 in params so you can use the network for prediction with the new parameters or retrain the network. Open the model function simplenetFcn. Then, remove the fully connected layer fc_2, and change the input data of the convolution operation dlconv for layer fc_3 to Vars.fc_1.

    open simplenetFcn

    The code below shows layers fc_1 and fc_3.

    % Conv:
    [weights, bias, stride, dilationFactor, padding, 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:
    [weights, bias, stride, dilationFactor, padding, 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 an ONNXParameters object. params contains 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 an ONNXParameters object. params contains the network parameters updated by removeParameter.

    Version History

    Introduced in R2020b