Main Content


Convert deep learning model function to a layer graph



lgraph = functionToLayerGraph(fun,x) returns a layer graph based on the deep learning array function fun. functionToLayerGraph converts only those operations in fun that operate on dlarray objects among the inputs in x. To include extra parameters or data in fun, see the topic Parameterizing Functions or the example Create Layer Graph from Function.

functionToLayerGraph evaluates fun(x) and traces the execution to derive an equivalent layer graph, to the extent possible. The steps in fun(x) that functionToLayerGraph can trace are both based on dlarray arguments and are supported calls for dlarray. See List of Functions with dlarray Support. For unsupported functions, functionToLayerGraph creates a PlaceholderLayer.

lgraph = functionToLayerGraph(fun,x,Name,Value) specifies options using one or more name-value pair arguments in addition to the input arguments in the previous syntax.


collapse all

The simplemodel function at the end of this example creates fully connected outputs followed by a softmax operation. To create a layer graph from this function based on dlarray data, create input arrays as dlarray objects, and create a function handle to the simplemodel function including the data.

rng default % For reproducibility
dlX1 = dlarray(rand(10),'CB');
dlX2 = dlarray(zeros(10,1),'CB');
fun = @(x)simplemodel(x,dlX1,dlX2);

Call functionToLayerGraph using a dlarray for the input data dlX.

dlX = dlarray(ones(10,1),'CB');
lgraph = functionToLayerGraph(fun,dlX)
lgraph = 
  LayerGraph with properties:

         Layers: [2x1 nnet.cnn.layer.Layer]
    Connections: [1x2 table]
     InputNames: {1x0 cell}
    OutputNames: {1x0 cell}

Examine the resulting layers in lgraph.

  2x1 Layer array with layers:

     1   'fc_1'   Fully Connected   10 fully connected layer
     2   'sm_1'   Softmax           softmax
function y = simplemodel(x,w,b)
y = fullyconnect(x,w,b);
y = softmax(y);

Input Arguments

collapse all

Function to convert, specified as a function handle.

Example: @relu

Data Types: function_handle

Data for the function, specified as any data type. Only dlarray data is traced and converted to a layer graph.

Example: dlarray(zeros(12*50,23))

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | string | struct | table | cell | function_handle | categorical | datetime | duration | calendarDuration | fi

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.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: 'GenerateLayer','placeholder-layer'

Type of layer to generate for unsupported operations in fun, specified as 'custom-layer' or 'placeholder-layer'.

When an operation in fun does not correspond to a layer in Deep Learning Toolbox™, the software generates a layer to represent that functionality. The 'GenerateLayer' option specifies the type of layer as follows.

Example: 'GenerateLayer','placeholder-layer'

Prefix for generate custom layers, specified as a char vector.

This option applies only when the 'GenerateLayer' option is 'custom-layer'. The name of each generated custom layer starts with the specified prefix.

Example: 'CustomLayerPrefix','myGeneratedLayer'

Output Arguments

collapse all

Layer graph, returned as a LayerGraph object.

Version History

Introduced in R2019b