I want to build two inputs, one output network. But the first input is an image and the second input is a vector. When I try to train the network with cell array including two sub arrays (one for images, one for vector), I got an error. "Invalid training data for multiple-input network. For multiple-input training, use a single datastore." I created 4D image array, a vector array for each input and labels array for training. How can I combine these data to a DataStore. Matlab Datastore couldn't get the data from defined variable from workspace.
Prashant Kumar answered .
2025-11-20
data = read(ds)
data =
4×3 cell array
{28×28 double} {128×128 double} {[7]}
{28×28 double} {128×128 double} {[7]}
{28×28 double} {128×128 double} {[9]}
{28×28 double} {128×128 double} {[9]}
%% Generate random training data for muliple input network
imgDat = randn([28 28 3 200]);
vectorData = randn([10 1 200]);
trainLabels = categorical(randi([0 1], 200,1));
%% Convert trianing data into cell arrays
imgCells = mat2cell(imgDat,28,28,3,ones(200,1));
vectorCells = mat2cell(vectorData,10,1,ones(200,1));
imgCells = reshape(imgCells,[200 1 1]);
vectorCells = reshape(vectorCells,[200 1 1]);
labelCells = arrayfun(@(x)x,trainLabels,'UniformOutput',false);
combinedCells = [imgCells vectorCells labelCells];
%% Save the converted data so that it can be loaded by filedatastore
save('traingData.mat','combinedCells');
filedatastore = fileDatastore('traingdata.mat','ReadFcn',@load);
trainingDatastore = transform(filedatastore,@rearrangeData);
%% Define muliple input network
layers1 = [
imageInputLayer([28 28 3],'Name','input')
convolution2dLayer(3,16,'Padding','same','Name','conv_1')
batchNormalizationLayer('Name','BN_1')
reluLayer('Name','relu_1')
fullyConnectedLayer(10,'Name','fc11')
additionLayer(2,'Name','add')
fullyConnectedLayer(2,'Name','fc12')
softmaxLayer('Name','softmax')
classificationLayer('Name','classOutput')];
lgraph = layerGraph(layers1);
layers2 = [imageInputLayer([10 1],'Name','vinput')
fullyConnectedLayer(10,'Name','fc21')];
lgraph = addLayers(lgraph,layers2);
lgraph = connectLayers(lgraph,'fc21','add/in2');
plot(lgraph)
%% Define trainingOptions and also set 'Shuffle' to 'never' for this workaround to work
options = trainingOptions('adam', ...
'InitialLearnRate',0.005, ...
'LearnRateSchedule','piecewise',...
'MaxEpochs',300, ...
'MiniBatchSize',1024, ...
'Verbose',1, ...
'Plots','training-progress',...
'Shuffle','never');
net = trainNetwork(trainingDatastore,lgraph,options);
%% function to be used to transform the filedatastore
%to ensure the read(datastore) returns M-by-3 cell array ie., (numInputs+1) columns
function out = rearrangeData(ds)
out = ds.combinedCells;
end