trainNetwork invalid network

202 views (last 30 days)
Why am I getting an invalid network error from trainNetwork? It says layers are not connected and that the input layer is not first and the output layer is not last. I am resuming functionality of my network following the directions in the link below. When I visualize my layers, they are all connected properly and the first layer is the input layer and the last is the output. Why am I getting these errors?
Error using trainNetwork (line 154)
Invalid network.
Caused by:
Layer 'L1': Missing input. Each layer input must be connected to the
output of another layer.
Layer 'L3': Missing input. Each layer input must be connected to the
output of another layer.
Detected missing inputs:
input 'in2'
...
Layer 'L7': Unused output. Each layer output must be connected to the
input of another layer.
Layer 'L0': An input layer must be first in the layer array.
Layer 'Lend': An output layer must be last in the layer array.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 2 Sep 2021
Edited: MathWorks Support Team on 28 Sep 2021
This error is due to a DAGNetwork object being inputted into the trainNetwork for the layers. The correct workflow is to use a Layer array or a LayerGraph object as input to trainNetwork:
 
To fix the issue, convert the DAGNetwork object into a LayerGraph object first before training:
>> newnet = trainNetwork(source, layerGraph(net),options);
The layer graph describes the architecture of a DAG network (layers and connections). Net.Layers is missing the connections causing those errors.
  3 Comments

Sign in to comment.

More Answers (2)

blue cat
blue cat on 20 Jun 2019
Edited: blue cat on 20 Jun 2019
Hi,
I tried "layerGraph(net)", but I still meet "Error using trainNetwork ; Invalid network; Layer '...': Unused input...." problem. How can I solve it?
Below is my code:
NET = shufflenet;NET.Layers;backbone = NET.Layers(1:170);
layers=[
backbone
fullyConnectedLayer(1,'Name','FC')
regressionLayer('Name','regressionL')];
...
net = trainNetwork(tbtrain,layerGraph(layers),options);
  2 Comments
Mohammad Bhat
Mohammad Bhat on 24 Jan 2020
lgraph = layerGraph(layers);
where layers is your netwrok/architecture
=================================================================================
net = trainNetwork(trainingData,lgraph ,opts);
=======================================
It will solve yoyur problem. It solved mine also

Sign in to comment.


TUGCE SENA ALTUNTAS
TUGCE SENA ALTUNTAS on 10 Feb 2021
Edited: TUGCE SENA ALTUNTAS on 10 Feb 2021
  • I'm trying to use Pretrained Networks (Resnet18, Resnet50 etc.) for training and I am getting the error I mentioned below. Can anyone who knows the reason be helpful?
The Codes:
net = alexnet;
analyzeNetwork(net)
% The first layer, the image input layer, requires input images of size 224-by-224-by-3, where 3 is the number of color channels.
inputSize = net.Layers(1).InputSize
% Replace Final Layers
% The last three layers of the pretrained network net are configured for 1000 classes. These three layers must be fine-tuned for the new classification problem. Extract all layers, except the last three, from the pretrained network.
layersTransfer = net.Layers(1:end-3);
% Transfer the layers to the new classification task by replacing the last three layers with a fully connected layer, a softmax layer, and a classification output layer. Specify the options of the new fully connected layer according to the new data. Set the fully connected layer to have the same size as the number of classes in the new data. To learn faster in the new layers than in the transferred layers, increase the WeightLearnRateFactor and BiasLearnRateFactor values of the fully connected layer.
numClasses = numel(categories(imdsTrain.Labels))
layers = [
layersTransfer
fullyConnectedLayer(numClasses,'WeightLearnRateFactor',20,'BiasLearnRateFactor',20)
softmaxLayer
classificationLayer];
Error:
Error using trainNetwork (line 183)
Invalid network.
Caused by:
Layer 'res2a': Unconnected input. Each layer input must be connected to the output of another
layer.
Detected unconnected inputs:
input 'in2'
Layer 'res2b': Unconnected input. Each layer input must be connected to the output of another
layer.
Detected unconnected inputs:
input 'in2'
Layer 'res3a': Unconnected input. Each layer input must be connected to the output of another
layer.
Detected unconnected inputs:
input 'in2'
Layer 'res3b': Unconnected input. Each layer input must be connected to the output of another
layer.
Detected unconnected inputs:
input 'in2'
Layer 'res4a': Unconnected input. Each layer input must be connected to the output of another
layer.
Detected unconnected inputs:
input 'in2'
Layer 'res4b': Unconnected input. Each layer input must be connected to the output of another
layer.
Detected unconnected inputs:
input 'in2'
Layer 'res5a': Unconnected input. Each layer input must be connected to the output of another
layer.
Detected unconnected inputs:
input 'in2'
Layer 'res5b': Unconnected input. Each layer input must be connected to the output of another
layer.
Detected unconnected inputs:
input 'in2'
  1 Comment
Ibrahim kaya
Ibrahim kaya on 9 Oct 2021
Follow this link:
https://www.mathworks.com/matlabcentral/answers/773617-errors-in-transfer-learning-using-resnet101

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!