Main Content

functionToLayerGraph

(To be removed) Convert deep learning model function to a layer graph

Since R2019b

functionToLayerGraph will be removed in a future release. Construct layer graphs manually instead. (since R2023a) For a list of layers, see List of Deep Learning Layers. For information about developing custom layers, see Define Custom Deep Learning Layers.

Description

example

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.

Examples

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
X1 = dlarray(rand(10),'CB');
X2 = dlarray(zeros(10,1),'CB');
fun = @(x)simplemodel(x,X1,X2);

Call functionToLayerGraph using a dlarray for the input data X.

X = dlarray(ones(10,1),'CB');
lgraph = functionToLayerGraph(fun,X)
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.

disp(lgraph.Layers)
  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);
end

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.

Limitations

functionToLayerGraph does not support these deep learning operations:

If you use functionToLayerGraph with a function that contains these operations, then the resulting layer graph contains placeholder layers.

Version History

Introduced in R2019b

collapse all

R2023a: To be removed

functionToLayerGraph will be removed in a future release. Construct layer graphs manually instead. For a list of layers, see List of Deep Learning Layers. For information about developing custom layers, see Define Custom Deep Learning Layers.