Main Content

setNetwork

Assign dlnetwork object as the state or output function of a neural state-space model

Since R2024b

    Description

    You can use dlnetwork objects to approximate the state or non-trivial part of the output function of a neural state-space model. A dlnetwork object supports more network structures when compared to the multi-layer perceptron network created using createMLPNetwork. Use setNetwork to map the input layers of the network to the corresponding state, input, and time of the neural state-space model.

    As an alternative to setNetwork, you can directly assign a dlnetwork object to the StateNetwork and OutputNetwork properties of an idNeuralStateSpace object. To do so, the dlnetwork object must have a configuration as specified in dlnet. In this case, the software maps the input layers of the dlnetwork object to the corresponding state, input, and time of the idNeuralStateSpace object by comparing the layer sizes with the number of states and inputs. If the layer sizes are not distinguishable, that is, if the number of states and inputs are equal, then the software maps the layers in the order of state, input, and time.

    For more information on neural state-space models and their structure, see What are Neural State-Space Models?.

    nss = setNetwork(nss,type,dlnet) approximates either the state or (non-trivial part of) the output function of the neural state-space object nss, depending on type, by assigning the custom dlnetwork object dlnet to the idNeuralStateSpace object nss. It maps the input layers of dlnet to the corresponding state, input, and time of the neural state-space model by comparing the size of the layers with the number of states and inputs.

    For example, to assign the network dlnet to the state function, use

    nss = setNetwork(nss,"state",dlnet)
    This command is equivalent to nss.StateNetwork = dlnet.

    To assign the network to the non-trivial part of the output function, use

    nss = setNetwork(nss,"output",dlnet)
    This command is equivalent to nss.OutputNetwork = dlnet.

    example

    nss = setNetwork(nss,type,dlnet,Name=Value) specifies name-value arguments after the input arguments in the previous syntax.

    When the input layer sizes are not distinguishable, use the name-value arguments to manually assign the input layers of the network to the corresponding state, input, and time of the neural state-space model.

    For example, to assign the network dlnet to the state function and map input layers with names "State" and "Input" to the state and input of the neural state-space model, respectively, use

    nss = setNetwork(nss,"state",dlnet,xName="State",uName="Input")

    example

    Examples

    collapse all

    Use idNeuralStateSpace to create a continuous-time neural state-space object with two states and one input. By default, the state function network has two hidden layers each with 64 neurons and a hyperbolic tangent activation function.

    nss = idNeuralStateSpace(2,NumInputs=1)
    nss =
    
    Continuous-time Neural ODE in 2 variables
         dx/dt = f(x(t),u(t))
          y(t) = x(t) + e(t)
     
    f(.) network:
      Deep network with 2 fully connected, hidden layers
      Activation function: tanh
     
    Variables: x1, x2
     
    Status:                                                         
    Created by direct construction or transformation. Not estimated.
    

    Create a custom dlnetwork object with two featureInputLayers of sizes equal to the number of states and inputs. Specify two hidden layers of 4 and 8 neurons, respectively, and use relu as the activation function. Specify one output layer of size equal to the number of states.

    layer1 = featureInputLayer(2,Name="state");
    layer2 = featureInputLayer(1,Name="input");
    layer3 = [ ...
       concatenationLayer(1,2,Name="concat")
       fullyConnectedLayer(4,Name="fc1")
       reluLayer(Name="act1")
       fullyConnectedLayer(8,Name="fc2")
       reluLayer(Name="act2")
       fullyConnectedLayer(2,Name="dx")];
    dlnet = dlnetwork(layer3,Initialize=false);
    dlnet = addLayers(dlnet,layer1);
    dlnet = addLayers(dlnet,layer2);
    dlnet = connectLayers(dlnet, "state", "concat/in1");
    dlnet = connectLayers(dlnet, "input", "concat/in2");
    plot(dlnet)

    Figure contains an axes object. The axes object contains an object of type graphplot.

    Initialize the network.

    dlnet = initialize(dlnet);

    To re-configure the state function network of nss, directly assign the custom network dlnet to the StateNetwork property of nss. As the input layer sizes of the network are distinguishable, the software automatically maps the input layers to the corresponding state and input of nss.

    nss.StateNetwork = dlnet;

    You can also use setNetwork to assign the custom network.

    nss = setNetwork(nss,"state",dlnet);
    

    You can now use time-domain data to perform estimation and validation.

    Use idNeuralStateSpace to create a continuous-time neural state-space object with one state and one input. By default, the state function network has two hidden layers each with 64 neurons and a hyperbolic tangent activation function.

    nss = idNeuralStateSpace(1,NumInputs=1)
    nss =
    
    Continuous-time Neural ODE in 1 variables
         dx/dt = f(x(t),u(t))
          y(t) = x(t) + e(t)
     
    f(.) network:
      Deep network with 2 fully connected, hidden layers
      Activation function: tanh
     
    Variables: x1
     
    Status:                                                         
    Created by direct construction or transformation. Not estimated.
    

    Create a custom dlnetwork object with two featureInputLayers of sizes equal to the number of states and inputs. Specify two hidden layers of 4 and 8 neurons, respectively, and use relu as the activation function. Specify one output layer of size equal to the number of states.

    layer1 = featureInputLayer(1,Name="state");
    layer2 = featureInputLayer(1,Name="input");
    layer3 = [ ...
       concatenationLayer(1,2,Name="concat")
       fullyConnectedLayer(4,Name="fc1")
       reluLayer(Name="act1")
       fullyConnectedLayer(8,Name="fc2")
       reluLayer(Name="act2")
       fullyConnectedLayer(1,Name="dx")];
    dlnet = dlnetwork(layer3,Initialize=false);
    dlnet = addLayers(dlnet,layer1);
    dlnet = addLayers(dlnet,layer2);
    dlnet = connectLayers(dlnet, "state", "concat/in1");
    dlnet = connectLayers(dlnet, "input", "concat/in2");
    plot(dlnet)

    Figure contains an axes object. The axes object contains an object of type graphplot.

    Initialize the network.

    dlnet = initialize(dlnet);

    To re-configure the state function network of nss, assign the custom network dlnet to nss. Because both the state and input layers have the same size, use setNetwork with name-value pair arguments to assign the input layers of the custom network to the corresponding state and input of nss.

    nss = setNetwork(nss,"state",dlnet,xName="state",uName="input");

    You can now use time-domain data to perform estimation and validation.

    Input Arguments

    collapse all

    Neural state-space system, specified as an idNeuralStateSpace object.

    Example: idNeuralStateSpace(2,NumInputs=1)

    Network type, specified as one of the following:

    • "state" — assigns the network to nss to approximate the state function of the model. For continuous state-space systems the state function returns the system state derivative with respect to time, while for discrete-time state-space systems it returns the next state. The inputs of the state function are time (if IsTimeInvariant is false), the current state, and the current input (if NumInputs is positive).

    • "output" — assigns the network to nss to approximate the non-trivial part of the output function of the model. This network returns the non-trivial system output, y2(t) = H(t,x,u), as a function of time (if IsTimeInvariant is false), the current state, and the current input (if NumInputs is positive and HasFeedthrough is true). For more information, see idNeuralStateSpace.

    Custom network to approximate the state or output function of nss, specified as a dlnetwork (Deep Learning Toolbox) object with the following configuration:

    • The input layers of the custom network must all be of type featureInputLayer (Deep Learning Toolbox). The network must have one featureInputLayer of size equal to the number of states (nx). For time-varying models, the network must have one featureInputLayer of size 1. In addition, if the model uses input signals (that is, if nu > 0):

      • To approximate the state function, the network must have one featureInputLayer of size equal to the number of inputs (nu)

      • To approximate the output function, the network must have one featureInputLayer of size equal to the number of inputs (nu) only if the HasFeedthrough argument of the idNeuralStateSpace object is set to true.

    • The network must contain exactly one output layer. To approximate the state function, the output layer size must be equal to the number of model states. To approximate the output function, the size must be equal to the number of additional outputs (that is, ny - nx, where ny is the total number of model outputs).

    You can use the Deep Network Designer (Deep Learning Toolbox) app to design the network or use the command dlnetwork (Deep Learning Toolbox).

    Name-Value Arguments

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Example: xName="st1"

    Use name-value arguments to manually assign the input layers of the custom network to the state, input, and time of the neural state-space model when the layer sizes are not distinguishable.

    Network input layer name corresponding to state or output (x) of nss, specified as a string or character array.

    If the type is "state", xName is the name of the input layer corresponding to the state network of nss. If the type is "output", xName is the name of the input layer corresponding to the output network of nss.

    Example: "State"

    Network input layer name corresponding to input (u) of nss, specified as a string or character array.

    Example: "Input"

    Network input layer name corresponding to time (t) of nss, specified as a string or character array.

    Example: "Time"

    Version History

    Introduced in R2024b