Main Content

initialize

Initialize learnable and state parameters of a dlnetwork

    Description

    example

    Tip

    Most dlnetwork objects are initialized by default. You only need to manually initialize a dlnetwork if it is uninitialized. You can check if a network is initialized using the Initialized property of the dlnetwork object.

    dlnet = initialize(dlnet) initializes any unset learnable parameters and state values of dlnet based on the input sizes defined by the network input layers. Any learnable or state parameters that already contain values remain unchanged.

    A network with unset, empty values for learnable and state parameters is uninitialized. You must initialize an uninitialized dlnetwork before you can use it. By default, dlnetwork objects are constructed with initial parameters and do not need initializing.

    example

    dlnet = initialize(dlnet,dlX1,...,dlXn) initializes any unset learnable parameters and state values of dlnet based on the example network inputs dlX1,...,dlXn. Use this syntax when the network has inputs that are not connected to an input layer.

    Examples

    collapse all

    Use the initialize function to initialize a dlnetwork object that contains an input layer.

    Define the network layers.

    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')];

    Create an uninitialized dlnetwork. Set the Initialize name-value option to false.

    dlnet = dlnetwork(layers,'Initialize',false);

    Examine the learnable parameters of the convolution layer.

    dlnet.Learnables.Value(dlnet.Learnables.Layer=='conv')
    ans = 
        {0×0 double}
        {0×0 double}
    

    Because the network is not initialized, the learnable parameters of the convolution layer are empty.

    Initialize the learnable parameters of the network with initial values.

    dlnet = initialize(dlnet);

    Check the learnable parameters of the convolution layer after initialization.

    dlnet.Learnables.Value(dlnet.Learnables.Layer=='conv')
    ans = 
        {5×5×1×20 dlarray}
        {1×1×20   dlarray}
    

    The learnable parameters of the convolution layer are now initialized with initial values of appropriate size based on the size of the input data.

    Check that the network is initialized and ready for training.

    dlnet.Initialized
    ans = 
       1
    

    Use the initialize function to initialize a multi-input dlnetwork object that contains one input layer and one unconnected input.

    Define the network architecture. Construct a network with two branches. The network takes two inputs, with one input per branch. The first branch contains an input layer, while the second branch does not. Connect the branches using an addition layer.

    numFilters = 24;
    inputSize = [64 64 3];
    
    layersBranch1 = [
        imageInputLayer(inputSize,'Normalization','None','Name','in')
        convolution2dLayer(3,6*numFilters,'Padding','same','Stride',2,'Name','conv1Branch1')
        groupNormalizationLayer('all-channels','Name','gn1Branch1')
        reluLayer('Name','relu1Branch1')
        convolution2dLayer(3,numFilters,'Padding','same','Name','conv2Branch1')
        groupNormalizationLayer('channel-wise','Name','gn2Branch1')
        additionLayer(2,'Name','add')
        reluLayer('Name','reluCombined')
        fullyConnectedLayer(10,'Name','fc')
        softmaxLayer('Name','sm')];
    
    layersBranch2 = [
        convolution2dLayer(1,numFilters,'Name','convBranch2')
        groupNormalizationLayer('all-channels','Name','gnBranch2')];
    
    lgraph = layerGraph(layersBranch1);
    lgraph = addLayers(lgraph,layersBranch2);
    lgraph = connectLayers(lgraph,'gnBranch2','add/in2');  

    Convert the network to a dlnetwork. To construct the dlnetwork object without initial values of learnable and state parameters, set the Initialize name-value option to false..

    dlnet = dlnetwork(lgraph,'Initialize',false);

    Examine the learnable parameters of the second group normalization layer in the first branch of the network.

    dlnet.Learnables.Value(dlnet.Learnables.Layer=='gn2Branch1')
    ans = 
        {0×0 double}
        {0×0 double}
    

    Because the network is not initialized, the learnable parameters of the layer are empty.

    Inspect the order of the network inputs.

    dlnet.InputNames
    ans = 1×2 cell    
    'in'         'convBranch2'       
    

    Create example input data with the same size and format as typical network inputs. Use an example input of size 64-by-64 with 3 channels for the input to the input layer in. Use an input of size 64-by-64 with 18 channels for the unconnected input to the layer convBranch2.

    dlX1 = dlarray(rand(inputSize),"SSCB");
    dlX2 = dlarray(rand([32 32 18]),"SSCB");

    Initialize the learnable parameters of the network using the example inputs.

    dlnet = initialize(dlnet,dlX1,dlX2);

    Check the learnable parameters of the convolution layer after initialization.

    dlnet.Learnables.Value(dlnet.Learnables.Layer=='gn2Branch1')
    ans = 
        {1×1×24 dlarray}
        {1×1×24 dlarray}
    

    The learnable parameters of the convolution layer are now initialized with initial values of appropriate size based on the size of the input data.

    Use the Initialized property of the network to check that the network is initialized and ready for training.

    dlnet.Initialized
    ans = 
       1
    

    Input Arguments

    collapse all

    Uninitialized network, specified as a dlnetwork object.

    Example network inputs, specified as dlarray objects.

    Example inputs must be formatted dlarray objects. Provide example inputs in the same order as the order specified by the InputNames property of the input network.

    Output Arguments

    collapse all

    Initialized network, returned as a dlnetwork object.

    Introduced in R2021a