Error using trainNetwork (line 191) Invalid network.

5 visualizaciones (últimos 30 días)
Fatih
Fatih el 2 de Feb. de 2025
Comentada: Fatih el 5 de Feb. de 2025
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.
++

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 2 de Feb. de 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')
Perhaps the inputLayer page is helfpul.
  3 comentarios
Cris LaPierre
Cris LaPierre el 3 de Feb. de 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.
Fatih
Fatih el 5 de Feb. de 2025
Thanks a lot. Once I analyzed it now works.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Image Data Workflows en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by