Y. K. asked . 2022-11-11

How can I train multi-input deep network without DataStore

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.
 

multi-input

multi-input deep n...deep learning , arraydatastore , Deep Learning Toolbox , Deep Learning with T

Expert Answer

Prashant Kumar answered . 2024-12-21 15:42:47

To define and train a deep learning network with multiple inputs, specify the network architecture using a layerGraph object and train using the trainNetwork function by specifying the multiple inputs using a combinedDatastore or transformedDatastore object.
 
 
For networks with multiple inputs, the datastore must be a combined or transformed datastore that returns a cell array with (numInputs+1) columns containing the predictors and the responses, where numInputs is the number of network inputs and numResponses is the number of responses. For i less than or equal to numInputs, the ith element of the cell array corresponds to the input layers.InputNames(i), where layers is the layer graph defining the network architecture. The last column of the cell array corresponds to the responses.
 
 
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]}
Hence in order to train Multiple input networks you have to use a datastore. Refer to Input Arguments & ds of trainNetwork and Multiple-Input and Multiple-Output Networks, Input Datastore for Training, Validation, and Inference for more information.
 
You can make use of the following code as workaround if your training data is saved in a .mat file:
 
 
%% 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

 


Not satisfied with the answer ?? ASK NOW

Frequently Asked Questions

MATLAB offers tools for real-time AI applications, including Simulink for modeling and simulation. It can be used for developing algorithms and control systems for autonomous vehicles, robots, and other real-time AI systems.

MATLAB Online™ provides access to MATLAB® from your web browser. With MATLAB Online, your files are stored on MATLAB Drive™ and are available wherever you go. MATLAB Drive Connector synchronizes your files between your computers and MATLAB Online, providing offline access and eliminating the need to manually upload or download files. You can also run your files from the convenience of your smartphone or tablet by connecting to MathWorks® Cloud through the MATLAB Mobile™ app.

Yes, MATLAB provides tools and frameworks for deep learning, including the Deep Learning Toolbox. You can use MATLAB for tasks like building and training neural networks, image classification, and natural language processing.

MATLAB and Python are both popular choices for AI development. MATLAB is known for its ease of use in mathematical computations and its extensive toolbox for AI and machine learning. Python, on the other hand, has a vast ecosystem of libraries like TensorFlow and PyTorch. The choice depends on your preferences and project requirements.

You can find support, discussion forums, and a community of MATLAB users on the MATLAB website, Matlansolutions forums, and other AI-related online communities. Remember that MATLAB's capabilities in AI and machine learning continue to evolve, so staying updated with the latest features and resources is essential for effective AI development using MATLAB.

Without any hesitation the answer to this question is NO. The service we offer is 100% legal, legitimate and won't make you a cheater. Read and discover exactly what an essay writing service is and how when used correctly, is a valuable teaching aid and no more akin to cheating than a tutor's 'model essay' or the many published essay guides available from your local book shop. You should use the work as a reference and should not hand over the exact copy of it.

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.