main content

analyze deep learning network architecture -凯发k8网页登录

analyze deep learning network architecture

description

use analyzenetwork to visualize and understand the architecture of a network, check that you have defined the architecture correctly, and detect problems before training. problems that analyzenetwork detects include missing or unconnected layers, incorrectly sized layer inputs, an incorrect number of layer inputs, and invalid graph structures.

tip

to interactively visualize, analyze, and train a network, use deepnetworkdesigner(net). for more information, see deep network designer.

example

analyzenetwork(layers) analyzes the network layers given by the layer array or layer graph layers and also detects errors and issues for trainnetwork workflows. the function displays an interactive visualization of the network architecture and provides detailed information about the network layers. the layer information includes the layer type, the size and format of the layer activations, and the size and number of learnable and state parameters.

each activation dimension has one of the following labels: s (spatial), c (channel), b (batch), t (time or sequence), or u (unspecified).

example

analyzenetwork(layers,targetusage=target) analyzes the network layers given by the layer array or layer graph layers for the specified target workflow. use this syntax when analyzing the layers for dlnetwork workflows.

analyzenetwork(layers,x1,...,xn,targetusage="dlnetwork") analyzes the layer array or layer graph layers using the example networks inputs x1,...,xn. the software propagates the example inputs through the network to determine the size and format of layer activations, the size and number of learnable and state parameters, and the total number of learnables. use this syntax to analyze a network that has one or more inputs that are not connected to an input layer.

example

analyzenetwork(net) analyzes the seriesnetwork, dagnetwork, or dlnetwork object net.

example

analyzenetwork(net,x1,...,xn) analyzes the dlnetwork object net using example networks inputs x1,...,xn. the software propagates the example inputs through the network to determine the size and format of layer activations, the size and number of learnable and state parameters, and the total number of learnables. use this syntax to analyze an uninitialized dlnetwork that has one or more inputs that are not connected to an input layer.

examples

load a pretrained googlenet convolutional neural network.

net = googlenet;

analyze the network. analyzenetwork displays an interactive plot of the network architecture and a table containing information about the network layers.

analyzenetwork(net)

investigate the network architecture using the plot to the left. select a layer in the plot. the selected layer is highlighted in the plot and in the layer table.

in the table, view layer information such as layer properties, layer type, and sizes of the layer activations and learnable parameters. the activations of a layer are the outputs of that layer. each activation dimension has one of the following labels:

  • s — spatial

  • c — channel

  • b — batch observations

  • t — time or sequence

  • u — unspecified

view the dimension labels to understand how data propagates through the network and how the layers modify the size and layout of activations.

select a deeper layer in the network. notice that activations in deeper layers are smaller in the spatial dimensions (the first two dimensions) and larger in the channel dimension (the last dimension). using this structure enables convolutional neural networks to gradually increase the number of extracted image features while decreasing the spatial resolution.

the deep learning network analyzer shows the total number of learnable parameters in the network, to one decimal place. to see the exact number of learnable parameters, pause on total learnables. to show the number of learnable parameters in each layer, click the arrow in the top-right corner of the layer table and select number of learnables. to sort the layer table by column value, hover the mouse over the column heading and click the arrow that appears. for example, you can determine which layer contains the most parameters by sorting the layers by the number of learnable parameters.

create a simple convolutional network with some skip connections. create a layer array containing the main branch of the network.

layers = [
    imageinputlayer([32 32 3])
    convolution2dlayer(5,16,padding="same")
    relulayer(name="relu_1")
    convolution2dlayer(3,16,padding="same",stride=2)
    relulayer
    additionlayer(2,name="add_1")
    convolution2dlayer(3,16,padding="same",stride=2)
    relulayer
    additionlayer(3,name="add_2")
    fullyconnectedlayer(10)
    softmaxlayer
    classificationlayer];
lgraph = layergraph(layers);

convert the layer array to a layer graph and add the skip connections. one of the skip connections contains a single 1-by-1 convolutional layer conv_skip.

layer = convolution2dlayer(1,16,stride=2,name="conv_skip");
lgraph = addlayers(lgraph,layer);
lgraph = connectlayers(lgraph,"relu_1","add_1/in2");
lgraph = connectlayers(lgraph,"add_1","add_2/in2");

analyze the network architecture using the analyzenetwork function. the function finds issues with three layers in the network.

analyzenetwork(lgraph)

investigate and fix the errors in the network. in this example, the following issues cause the errors:

  • the layer conv_skip is not connected to the rest of the network. it should be a part of the shortcut connection between the add_1 and add_2 layers. to fix this error, connect add_1 to conv_skip and conv_skip to add_2.

  • the add_2 layer is specified to have three inputs, but the layer only has two inputs. to fix the error, specify the number of inputs as 2.

  • all the inputs to an addition layer must have the same size, but the add_1 layer has two inputs with different sizes. because the conv_2 layer has a stride value of 2, this layer downsamples the activations by a factor of two in the first two dimensions (the spatial dimensions). to resize the input from the relu2 layer so that it has the same size as the input from relu_1, remove the downsampling by removing the stride value of the conv_2 layer.

apply these modifications to the layer graph construction from the beginning of this example and create a new layer graph.

layers = [
    imageinputlayer([32 32 3])
    convolution2dlayer(5,16,padding="same")
    relulayer(name="relu_1")
    convolution2dlayer(3,16,padding="same")
    relulayer
    additionlayer(2,name="add_1")
    convolution2dlayer(3,16,padding="same",stride=2)
    relulayer
    additionlayer(2,name="add_2")
    fullyconnectedlayer(10)
    softmaxlayer
    classificationlayer];
lgraph = layergraph(layers);
layer = convolution2dlayer(1,16,stride=2,name="conv_skip");
lgraph = addlayers(lgraph,layer);
lgraph = connectlayers(lgraph,"relu_1","add_1/in2");
lgraph = connectlayers(lgraph,"add_1","conv_skip");
lgraph = connectlayers(lgraph,"conv_skip","add_2/in2");

analyze the new architecture. the new network does not contain any errors and is ready to be trained.

analyzenetwork(lgraph)

create a layer array for a custom training loop. for custom training loop workflows, the network must not have an output layer.

layers = [
    imageinputlayer([28 28 1],normalization="none")
    convolution2dlayer(5,20,padding="same")
    batchnormalizationlayer
    relulayer
    convolution2dlayer(3,20,padding="same")
    batchnormalizationlayer
    relulayer
    convolution2dlayer(3,20,padding="same")
    batchnormalizationlayer
    relulayer
    fullyconnectedlayer(10)
    softmaxlayer];

analyze the layer array using the analyzenetwork function and set the targetusage option to "dlnetwork".

analyzenetwork(layers,targetusage="dlnetwork")

here, the function does not report any issues with the layer array.

to analyze a network that has an input that is not connected to an input layer, you can provide example network inputs to the analyzenetwork function. you can provide example inputs when you analyze dlnetwork objects, or when you analyze layer arrays or layergraph objects for custom training workflows by setting the targetusage option to "dlnetwork".

define the network architecture. construct a network with two branches. the network takes two inputs, with one input per branch. connect the branches using an addition layer.

numfilters = 24;
inputsize = [64 64 3];
layersbranch1 = [
    convolution2dlayer(3,6*numfilters,padding="same",stride=2)
    groupnormalizationlayer("all-channels")
    relulayer
    convolution2dlayer(3,numfilters,padding="same")
    groupnormalizationlayer("channel-wise")
    additionlayer(2,name="add")
    relulayer
    fullyconnectedlayer(10)
    softmaxlayer];
layersbranch2 = [
    convolution2dlayer(1,numfilters,name="conv_branch")
    groupnormalizationlayer("all-channels",name="gn_branch")];
lgraph = layergraph(layersbranch1);
lgraph = addlayers(lgraph,layersbranch2);
lgraph = connectlayers(lgraph,"gn_branch","add/in2");

create the dlnetwork. because this network contains an unconnected input, create an uninitialized dlnetwork object by setting the initialize option to false.

net = dlnetwork(lgraph,initialize=false);

view the network input names.

net.inputnames
ans = 1×2 cell
    {'conv_1'}    {'conv_branch'}

create example network inputs of the same size and format as typical inputs for this network using random dlarray objects. for both inputs, use a batch size of 32. use an input of size 64-by-64 with three channels for the input to the layer "input". use an input of size 64-by-64 with 18 channels for the input to the layer "conv_branch".

x1 = dlarray(rand([inputsize 32]),"sscb");
x2 = dlarray(rand([32 32 18 32]),"sscb");

analyze the network. provide the example inputs in the same order as the inputnames property of the dlnetwork. you must provide an example input for all network inputs, including inputs that are connected to an input layer.

analyzenetwork(net,x1,x2)

input arguments

network layers, specified as a array or a object.

for a list of built-in layers, see .

deep learning network, specified as a , , or object.

target workflow, specified as one of the following:

  • "trainnetwork" — analyze layer graph for usage with the trainnetwork function. for example, the function checks that the layer graph has an output layer and no disconnected layer outputs.

  • "dlnetwork" — analyze layer graph for usage with dlnetwork objects. for example, the function checks that the layer graph does not have any output layers.

example network inputs, specified as formatted dlarray objects. the software propagates the example inputs through the network to determine the size and format of layer activations, the size and number of learnable and state parameters, and the total number of learnables.

use example inputs when you want to analyze a network that does not have any input layers or that has inputs that are not connected to an input layer.

the order in which you must specify the example inputs depends on the type of network you are analyzing:

  • layer array — provide example inputs in the same order that the layers that require inputs appear in the layer array.

  • layergraph — provide example inputs in the same order as the layers that require inputs appear in the layers property of the layergraph.

  • dlnetwork — provide example inputs in the same order as the inputs are listed in the inputnames property of the dlnetwork.

if a layer has multiple unconnected inputs, then example inputs for that layer must be specified separately in the same order as they appear in the layer’s inputnames property.

you must specify one example input for each input to the network, even if that input is connected to an input layer.

version history

introduced in r2018a

网站地图