DLnetwork definition and training problem with outputLayer

47 views (last 30 days)
I need to use dlnetwork (in order to export it to ONNX format).
I have defined its layers as follows:
layers = [
sequenceInputLayer(202,"Name","sequence")
fullyConnectedLayer(25,"Name","fc")
fullyConnectedLayer(2,"Name","fc_1")
softmaxLayer("Name","softmax")];
and then I defined the net:
nett = dlnetwork(layers);
so until here everything is fine.
When I am trying to train the network:
[net,tr] = trainNetwork(DATA,Alllabels, layers, options);
I get the following error:
Network: Missing output layer. The network must have at least one output layer.
Layer 'softmax': Unconnected output. Each layer output must be connected to the input of another
layer.
So I redefined my layers to include an ouput layer:
layers = [
sequenceInputLayer(202,"Name","sequence")
fullyConnectedLayer(25,"Name","fc")
fullyConnectedLayer(2,"Name","fc_1")
softmaxLayer("Name","softmax")
classificationLayer()];
but now the definition of the network as before (nett = dlnetwork(layers) ) returns the following error:
Layer 'classoutput': Detected output layer. The network must not have output layers.
So this is kind of a catch-22 problem - I need to add an output layer to train the network, but I must remove an output layer for defining it!!
Does someone have clue?
Thanks

Answers (2)

Abhinav Aravindan
Abhinav Aravindan on 8 May 2024
I understand that you are trying to train a deep learning network and export the trained network as an “ONNX” file but facing an issue in defining and training the network. While utilizing dlnetwork, the layers cannot contain an output layer.
The network input to “exportONNXNetwork” need not be a “dlnetwork” object. “layers” array can be utilized directly as an input to “trainNetwork for training the neural network. The output of “trainNetwork” can then be exported as an “ONNX” file as given below. Kindly note that this is with respect to MATLAB R2023a.
% Define your network layers
layers = [
sequenceInputLayer(202,"Name","sequence")
fullyConnectedLayer(25,"Name","fc")
fullyConnectedLayer(2,"Name","fc_1")
softmaxLayer("Name","softmax")
classificationLayer()];
% Replace with your data and required training options
net = trainNetwork(data,layers,options);
% Export trained network as ONNX file
exportONNXNetwork(net, filename.onnx)
Please find below the documentation links for your reference.
I hope this helps resolve your issue!

Lucas García
Lucas García on 11 May 2024

You should use the trainnet function instead of trainNetwork to train dlnetwork objects. That said, you can convert the Series or DAG network trained with trainNetwork if you are using R2024a by using dag2dlnetwork .

Categories

Find more on Image Data Workflows in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!