主要内容

replaceLayer

Replace layer in layer graph

描述

example

newlgraph=替换者(lgraph,分层,larray)替换图层分层in the layer graphlgraph与层中的图层larray

replaceLayer连接图层larray顺序连接larray进入图层图。

example

newlgraph=替换者(lgraph,分层,larray,“重新连接”,模式)additionally specifies the method of reconnecting layers.

例子

全部收缩

定义简单的网络体系结构并绘制它。

layers = [ImageInputlayer([28 28 1],,'Name','input')卷积2Dlayer(3,16,,'Padding','相同的','Name','conv_1') reluLayer('Name','relu_1')加法器(2,'Name','add')完整连接的layer(10,'Name','FC') softmaxLayer('Name','softmax')分类器('Name','classoutput');lgraph = layergraph(layers);lgraph =连接器(lgraph,'input','add/in2');figure plot(lgraph)

Figure contains an axes object. The axes object contains an object of type graphplot.

用批处理层面层替换网络中的Relu层,然后是泄漏的Relu层。

larray = [batchnormalizationlayer('Name','BN1') leakyReluLayer('Name','leakyRelu_1','规模',0.1)]; lgraph = replaceLayer(lgraph,'relu_1',larray); plot(lgraph)

Figure contains an axes object. The axes object contains an object of type graphplot.

This example shows how to import the layers from a pretrained Keras network, replace the unsupported layers with custom layers, and assemble the layers into a network ready for prediction.

Import Keras Network

Import the layers from a Keras network model. The network in'digitsDAGnetwithnoise.h5'classifies images of digits.

filename ='digitsDAGnetwithnoise.h5';lgraph = importkeraslayers(文件名,'ImportWeights',true);
警告:无法导入某些KERAS层,因为它们不受深度学习工具箱的支持。金宝app它们已被占位层层所取代。要查找这些层,请在返回的对象上调用函数findplaceholderlayers。

KERAS网络包含一些深度学习工具箱不支持的层。金宝app这ImportKeraslayersfunction displays a warning and replaces the unsupported layers with placeholder layers.

Plot the layer graph usingplot

图图(lgraph)标题(“导入网络”)

Figure contains an axes object. The axes object with title Imported Network contains an object of type graphplot.

替换占位符层

To replace the placeholder layers, first identify the names of the layers to replace. Find the placeholder layers usingFindplaceholderlayers

placeholderLayers = findPlaceholderLayers(lgraph)
占位符= 2x1带有层的占位符层阵列:1'gaussian_noise_1'占位符占位座占位符,用于'gaussiannoise'keras'keras'2'gaussian_noise_noise_2'noise_2'占位符占位符,用于'gaussiannoise'keras'keras'keras'

Display the Keras configurations of these layers.

placeholderLayers.KerasConfiguration
ans =带有字段的结构:trainable: 1 name: 'gaussian_noise_1' stddev: 1.5000
ans =带有字段的结构:训练:1个名称:'Gaussian_noise_2'stddev:0.7000

Define a custom Gaussian noise layer. To create this layer, save the filegaussianNoiseLayer.m在当前文件夹中。然后,创建两个高斯噪声层,其配置与导入的keras层相同。

gnLayer1 = gaussianNoiseLayer(1.5,'new_gaussian_noise_1');gnlayer2 = Gaussiannoiselayer(0.7,'new_gaussian_noise_2');

Replace the placeholder layers with the custom layers usingreplaceLayer

lgraph =替代者(lgraph,'gaussian_noise_1',gnLayer1); lgraph = replaceLayer(lgraph,'gaussian_noise_2',gnlayer2);

使用更新的图层图绘制plot

图图(lgraph)标题("Network with Replaced Layers")

Figure contains an axes object. The axes object with title Network with Replaced Layers contains an object of type graphplot.

指定类名

如果导入的分类层不包含这些类,则必须在预测之前指定这些类。如果您不指定类,则该软件会自动将类设置为1,2, ...,N, whereNis the number of classes.

Find the index of the classification layer by viewing the层s图层图的属性。

lgraph.Layers
ans = 15x1层阵列带有图层:1'input_1'图像输入28x28x1图像2'conv2d_1'卷积20 7x7x1卷积,步幅为[1 1]和填充'相同'3'CORV2D_1_RELU'cons2d_1_RELU'RELU RELU 4'CORMU 4'CORV2D_2D_2卷积20 3X3X1 CORDITITY convertution convertions 20 3x3x1汇总stride [1 1] and padding 'same' 5 'conv2d_2_relu' ReLU ReLU 6 'new_gaussian_noise_1' Gaussian Noise Gaussian noise with standard deviation 1.5 7 'new_gaussian_noise_2' Gaussian Noise Gaussian noise with standard deviation 0.7 8 'max_pooling2d_1' Max Pooling 2x2 max pooling withstride [2 2] and padding 'same' 9 'max_pooling2d_2' Max Pooling 2x2 max pooling with stride [2 2] and padding 'same' 10 'flatten_1' Keras Flatten Flatten activations into 1-D assuming C-style (row-major) order 11 'flatten_2' Keras Flatten Flatten activations into 1-D assuming C-style (row-major) order 12 'concatenate_1' Depth concatenation Depth concatenation of 2 inputs 13 'dense_1' Fully Connected 10 fully connected layer 14 'activation_1' SoftmaxSoftMax 15'分类yer_activation_1'分类输出crossentropyex

这classification layer has the name'classificationlayer_activation_1'。查看分类层,检查Classesproperty.

cLayer = lgraph.Layers(end)
clayer =带有属性的classificationutputlayer:name:'ClassificationLayer_activation_1'类:'auto'classWeights:'none''''''''''''outo'auto'hyperParameters lossfunction:'crossentRopyex''

Because theClasses该层的属性是'auto',您必须手动指定课程。将类设置为0,1, ...,9,然后用新的分类层替换导入的分类层。

clayer.classes =字符串(0:9)
cLayer = ClassificationOutputLayer with properties: Name: 'ClassificationLayer_activation_1' Classes: [0 1 2 3 4 5 6 7 8 9] ClassWeights: 'none' OutputSize: 10 Hyperparameters LossFunction: 'crossentropyex'
lgraph =替代者(lgraph,'classificationlayer_activation_1',cLayer);

Assemble Network

Assemble the layer graph using汇编工作。该功能返回DAGNetwork准备用于预测的对象。

net = assembleNetwork(lgraph)
net = DAGNetwork with properties: Layers: [15x1 nnet.cnn.layer.Layer] Connections: [15x2 table] InputNames: {'input_1'} OutputNames: {'ClassificationLayer_activation_1'}

Input Arguments

全部收缩

图层图,指定为层Graph目的。To create a layer graph, useLayerGraph

Name of the layer to replace, specified as a string scalar or a character vector.

网络层,指定为大批。

有关内置层的列表,请参见List of Deep Learning Layers

重新连接层的方法specified as one of the following:

  • '姓名'- 重新连接larrayusing the input and output names of the replaced layer. For each layer connected to an input of the replaced layer, reconnect the layer to the input of the same input name oflarray(1)。对于连接到替换层输出的每个图层,将图层重新连接到同一输出名称的输出名称larray(end)

  • '命令'- 重新连接larrayusing the order of the input names oflarray(1)以及输出名称larray(end)。Reconnect the layer connected to theith input of the replaced layer to theith input oflarray(1)。Reconnect the layer connected to thej替换层的输出到jth output oflarray(end)

Data Types:char|string

Output Arguments

全部收缩

Output layer graph, returned as a层Graph目的。

Introduced in R2018b