DLnetwork definition and training problem with outputLayer

48 次查看(过去 30 天)
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

回答(2 个)

Abhinav Aravindan
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 2024-5-11

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 .

类别

Help CenterFile Exchange 中查找有关 Image Data Workflows 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by