replaceLayer

在层图中替换层

描述

例子

newlgraph= replaceLayer(lgraph,layerName,larray)替换层layerName在图层图中lgraph与所述层中larray

replaceLayer连接层在larray顺序并连接larray入层图形。

例子

newlgraph= replaceLayer(lgraph,layerName,larray'ReconnectBy',模式)另外指定重新连接层的方法。

例子

全部折叠

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

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

与一批正常化层,接着漏RELU层在网络中更换RELU层。

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

这个例子展示了如何从一个预先训练好的Keras网络中导入这些层,用定制层替换不支持的层,并将这些层组装到一个网络中以备预测。万博1manbetx

进口Keras网络

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

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

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

绘制层图使用情节

图图(lgraph)称号(“进口网络”)

替换占位符层

要替换占位符层,首先要标识要替换的层的名称。使用以下命令查找占位符层findPlaceholderLayers

placeholderLayers = findPlaceholderLayers (lgraph)
placeholderLayers = 2×1 PlaceholderLayer阵列层:1 'gaussian_noise_1' PLACEHOLDER LAYER占位符 '高斯噪声' Keras层2 'gaussian_noise_2' PLACEHOLDER LAYER占位符 '高斯噪声' Keras层

显示这些层的Keras配置。

placeholderLayers.KerasConfiguration
ans =同场的结构:可训练:1名:'gaussian_noise e_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 = replaceLayer (lgraph,“gaussian_noise_1”,gnLayer1);lgraph = replaceLayer (lgraph,“gaussian_noise_2”,gnLayer2);

使用绘制更新的层图形情节

图图(lgraph)称号(“替换图层的网络”)

指定类名

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

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

lgraph.Layers
ANS = 15X1层阵列层:1 'INPUT_1' 图像输入28x28x1图像2 'conv2d_1' 卷积20个7x7x1卷积步幅[1 1]和填充 '相同' 3 'conv2d_1_relu' RELU RELU 4 'conv2d_2' 卷积20个3x3x1卷积用步幅[1 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”最大池2x2的最大池与步幅[2 2]和填补“相同” 9“max_pooling2d_2”最大池2x2的最大蓄留与步幅[2 2]和填补“相同” 10“flatten_1” Keras平铺展平激活成1- d假设C-式(行主)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”。查看分类层,然后检查财产。

粘土= lgraph.Layers(结束)
CLAYER = ClassificationOutputLayer与属性:名称: 'ClassificationLayer_activation_1' 类: '自动' OutputSize: '自动' 超参数LossFunction: 'crossentropyex'

因为该层的属性是“汽车”,您必须手动指定的类。设置类0,1、……9,然后替换用新导入的分类层。

cLayer.Classes =串(0:9)
cLayer = ClassificationOutputLayer with properties: Name: 'ClassificationLayer_activation_1'类:[0 1 2 3 4 5 6 7 8 9]OutputSize: 10个超参数
lgraph = replaceLayer (lgraph,“ClassificationLayer_activation_1”、粘土);

组装网络

使用下列工具组装层图assembleNetwork。该函数返回一个DAGNetwork对象是准备用于预测。

净= assembleNetwork (lgraph)
net = DAGNetwork with properties: Layers:[15×1 nnet.cn .layer. layer] Connections:[15×2 table] InputNames: {'input_1'} OutputNames: {'ClassificationLayer_activation_1'}

输入参数

全部折叠

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

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

网络层,指定为数组中。

对于内置层,请参阅列表深度学习层次列表

方法来重新连接层指定为以下之一:

  • '名称'- 重新连接larray使用被替换层的输入和输出名称。对于连接到替换层的输入的每个层,重新将该层连接到具有相同输入名称的输入larray (1)。对于连接到替换层的输出各层,重新连接层的相同的输出名称的输出larray(结束)

  • '订购'- 重新连接larray使用的输入名称的顺序larray (1)和输出名称larray(结束)。重新连接层连接到层的输入th的输入larray (1)。重新连接层连接到j被替换的层对的个输出jth的输出larray(结束)

数据类型:烧焦|字符串

输出参数

全部折叠

输出层图形,作为a返回LayerGraph对象。

介绍了R2018b