主要内容

replaceLayer

在层图或网络中替换层

描述

例子

lgraphUpdated= replaceLayer (lgraphlayerNamelarray替换图层layerName在图层图中lgraph有了图层larray

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

例子

netUpdated= replaceLayer (layerNamelarray替换图层layerNamedlnetwork对象有了图层larray

replaceLayer连接层larray顺序连接larray进入网络。

___= replaceLayer (___“ReconnectBy”,模式另外指定了重新连接层的方法。

例子

全部折叠

定义一个简单的网络架构并绘制它。

图层= [imageInputLayer([28 28 1],“名字”“输入”) convolution2dLayer(3, 16岁,“填充”“相同”“名字”“conv_1”) reluLayer (“名字”“relu_1”) additionLayer (2“名字”“添加”) fullyConnectedLayer (10“名字”“俱乐部”) softmaxLayer (“名字”“softmax”) classificationLayer (“名字”“classoutput”));lgraph = layerGraph(图层);lgraph = connectLayers(“输入”“添加/ in2”);图绘制(lgraph)

图中包含一个轴对象。axis对象包含一个graphplot类型的对象。

将网络中的ReLU层替换为批处理规范化层,然后再替换为泄漏的ReLU层。

larray = [batchNormalizationLayer(“名字”“BN1”) leakyReluLayer (“名字”“leakyRelu_1”“规模”0.1)];lgraph =替换层(lgraph,“relu_1”, larray);情节(lgraph)

图中包含一个轴对象。axis对象包含一个graphplot类型的对象。

这个示例展示了如何从预训练的Keras网络导入层,用自定义层替换不支持的层,并将这些层组装成一个网络,以便进行预测。万博1manbetx

导入Keras网络

从Keras网络模型导入层。美国的网络“digitsDAGnetwithnoise.h5”对数字图像进行分类。

文件名=“digitsDAGnetwithnoise.h5”;lgraph = importKerasLayers(文件名,“ImportWeights”,真正的);
警告:无法导入一些Keras层,因为深度学习工具箱不支持它们。万博1manbetx它们已被占位符层所取代。为了找到这些层,在返回的对象上调用findPlaceholderLayers函数。

Keras网络包含一些深度学习工具箱不支持的层。万博1manbetx的importKerasLayers函数显示警告并将不支持的层替换为占位符层。万博1manbetx

绘制层图使用情节

图表(lgraph)标题(“进口网络”

图中包含一个轴对象。标题为Imported Network的axes对象包含一个graphplot类型的对象。

替换占位符图层

要替换占位符层,首先确定要替换的层的名称。找到占位符层使用findPlaceholderLayers

placeholderLayers = findPlaceholderLayers(lgraph)
1' gaussian_noise_1' PLACEHOLDER LAYER 'GaussianNoise' Keras LAYER 2' gaussian_noise_2' PLACEHOLDER LAYER 'GaussianNoise' Keras层的占位符

显示这些层的Keras配置。

placeholderLayers。KerasConfiguration
ans =带字段的结构:可训练:1名:'gaussian_noise_1' stddev: 1.5000
ans =带字段的结构:可训练:1名:'gaussian_noise_2' stddev: 0.7000

定义一个自定义高斯噪声层。要创建这个图层,保存文件gaussianNoiseLayer.m在当前文件夹中。然后,创建两个高斯噪声层,与导入的Keras层配置相同。

gnLayer1 = gaussianNoiseLayer(1.5,“new_gaussian_noise_1”);gnLayer2 = gaussianNoiseLayer(0.7,“new_gaussian_noise_2”);

使用自定义层替换占位符层replaceLayer

lgraph =替换层(lgraph,“gaussian_noise_1”, gnLayer1);lgraph =替换层(lgraph,“gaussian_noise_2”, gnLayer2);

绘制更新后的图层图情节

图表(lgraph)标题(“替换层的网络”

图中包含一个轴对象。标题为Network with replace Layers的axes对象包含一个graphplot类型的对象。

指定类名

如果导入的分类层不包含类,则必须在预测之前指定这些类。如果您没有指定类,那么软件将自动将类设置为12、……N,在那里N是类的数量。

属性来查找分类层的索引层图的属性。

lgraph。层
ans = 15x1带有图层的图层数组:1“input_1”28 x28x1图像输入图像2 conv2d_1的二维卷积20 7 x7x1旋转步[1]和填充“相同”3“conv2d_1_relu”ReLU ReLU 4“conv2d_2”二维卷积20 3 x3x1旋转步[1]和填充“相同”5“conv2d_2_relu”ReLU ReLU 6 new_gaussian_noise_1高斯噪声的高斯噪声标准差为1.5 7 new_gaussian_noise_2高斯噪声的高斯噪声标准差为0.7 8“max_pooling2d_1”二维最大池2 x2马克斯池与步伐[2 2] and padding 'same' 9 'max_pooling2d_2' 2-D 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' Softmax softmax 15 'ClassificationLayer_activation_1' Classification Output crossentropyex

分类层有名称“ClassificationLayer_activation_1”.查看分类层,检查财产。

cLayer = lgraph.Layers(end)
cLayer = ClassificationOutputLayer与属性:名称:'ClassificationLayer_activation_1'类:'auto' ClassWeights: 'none' OutputSize: 'auto'超参数LossFunction: 'crossentropyex'

因为层的属性为“汽车”时,必须手动指定类。将类设置为01、……9,然后将导入的分类层替换为新的分类层。

粘土。class = string(0:9)
cLayer = ClassificationOutputLayer与属性:名称:'ClassificationLayer_activation_1'类:[0 1 2 3 4 5 6 7 8 9]ClassWeights: 'none' OutputSize: 10超参数LossFunction: 'crossentropyex'
lgraph =替换层(lgraph,“ClassificationLayer_activation_1”、粘土);

组装网络

使用组合图层图assembleNetwork.函数返回一个DAGNetwork对象,该对象已准备用于预测。

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

输入参数

全部折叠

层图,指定为aLayerGraph对象。要创建图层图,请使用layerGraph

神经网络,指定为adlnetwork对象。

要替换的层的名称,指定为字符串标量或字符向量。

网络层,指定为a数组中。

有关内置层的列表,请参见深度学习层列表

方法重新连接指定为下列之一的层:

  • “名字”——重新连接larray使用被替换层的输入和输出名称。对于连接到被替换层的输入的每个层,将该层重新连接到的相同输入名称的输入larray (1).对于连接到被替换层的输出的每个层,将该层重新连接到相同输出名称的输出larray(结束)

  • “秩序”——重新连接larray的输入名称的顺序larray (1)的输出名称larray(结束).的连接层重新连接第Th个被替换层的输入的Th输入larray (1).的连接层重新连接j第Th输出替换层到j的输出larray(结束)

数据类型:字符|字符串

输出参数

全部折叠

更新的图层图,返回为LayerGraph对象。

更新后的网络,以未初始化的形式返回dlnetwork对象。

初始化a的可学习参数dlnetwork对象时,使用初始化函数。

版本历史

在R2018b中引入