Main Content

functionToLayerGraph

Convert deep learning model function to a layer graph

Description

example

lgraph= functionToLayerGraph(fun,x)returns a layer graph based on the deep learning array functionfun.functionToLayerGraphconverts only those operations infunthat operate ondlarrayobjects among the inputs inx. To include extra parameters or data infun, see the topicParameterizing Functionsor the exampleCreate Layer Graph from Function.

functionToLayerGraphevaluatesfun(x) and traces the execution to derive an equivalent layer graph, to the extent possible. The steps infun(x) thatfunctionToLayerGraphcan trace are both based ondlarrayarguments and are supported calls fordlarray. SeeList of Functions with dlarray Support. For unsupported functions,functionToLayerGraphcreates aPlaceholderLayer.

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

Thesimplemodelfunction at the end of this example creates fully connected outputs followed by asoftmaxoperation. To create a layer graph from this function based ondlarraydata, create input arrays asdlarrayobjects, and create a function handle to thesimplemodelfunction including the data.

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

CallfunctionToLayerGraphusing adlarrayfor the input datadlX.

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 inlgraph.

disp(lgraph.Layers)
2x1 Layer array with layers: 1 'fc_1' Fully Connected 10 fully connected layer 2 'sm_1' Softmax softmax
functiony = 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. Onlydlarraydata 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 asName1=Value1,...,NameN=ValueN, whereNameis the argument name andValueis 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 encloseNamein quotes.

Example:'GenerateLayer','placeholder-layer'

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

When an operation infundoes 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 aLayerGraphobject.

Version History

Introduced in R2019b