addParameter

Add parameter to ONNXParameters object

    Description

    example

    params = addParameter(params,name,value,type) adds the network parameter specified by name, value, and type to the ONNXParameters object params. The returned params object contains the model parameters of the input argument params together with the added parameter, stacked sequentially. The added parameter name must be unique, nonempty, and different from the parameter names in params.

    params = addParameter(params,name,value,type,NumDimensions) adds the network parameter specified by name, value, type, and NumDimensions to params.

    Examples

    collapse all

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

    Create an ONNX model from the pretrained alexnet network. Then import alexnet.onnx as a function. Import the pretrained ONNX network using importONNXFunction, which returns an ONNXParamaters 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 alexnetFcn.

    net = alexnet;
    exportONNXNetwork(net,'alexnet.onnx');
    params = importONNXFunction('alexnet.onnx','alexnetFcn');
    A function containing the imported ONNX network has been saved to the file alexnetFcn.m.
    To learn how to use this function, type: help alexnetFcn.
    

    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:
        data_Mean: [227×227×3 dlarray]
          conv1_W: [11×11×3×96 dlarray]
          conv1_B: [96×1 dlarray]
          conv2_W: [5×5×48×256 dlarray]
          conv2_B: [256×1 dlarray]
          conv3_W: [3×3×256×384 dlarray]
          conv3_B: [384×1 dlarray]
          conv4_W: [3×3×192×384 dlarray]
          conv4_B: [384×1 dlarray]
          conv5_W: [3×3×192×256 dlarray]
          conv5_B: [256×1 dlarray]
            fc6_W: [6×6×256×4096 dlarray]
            fc6_B: [4096×1 dlarray]
            fc7_W: [1×1×4096×4096 dlarray]
            fc7_B: [4096×1 dlarray]
            fc8_W: [1×1×4096×1000 dlarray]
            fc8_B: [1000×1 dlarray]
    
    
    params.Nonlearnables
    ans = struct with fields:
                conv1_Stride: [1×2 dlarray]
        conv1_DilationFactor: [1×2 dlarray]
               conv1_Padding: [1×1 dlarray]
              pool1_PoolSize: [1×2 dlarray]
                pool1_Stride: [1×2 dlarray]
               pool1_Padding: [1×1 dlarray]
                conv2_Stride: [1×2 dlarray]
        conv2_DilationFactor: [1×2 dlarray]
               conv2_Padding: [2×2 dlarray]
              pool2_PoolSize: [1×2 dlarray]
                pool2_Stride: [1×2 dlarray]
               pool2_Padding: [1×1 dlarray]
                conv3_Stride: [1×2 dlarray]
        conv3_DilationFactor: [1×2 dlarray]
               conv3_Padding: [2×2 dlarray]
                conv4_Stride: [1×2 dlarray]
        conv4_DilationFactor: [1×2 dlarray]
               conv4_Padding: [2×2 dlarray]
                conv5_Stride: [1×2 dlarray]
        conv5_DilationFactor: [1×2 dlarray]
               conv5_Padding: [2×2 dlarray]
              pool5_PoolSize: [1×2 dlarray]
                pool5_Stride: [1×2 dlarray]
               pool5_Padding: [1×1 dlarray]
                  fc6_Stride: [1×2 dlarray]
          fc6_DilationFactor: [1×2 dlarray]
                 fc6_Padding: [1×1 dlarray]
                  fc7_Stride: [1×2 dlarray]
          fc7_DilationFactor: [1×2 dlarray]
                 fc7_Padding: [1×1 dlarray]
                  fc8_Stride: [1×2 dlarray]
          fc8_DilationFactor: [1×2 dlarray]
                 fc8_Padding: [1×1 dlarray]
    
    

    The network has parameters that represent three fully connected layers. You can add a fully connected layer in the original parameters params between layers fc7 and fc8. The new layer might increase the classification accuracy.

    Name the new layer fc9, because each added parameter name must be unique. The addParameter function always adds a new parameter sequentially to the params.Learnables or params.Nonlearnables structure. The order of the layers in the model function alexnetFcn determines the order in which the network layers are executed. The order or the names of the parameters do not influence the execution order.

    Add a new fully connected layer fc9 with the same parameters as fc7.

    params = addParameter(params,'fc9_W',params.Learnables.fc7_W,'Learnable');
    params = addParameter(params,'fc9_B',params.Learnables.fc7_B,'Learnable');
    params = addParameter(params,'fc9_Stride',params.Nonlearnables.fc7_Stride,'Nonlearnable');
    params = addParameter(params,'fc9_DilationFactor',params.Nonlearnables.fc7_DilationFactor,'Nonlearnable');
    params = addParameter(params,'fc9_Padding',params.Nonlearnables.fc7_Padding,'Nonlearnable');

    Display the updated learnable and nonlearnable parameters.

    params.Learnables
    ans = struct with fields:
        data_Mean: [227×227×3 dlarray]
          conv1_W: [11×11×3×96 dlarray]
          conv1_B: [96×1 dlarray]
          conv2_W: [5×5×48×256 dlarray]
          conv2_B: [256×1 dlarray]
          conv3_W: [3×3×256×384 dlarray]
          conv3_B: [384×1 dlarray]
          conv4_W: [3×3×192×384 dlarray]
          conv4_B: [384×1 dlarray]
          conv5_W: [3×3×192×256 dlarray]
          conv5_B: [256×1 dlarray]
            fc6_W: [6×6×256×4096 dlarray]
            fc6_B: [4096×1 dlarray]
            fc7_W: [1×1×4096×4096 dlarray]
            fc7_B: [4096×1 dlarray]
            fc8_W: [1×1×4096×1000 dlarray]
            fc8_B: [1000×1 dlarray]
            fc9_W: [1×1×4096×4096 dlarray]
            fc9_B: [4096×1 dlarray]
    
    
    params.Nonlearnables
    ans = struct with fields:
                conv1_Stride: [1×2 dlarray]
        conv1_DilationFactor: [1×2 dlarray]
               conv1_Padding: [1×1 dlarray]
              pool1_PoolSize: [1×2 dlarray]
                pool1_Stride: [1×2 dlarray]
               pool1_Padding: [1×1 dlarray]
                conv2_Stride: [1×2 dlarray]
        conv2_DilationFactor: [1×2 dlarray]
               conv2_Padding: [2×2 dlarray]
              pool2_PoolSize: [1×2 dlarray]
                pool2_Stride: [1×2 dlarray]
               pool2_Padding: [1×1 dlarray]
                conv3_Stride: [1×2 dlarray]
        conv3_DilationFactor: [1×2 dlarray]
               conv3_Padding: [2×2 dlarray]
                conv4_Stride: [1×2 dlarray]
        conv4_DilationFactor: [1×2 dlarray]
               conv4_Padding: [2×2 dlarray]
                conv5_Stride: [1×2 dlarray]
        conv5_DilationFactor: [1×2 dlarray]
               conv5_Padding: [2×2 dlarray]
              pool5_PoolSize: [1×2 dlarray]
                pool5_Stride: [1×2 dlarray]
               pool5_Padding: [1×1 dlarray]
                  fc6_Stride: [1×2 dlarray]
          fc6_DilationFactor: [1×2 dlarray]
                 fc6_Padding: [1×1 dlarray]
                  fc7_Stride: [1×2 dlarray]
          fc7_DilationFactor: [1×2 dlarray]
                 fc7_Padding: [1×1 dlarray]
                  fc8_Stride: [1×2 dlarray]
          fc8_DilationFactor: [1×2 dlarray]
                 fc8_Padding: [1×1 dlarray]
                  fc9_Stride: [1×2 dlarray]
          fc9_DilationFactor: [1×2 dlarray]
                 fc9_Padding: [1×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 by using open alexnetFcn and add the fully connected layer fc9 between layers fc7 and fc8.

    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'

    Value of the parameter, specified as a numeric array, character vector, or string scalar. To duplicate an existing network layer (stored in params), copy the parameter values of the network layer.

    Example: params.Learnables.conv1_W

    Example: params.Nonlearnables.conv1_Padding

    Data Types: single | double | char | string

    Type of parameter, specified as 'Learnable', 'Nonlearnable', or 'State'.

    • The value 'Learnable' specifies a parameter that is updated by the network during training (for example, weights and bias of convolution).

    • The value 'Nonlearnable' specifies a parameter that remains unchanged during network training (for example, padding).

    • The value 'State' specifies a parameter that contains information remembered by the network between iterations and updated across multiple training batches.

    Data Types: char | string

    Number of dimensions for every parameter, specified as a structure. NumDimensions includes trailing singleton dimensions.

    Example: params.NumDimensions.conv1_W

    Example: 4

    Output Arguments

    collapse all

    Network parameters, returned as an ONNXParameters object. params contains the network parameters updated by addParameter.

    Introduced in R2020b