What value does trainNetwork expect from read(ds), when hasdata(ds)= false?

4 views (last 30 days)
** Edit I figured out my issue. read(ds) should throw an error when hasdat(ds) = false. The error needs to include an error id with the mnemonic field 'noMoreData'. I am not exactly sure what component field should be, but it shouldn't be 'MATLAB' because my datastore script isn't part of a Matlab toolbox. I used the name of my datastore class as the component field. I was confused about the fields in matlab errors, so reading this article helped: https://www.mathworks.com/help/matlab/ref/error.html#bumt5nd-msgID. Here is the error message that worked for me:
error('myDatastore:noMoreData',['No more data to read.\nUse the reset ',...
'method to reset the datastore to the start of ' ,...
'the data. \nBefore calling the read method, ',...
'check if data is available to read ',...
'by using the hasdata method.']);
Original question:
I built a custom datastore, and want to use it with a neural network. However, I get an error using trainNetwork when it reaches the end of the first epoch. My read(ds) function returns a cell array, where the first 4 cells are double arrays and the last cell is a categorical. The error I get is "Input four expected a cell but received a double instead."
I developed the datastore based on this documentation, as well as looking at the code for arrayDatastore and tabularTextDatastore. The example for the custom datastore is:
function tf = hasdata(myds)
% Return true if more data is available.
tf = hasfile(myds.FileSet);
end
function [data,info] = read(myds)
% Read data and information about the extracted data.
if ~hasdata(myds)
error(sprintf(['No more data to read.\nUse the reset ',...
'method to reset the datastore to the start of ' ,...
'the data. \nBefore calling the read method, ',...
'check if data is available to read ',...
'by using the hasdata method.']))
end
fileInfoTbl = nextfile(myds.FileSet);
data = MyFileReader(fileInfoTbl);
info.Size = size(data);
info.FileName = fileInfoTbl.FileName;
info.Offset = fileInfoTbl.Offset;
% Update CurrentFileIndex for tracking progress
if fileInfoTbl.Offset + fileInfoTbl.SplitSize >= ...
fileInfoTbl.FileSize
myds.CurrentFileIndex = myds.CurrentFileIndex + 1 ;
end
end
The tabularTextDatastore readData function handles having no data like this:
% error early if no data available
if ~hasdata(ds)
error(message('MATLAB:datastoreio:splittabledatastore:noMoreData'));
end
ArrayDatastore does this:
if ~arrds.hasdata()
msgid = "MATLAB:io:datastore:array:read:NoMoreData";
error(message(msgid));
end
I have tried each of these strategies in my read function (literally copy-pasting those pieces of code, but always get the same error. I can't seem to find the code underlying trainNetwork to see how it handles it. Does anyone know what trainNetwork expects from the read(ds) function when you've run out of data?

Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!