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 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 (MATLAB) 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.

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

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

Output Arguments

collapse all

Layer graph, returned as a LayerGraph object.

When a portion of fun is unsupported, lgraph contains PlaceholderLayer layers representing the unsupported functionality. To create a working network in this case, see Define Custom Deep Learning Layers or Define Custom Networks.

Introduced in R2019b