Are dlnetworks supposed to be allowed to have output layers?

14 ビュー (過去 30 日間)
Matt J
Matt J 2025 年 3 月 28 日
コメント済み: Jack 2025 年 4 月 2 日
Are dlnetworks allowed to have output layers? In the following code, I manage to create one, so the answer would seem to be yes.
layers= [imageInputLayer([1,1,1]) , reluLayer(Name='relu') ] ;
dln = replaceLayer( dlnetwork(layers) ,'relu', regressionLayer);
class(dln)
ans = 'dlnetwork'
dln.Layers
ans =
2x1 Layer array with layers: 1 'imageinput' Image Input 1x1x1 images with 'zerocenter' normalization 2 'regressionoutput' Regression Output mean-squared-error
However, when I try to create this more directly, an error is raised:
dln = dlnetwork( [imageInputLayer([1,1,1]) , regressionLayer] )
Error using dlnetwork/initialize (line 600)
Invalid network.

Error in dlnetwork (line 182)
net = initialize(net, dlX{:});

Caused by:
Layer 'regressionoutput': Detected output layer. The network must not have output layers.
Have I found an unintended backdoor?

採用された回答

Jack
Jack 2025 年 3 月 28 日
By design, dlnetwork objects are intended for custom training loops and are not supposed to include output layers like regressionLayer or classificationLayer. If you try to create a dlnetwork directly with an output layer, MATLAB throws an error. The fact that replaceLayer can slip in an output layer is effectively a workaround, but it isn’t officially supported.
Follow me so you can message me anytime with future questions. If this helps, please accept the answer and upvote it as well.
  3 件のコメント
Jack
Jack 2025 年 4 月 1 日
Regarding documentation, while you might not find an explicit statement saying “dlnetwork objects must not contain output layers,” the behavior and error messages in MATLAB reflect this design choice. The documentation and examples for dlnetwork consistently show networks built without an output layer, reinforcing that output layers are meant to be handled externally in your training loop.
Jack
Jack 2025 年 4 月 2 日
@Matt J did this clear it up?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImage Data Workflows についてさらに検索

製品


リリース

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by