How is data characterised as 'spatial or temporal' in the context of neural networks?
4 views (last 30 days)
Show older comments
Nikolas Katsaros
on 3 Mar 2023
Commented: Nikolas Katsaros
on 7 Mar 2023
I have been repeatedly facing this error and it confuses me. Any explanations or general knowledge on the fullyConnectedLayer would be very helpful!
Invalid input data for fully connected layer. The input data must not have both spatial and temporal dimensions.
I am trying to classify 1D time series data (signals) into 2 classes using a 1D CNN using the following code:
inputSize = size(xTrain);
numClasses = 2; % Cracked or not cracked
layers = [
sequenceInputLayer(inputSize,'Name', 'input','MinLength', time_points)
convolution1dLayer(32,20,'Padding', 'same')
batchNormalizationLayer
reluLayer
maxPooling1dLayer(2,'Stride',2)
convolution1dLayer(64,10,'Padding', 'same')
batchNormalizationLayer
reluLayer
maxPooling1dLayer(2,'Stride',2)
fullyConnectedLayer(256)
reluLayer
dropoutLayer(0.5)
fullyConnectedLayer(numClasses)
softmaxLayer
classificationLayer];
%----------------------Define training options-----------------------------
options = trainingOptions('adam', ...
'InitialLearnRate',0.01, ...
'MaxEpochs',50, ...
'MiniBatchSize',128, ...
'Shuffle','every-epoch', ...
'ValidationData',{xTest,yTest}, ...
'ValidationFrequency',5, ...
'Verbose',false, ...
'Plots','training-progress');
% Train the network
net = trainNetwork(xTrain, yTrain, layers, options);
The input data is in a cell array with dimensions (320,2) where col 1 is the signal data and col 2 is the label(a 0 or 1). The signal data is a 2000x1 array with complex values representing the amplitude of a wave at a point in time.
If anyone has any idea how to resolve this - please let me know!
Thank you
0 Comments
Accepted Answer
Nayan
on 7 Mar 2023
As your error suggests, the input data to your fullyConnectedLayer has both spatial and temporal dimensions" . This may have occured because the "maxPooling1dLayer" applied to the "convolution1dLayer(64,10,'Padding', 'same')" retains the number of channels after "maxPooling". You may want to use "layer = flattenLayer" after the maxPooling1dLayer(2,'Stride',2) layer.
I would suggest you to go through https://www.mathworks.com/help/deeplearning/ref/nnet.cnn.layer.flattenlayer.html to read more about flatten.
Also, it would helpful to use "analyzeNetwork" to get more details on the dimensions of each layers.
Read about analyzeNetwork from https://www.mathworks.com/help/deeplearning/ref/analyzenetwork.html
3 Comments
Nayan
on 7 Mar 2023
Happy Learning Nikolas, Hope "analyzeNetwork " can help you understand your model better and get the desired results.
More Answers (1)
Matt J
on 7 Mar 2023
Edited: Matt J
on 7 Mar 2023
The input data is in a cell array with dimensions (320,2) where col 1 is the signal data and col 2 is the label(a 0 or 1). The signal data is a 2000x1 array with complex values representing the amplitude of a wave at a point in time.
This seems to violate the requirements in the trainNetwork documentation in several ways. See the doc excerpt below, in particular the high-lighted sections.
Additionally, it is not clear how you intend he network to process your complex-valued input. How are the maxpooling layers to maximize over numbers that are not real-valued? How will the backpropagation process take derivatives with respect to complex-valued functions (in theory I guess there is a way, but I'm doubtful if trainNetwork is that fancy). What you might want to do is split the real and imaginary parts of the signal into separate channels.
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!