error: Invalid input data. Invalid number of spatial dimensions. Layer expects 0 but received 2.
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
I got 6960 traing dataset. each data size is 11890*2(double)
full code here
%% load data
Path1 = fullfile('E:\','m2020','kin2','trainset');
Path2 = fullfile('E:\','m2020','kin2','testset');
dsTrain = myDatastore(Path1)
%% define network
inputSize = dsTrain.SequenceDimension;
numClasses = dsTrain.NumClasses;
numHiddenUnits = 100;
layers = [ ...
sequenceInputLayer(inputSize, 'Name','input')
bilstmLayer(numHiddenUnits,'Name','bilstm')
fullyConnectedLayer(numClasses,'Name','fc')
sigmoidLayer('Name','sigmoid')
];
lgraph = layerGraph(layers);
dlnet = dlnetwork(lgraph)
% spec
numEpochs = 50;
miniBatchSize = 128;
initialLearnRate = 0.01;
decay = 0.01;
momentum = 0.9;
%% define minibatch
mbq = minibatchqueue(dsTrain,...
'MiniBatchSize',miniBatchSize,...
'MiniBatchFcn',@preprocessMiniBatch,...
'MiniBatchFormat',{'SSCB',''});
figure
lineLossTrain = animatedline('Color',[0.85 0.325 0.098]);
ylim([0 inf])
xlabel("Iteration")
ylabel("Loss")
grid on
velocity = [];
%%
iteration = 0;
start = tic;
% Loop over epochs.
for epoch = 1:numEpochs
% Shuffle data.
shuffle(mbq);
% Loop over mini-batches.
while hasdata(mbq)
iteration = iteration + 1;
% Read mini-batch of data.
[dlX, dlY] = next(mbq);
% Evaluate the model gradients, state, and loss using dlfeval and the
% modelGradients function and update the network state.
[gradients,state,loss] = dlfeval(@modelGradients,dlnet,dlX,dlY);
dlnet.State = state;
% Determine learning rate for time-based decay learning rate schedule.
learnRate = initialLearnRate/(1 + decay*iteration);
% Update the network parameters using the SGDM optimizer.
[dlnet,velocity] = sgdmupdate(dlnet,gradients,velocity,learnRate,momentum);
% Display the training progress.
D = duration(0,0,toc(start),'Format','hh:mm:ss');
addpoints(lineLossTrain,iteration,loss)
title("Epoch: " + epoch + ", Elapsed: " + string(D))
drawnow
end
end
this code is almost same with mathworks example(https://kr.mathworks.com/help/deeplearning/ug/train-network-using-custom-training-loop.html)
but the example is using imagedataset so i needed to change some
the error occurs at dlfeval.
I noticed forward(dlnet,dlX) making this error in modelgradients
the error says:
layer 'input' : Invalid input data. Invalid number of spatial dimensions. Layer expects 0 but received 2.
I guess it happens only to sequenceinputlayer.
what's the 0 and 2 meaning? how can I fix it?
4 Kommentare
zahoor m
am 12 Dez. 2023
Layer 'input': Invalid input data. Invalid number of spatial dimensions. Layer expects 2 but received
0.
Antworten (1)
Omega
am 6 Feb. 2025
Hi Yeon,
The error message you are encountering is because of the mismatch in input format expected by the "sequenceInputLayer" and the actual format of your data. The "sequenceInputLayer" expects sequence data without spatial dimensions (i.e., a spatial dimension of zero). This means it expects a 1D vector (of features) per time step.
However, your actual data has two spatial dimensions, which is interpreted as an image or matrix per time step.
To fix this error, convert your 2D data into a format that the "sequenceInputLayer" can handle. Without a sample input data or knowing the exact dimensions, I can not provide specific guidance. However, you might need to reshape the each data point into a sequence of vectors. You can achieve this by overriding the "preprocessMiniBatch" function and reshaping each data point into a vector.
Additionally, you will need to update the data formats for the "MiniBatchFormat" parameter accordingly. You can refer to the following MathWorks documentation link for more information on data formats:
0 Kommentare
Siehe auch
Kategorien
Mehr zu Parallel and Cloud finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!