Error using trainNetwork (line 191) Invalid network.
5 views (last 30 days)
Show older comments
I receive the following error for my function given below. .Would you please help me to correct the function?
for the following input
YPred = trainAndPredictHADEL(XTrain, YTrain, XTest);
++
% Function to train and predict using HADEL model
function YPred = trainAndPredictHADEL(XTrain, YTrain, XTest)
% Define HADEL model architecture
numFeatures = size(XTrain, 2);
numClasses = numel(categories(YTrain));
% Feature-Level Attention
featureAttention = [
fullyConnectedLayer(64, 'Name', 'fc_feature_attention')
reluLayer('Name', 'relu_feature_attention')
fullyConnectedLayer(1, 'Name', 'fc_feature_weights')
softmaxLayer('Name', 'feature_attention_weights')
];
% Temporal Attention (not used for Iris dataset, but included for completeness)
temporalAttention = [
sequenceInputLayer(numFeatures, 'Name', 'input_sequence')
lstmLayer(64, 'OutputMode', 'sequence', 'Name', 'lstm_temporal_attention')
fullyConnectedLayer(1, 'Name', 'fc_temporal_weights')
softmaxLayer('Name', 'temporal_attention_weights')
];
% Combine into Hierarchical Attention
%%Bu kısmı hata verdi aşağıdaki gibi koydum
% hierarchicalAttention = [
% featureAttention
% temporalAttention
% dotProductLayer(1, 'Name', 'weighted_output')
% ];
hierarchicalAttention = [
featureAttention
temporalAttention
];
% Add classification layers
layers = [
hierarchicalAttention
fullyConnectedLayer(64, 'Name', 'fc_final')
reluLayer('Name', 'relu_final')
fullyConnectedLayer(numClasses, 'Name', 'fc_output')
softmaxLayer('Name', 'softmax_output')
classificationLayer('Name', 'output')
];
% Train the network
options = trainingOptions('adam', ...
'MaxEpochs', 50, ...
'MiniBatchSize', 8, ...
'Verbose', false);
net = trainNetwork(XTrain, YTrain, layers, options);
% Predict on test data
YPred = classify(net, XTest);
end
++
Error
++
Error using trainNetwork (line 191)
Invalid network.
Error in trainAndPredictHADEL (line 54)
net = trainNetwork(XTrain, YTrain, layers, options);
Caused by:
Layer 'fc_feature_attention': Unconnected input. Each layer input must be connected to the output of another layer.
Layer 'input_sequence': An input layer must be first in the layer array.
++
0 Comments
Accepted Answer
Cris LaPierre
on 2 Feb 2025
You are missing an input layer for your model. See the final error message: " Layer 'input_sequence': An input layer must be first in the layer array."
Your first layer is fullyConnectedLayer(64, 'Name', 'fc_feature_attention')
3 Comments
Cris LaPierre
on 3 Feb 2025
I think the issue here is that you have defined the concatenation layer to have 2 inputs, but have only provided one. See the concatenationLayer doc page.
Perhaps this layer is not doing what you think. Try removing it.
More Answers (0)
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!