findreplace matlab,findLayersToReplace.m

该代码段定义了一个函数`findLayersToReplace`,用于查找给定的层图(layer graph)中单一的分类层及其前一个可学习层(全连接层或卷积层)。首先检查输入是否为`LayerGraph`对象,然后通过遍历层图的连接找到分类层。如果网络有分支或分类层前没有可学习层,则抛出错误。最终返回找到的可学习层和分类层。
摘要由CSDN通过智能技术生成

% findLayersToReplace(lgraph) finds the single classification layer and the

% preceding learnable (fully connected or convolutional) layer of the layer

% graph lgraph.

function [learnableLayer,classLayer] = findLayersToReplace(lgraph)

if ~isa(lgraph,'nnet.cnn.LayerGraph')

error('Argument must be a LayerGraph object.')

end

% Get source, destination, and layer names.

src = string(lgraph.Connections.Source);

dst = string(lgraph.Connections.Destination);

layerNames = string({lgraph.Layers.Name}');

% Find the classification layer. The layer graph must have a single

% classification layer.

isClassificationLayer = arrayfun(@(l) ...

(isa(l,'nnet.cnn.layer.ClassificationOutputLayer')|isa(l,'nnet.layer.ClassificationLayer')), ...

lgraph.Layers);

if sum(isClassificationLayer) ~= 1

error('Layer graph must have a single classification layer.')

end

classLayer = lgraph.Layers(isClassificationLayer);

% Traverse the layer graph in reverse starting from the classification

% layer. If the network branches, throw an error.

currentLayerIdx = find(isClassificationLayer);

while true

if numel(currentLayerIdx) ~= 1

error('Layer graph must have a single learnable layer preceding the classification layer.')

end

currentLayerType = class(lgraph.Layers(currentLayerIdx));

isLearnableLayer = ismember(currentLayerType, ...

['nnet.cnn.layer.FullyConnectedLayer','nnet.cnn.layer.Convolution2DLayer']);

if isLearnableLayer

learnableLayer = lgraph.Layers(currentLayerIdx);

return

end

currentDstIdx = find(layerNames(currentLayerIdx) == dst);

currentLayerIdx = find(src(currentDstIdx) == layerNames);

end

end

一键复制

编辑

Web IDE

原始数据

按行查看

历史

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值