change outputlayer in layergraph object

Hello,
I have a layerGraph object and I need to remove the output layer because it is not relevant and it is not supported by Deep Learning HDL Toolbox.
I removed another layer and then make the corresponding connection but the output layer is different because I need to change the 'OutputNames' or set the out output layer in other way. However, 'OutputNames' property is read only.
I also tried to replace the last layer by the previous one and remove the previous layer but I get the same result
find in the following link the layerGraph object layers.mat
Is there any way to change the output layer in a layerGraph object?
Thank you in advance.

 Accepted Answer

For example,
lgraph=layerGraph(regressionLayer)
lgraph =
LayerGraph with properties: InputNames: {1×0 cell} OutputNames: {'regressionoutput'} Layers: [1×1 nnet.cnn.layer.RegressionOutputLayer] Connections: [0×2 table]
layers=lgraph.Layers;
layers(end).Name='newName';
lgraph=layerGraph(layers)
lgraph =
LayerGraph with properties: InputNames: {1×0 cell} OutputNames: {'newName'} Layers: [1×1 nnet.cnn.layer.RegressionOutputLayer] Connections: [0×2 table]

8 Comments

Hello @Matt J,
If I'm not wrong, it only changes the name of the layer and biulds a new layerGraph object with the same layer but with a different name.
I tried something similar, replacing the last layer and replacing its name but layerGraph's 'OutputNames' property changes to be empty and last layer seems not no be connected or set as output layer
I need to remove the 'BinaryCrossEntropyRegressionLaye_reshape' and make 'conv2d_8_sigmoid' the output layer.
-->
Before replacing/removing last layer
LayerGraph with properties:
Layers: [22×1 nnet.cnn.layer.Layer]
Connections: [21×2 table]
InputNames: {'input_1'}
OutputNames: {'BinaryCrossEntropyRegressionLayer_reshape'}
After replacing/removing last layer
LayerGraph with properties:
Layers: [21×1 nnet.cnn.layer.Layer]
Connections: [20×2 table]
InputNames: {'input_1'}
OutputNames: {1×0 cell}
However, if i read the name of the last layer it is nt empty (you suggested changing the last layer name but it doesn't change 'outputNames' porperty):
layers.Layers(end).Name
ans =
'conv2d_8_sigmoid'
The problem comess when I try to assemble a NN with 'assembleNetwork' function
Network: Missing output layer. The network must have at least one output layer.
Invalid network.
Caused by:
Network: Missing output layer. The network must have at least one output layer.
Layer 'conv2d_8_sigmoid': Unconnected output. Each layer output must be connected to the input of another layer.
In addition I trid to replace the last layer with a copy of the previous layer changing its name to 'BinaryCrossEntropyRegressionLayer_reshape' but I get 'Output Names' property empty
outLayer=layers.Layers(end-1);
outLayer.Name = layers.Layers(end).Name;
layers = replaceLayer(layers, layers.Layers(end).Name, outLayer)
layers =
LayerGraph with properties:
Layers: [22×1 nnet.cnn.layer.Layer]
Connections: [21×2 table]
InputNames: {'input_1'}
OutputNames: {1×0 cell}
The point how change the connection of the last layer with the output layer or change the reference of the output layer.
I fixed it replacing last layer by a classification layer
Matt J
Matt J on 19 May 2023
Edited: Matt J on 19 May 2023
If I'm not wrong, it only changes the name of the layer and biulds a new layerGraph object with the same layer but with a different name.
Yes, it was an example. Basically, if you extract the Layers property, you can manipulate the layer array freely and build a new graph. It was not clear from your post precisely what change you are trying to make but it should still apply.
I fixed it replacing last layer by a classification layer
I'm glad, but please Accept-click the answer if it resolved your question.
So the problem was that only classification or regression layers are supported to be output layers in a layer graph object, wasn't it? what about the 'BinaryCrossEntropyRegressionLayer_reshape' I had?
So the problem was that only classification or regression layers are supported to be output layers in a layer graph object, wasn't it?
I don't see how the choice of output layer would have been the issue. A layer graph does not even have to have an output layer. It can even consist of a single convolutional layer, e.g.,
lg=layerGraph(convolution2dLayer([1,1],1))
lg =
LayerGraph with properties: InputNames: {1×0 cell} OutputNames: {1×0 cell} Layers: [1×1 nnet.cnn.layer.Convolution2DLayer] Connections: [0×2 table]
In that example 'IutputNames' and 'OutputNames' properties are 1x0 cell. That means that are empty and input and output layers are not defined. I think you will get the same error whe you assemble that layerGraph object with 'assembleNetwork' function, coudl you check it?
It will definitely given an error because it has no input and output layers, but that is not the point. Your original question was how to change the output name property of an output layer within an existing layerGraph and my original answer demonstrated how to do that.
If the question is about making predictions in a network without using an output layer, you can do it by using dlnetwork instead of assembleNetwork.
cl=convolution2dLayer([3,3],1 ,'Padding','same','Weights',rand(3),'Bias',0);
net=dlnetwork(cl,networkDataLayout([8,8,1],'SSC'));
net.predict(dlarray(rand(8),'SSC'))
ans =
8(S) × 8(S) × 1(C) single dlarray 1.9330 1.6235 1.4864 1.6687 2.1314 1.7076 1.5632 1.1103 2.1311 2.7175 1.8709 3.1219 2.9320 2.4912 2.6637 1.8686 2.0587 2.2242 2.5526 2.5633 2.7993 2.7756 2.4032 1.2422 1.8285 2.6174 1.9562 2.4646 2.5606 2.4817 2.1627 1.1586 1.9782 2.2336 2.9013 2.7397 2.2003 2.0638 1.7779 1.1409 1.0009 2.6439 2.3893 3.0688 2.0361 2.4400 2.3526 1.9205 1.3489 2.1934 2.7162 2.8103 2.6220 2.7962 2.9462 2.1174 1.0200 1.3155 1.1040 1.8461 1.8275 1.5256 2.7954 2.0552
I'm sorry but the original question was how to change the output layer itself. Maybe the misunderstanding comes because the 'OutputNames' property. Anyway, question is solved. Thank you

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2021b

Asked:

on 18 May 2023

Commented:

on 24 May 2023

Community Treasure Hunt

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

Start Hunting!