Skip to content

Prefer concatentation over connectLayers #1

@ManWithSleeve

Description

@ManWithSleeve

Compare:

lgraph = addLayers(lgraph, softmaxLayer('Name','softmax'));
lgraph = addLayers(lgraph, classificationLayer('Name','classification'));
lgraph = connectLayers(lgraph,'fc','softmax');
lgraph = connectLayers(lgraph,'softmax','classification');

with
gapFc = [
globalAveragePooling2dLayer(Name="GAP")
fullyConnectedLayer(num_classes)
];
lgraph = addLayers(lgraph,gapFc);
lgraph = connectLayers(lgraph,"batchnorm_"+2*depth,"GAP");

I think that the second is nicer as it only requires one call to addLayers and connectLayers

     out = [
         softmaxLayer(Name='softmax'))
         classificationLayer(Name='classification'))
     ];
     lgraph = addLayers(lgraph, out);
     lgraph = connectLayers(lgraph,'fc','softmax');

And then, because of the call to connectLayers I think it is better to explicitly name the final fully connected layer rather than reply on automatic naming, i.e., L90 should be fullyConnectedLayer(num_classes,Name='fc')

Another thing to consider is to concatenate the last two layers with the previous two before the call to addLayer, i.e.,

last = [
    globalAveragePooling2dLayer(Name="GAP")
    fullyConnectedLayer(num_classes)
];
if connectOutputLayers 
    last = [
        last
        softmaxLayer()
        classificationLayer()
    ];
end
lgraph = addLayers(lgraph,last);
lgraph = connectLayers(lgraph,"batchnorm_"+2*depth,"GAP");

With that version, you don't need to explicitly name most of the layers, just then ones that are referenced in the calls to connectLayers.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions