Response must not contain any NaNs??

30 ビュー (過去 30 日間)
Sho Wright
Sho Wright 2021 年 1 月 3 日
コメント済み: Noel Png 2021 年 6 月 24 日
I am trying to make a image to number CNN with a regression layer, and keep getting the error: "Error using trainNetwork (line 183) Invalid training data. For regression tasks, responses must be a vector, a matrix, or a 4-D array of numeric responses. Responses must not contain NaNs."
I'm attempting to use the imageDatastore function, and convert it into 4-D array using imds2array, and I'm not sure how I set it up incorrectly, here's my code so far:
Why is it "Not a Number"? What should I be changing/adding to get past this error?
%Loading Dataset
imds = imageDatastore('PlaceLocationHere', ...
'IncludeSubfolders',true, ...
[X, Y] = imds2array(imds);
layers = [
imageInputLayer([25 25 1])
%Network Options
miniBatchSize = 128;
validationFrequency = floor(numel(Y)/miniBatchSize);
options = trainingOptions('sgdm', ...
'MiniBatchSize',miniBatchSize, ...
'MaxEpochs',8, ...
'InitialLearnRate',1e-3, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropFactor',0.1, ...
'LearnRateDropPeriod',20, ...
'Shuffle','every-epoch', ...
'ValidationData',{X,Y}, ...
'Plots','training-progress', ...
%Training the network
net=trainNetwork(X, Y, layers, options); %What should I put as the input?
function [X, Y] = imds2array(imds)
% X - Input data as an H-by-W-by-C-by-N array, where H is the
% height and W is the width of the images, C is the number of
% channels, and N is the number of images.
% Y - Categorical vector containing the labels for each observation.
imagesCellArray = imds.readall();
numImages = numel( imagesCellArray );
[h, w, c] = size( imagesCellArray{1} );
X = zeros( 1365, 2048, 3, 16); % size of images in practice folder (h,w,c,n)
for i=1:numImages
X(:,:,:,i) = im2double( imagesCellArray{i} );
Y = imds.Labels;


Athul Prakash
Athul Prakash 2021 年 1 月 6 日
Hi Sho,
NaN is a special floating-point value which is used as a placeholder in cases where a 'double' type number was expected but could not be obtained. Sometimes, NaN is deliberately set to indicate a soft error. You can learn more about NaN in this doc:Missing Data in MATLAB.
There are many workflows for which the input data cannot be a NaN, such as for trainNetwork as in your case.
It seems like some of the data, from the datastore you have creted, could not be converted to array values and were set to NaN instead. You may inspect the variable 'X' to find where are the NaNs.
isnan(X) % You can start with isnan() to locate the NaN values.
Additionally, settings breakpoints in your script and inspecting the input data as it is being processed should help you to identify where the NaNs are originating. Set BreakPoints in MATLAB
Hope it Helps!
  2 件のコメント
Noel Png
Noel Png 2021 年 6 月 24 日
Hi, could you elaborate on your solution ?
Im facing a similar problem


その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by