Clear Filters
Clear Filters

error: Invalid input data. Invalid number of spatial dimensions. Layer expects 0 but received 2.

19 views (last 30 days)
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?
  3 Comments
yanqi liu
yanqi liu on 13 Dec 2021
yes,sir,may be upload the data to do some analysis,such as
https://ww2.mathworks.cn/matlabcentral/answers/1603220-number-of-observations-in-x-and-y-disagree-for-convolution-neural-network?s_tid=srchtitle
zahoor m
zahoor m on 12 Dec 2023
Layer 'input': Invalid input data. Invalid number of spatial dimensions. Layer expects 2 but received
0.

Sign in to comment.

Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!