主要内容

本页翻译不是最新的。点击此处可查看最新英文版本。

connectLayers

在神经网络中连接各层

说明

netUpdated = connectLayers(net,s,d) 将源层 s 连接到 dlnetwork 对象 net 中的目标层 d。更新后的网络 netUpdated 包含与 net 相同的层,并包含新的连接。

示例

示例

全部折叠

创建一个空的神经网络 dlnetwork 对象,并添加一个包含两个输入且名称为 'add' 的相加层。

net = dlnetwork;
layer = additionLayer(2,'Name','add');
net = addLayers(net,layer);

向神经网络添加两个 ReLU 层,并将它们连接到相加层。相加层输出 ReLU 层的输出总和。

layer = reluLayer('Name','relu1');
net = addLayers(net,layer);
net = connectLayers(net,'relu1','add/in1');

layer = reluLayer('Name','relu2');
net = addLayers(net,layer);
net = connectLayers(net,'relu2','add/in2');

在图中可视化更新后的网络。

plot(net)

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

定义一个双输出神经网络,该神经网络在给定二维图像作为输入的情况下预测分类标签和数值。

指定类和响应的数量。

numClasses = 10;
numResponses = 1;

创建一个空的神经网络。

net = dlnetwork;

定义网络主分支的各层和 softmax 输出。

layers = [
    imageInputLayer([28 28 1],Normalization="none")

    convolution2dLayer(5,16,Padding="same")
    batchNormalizationLayer
    reluLayer(Name="relu_1")

    convolution2dLayer(3,32,Padding="same",Stride=2)
    batchNormalizationLayer
    reluLayer
    convolution2dLayer(3,32,Padding="same")
    batchNormalizationLayer
    reluLayer

    additionLayer(2,Name="add")

    fullyConnectedLayer(numClasses)
    softmaxLayer(Name="softmax")];

net = addLayers(net,layers);

添加跳过连接。

layers = [
    convolution2dLayer(1,32,Stride=2,Name="conv_skip")
    batchNormalizationLayer
    reluLayer(Name="relu_skip")];

net = addLayers(net,layers);
net = connectLayers(net,"relu_1","conv_skip");
net = connectLayers(net,"relu_skip","add/in2");

为回归输出添加全连接层。

layers = fullyConnectedLayer(numResponses,Name="fc_2");
net = addLayers(net,layers);
net = connectLayers(net,"add","fc_2");

查看图中的神经网络。

figure
plot(net)

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

输入参数

全部折叠

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

连接源,指定为字符向量或字符串标量。

  • 如果源层只有一个输出,则 s 是该层的名称。

  • 如果源层有多个输出,则 s 是层名称后跟 "/" 字符和层输出的名称:"layerName/outputName"

示例: "conv"

示例: "mpool/indices"

连接目标,指定为字符串标量或字符向量。

  • 如果目标层只有一个输入,则 d 是该层的名称。

  • 如果目标层有多个输入,则 d 是层名称后跟 "/" 字符和层输入的名称:"layerName/inputName"

示例: "fc"

示例: "add/in1"

输出参量

全部折叠

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

要初始化 dlnetwork 对象的可学习参数,请使用 initialize 函数。

connectLayers 函数不会保留量化信息。如果输入网络是量化网络,则输出网络不包含量化信息。

版本历史记录

在 R2017b 中推出

全部展开