MATLAB Answers

Can I use "trainNetwork" to train deep neural networks with non-image or non-sequence data for regression​/classific​ation?

96 views (last 30 days)
Can I use "trainNetwork" to train deep neural networks with non-image or non-sequence data for regression/classification?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 22 Oct 2020
Edited: MathWorks Support Team on 8 Dec 2020
It is possible to use "trainNetwork" to train deep neural networks for regression/classification using non-image or non-sequence data.
For MATLAB R2020b and later:
Use "featureInputLayer" when you have a data set of numeric scalars representing features (data without spatial or time dimensions).
The following example in the "trainNetwork" documentation page uses "featureInputLayer" to train a deep learning model.
For MATLAB R2020a and earlier:
If you follow the documentation link:
for "trainNetwork", then the function assumes the input "X" to be an image matrix of size 4D with the last dimension corresponding to different images. The output "Y" can be 2D matrix. So you can use the following workaround to reshape the input dataset, if you want to use deep neural networks for solving traditional regression and/or classification problems for non-image, non-sequence data. 
nFeatures = 20; 
nExamples = 10000;
nOutputs = 1; % this example is for setting up a regression problem
x = rand(nExamples,nFeatures);
t = rand(nExamples, nOutputs);
XNew = reshape(x', [1,1,size(x,2),size(x,1)]);
The new input "XNew" now has the last dimension corresponding to different observations in the data set. 
You will also have to change the layers as shown below:\n
layers = [ ...
imageInputLayer([1 1 nFeatures]); % this layer needs the first 3 dimensions of input "XNew"
fullyConnectedLayer(10);
fullyConnectedLayer(nOutputs); % this connected layer needs to have an output-size same as the number of responses (columns) in the output data set "t"
regressionLayer];
options = trainingOptions('sgdm');
trainedNet = trainNetwork(XNew, t, layers, options);
\n
"trainNetwork" is actually agnostic to images or any other type of data, as long as you have less than 3 dimensions and all observations have the same dimensionality. In that case, observations are the 4-th dimension, like this example. This has the advantage that data requiring up to 3 dimensions (like colour images) can be easily represented. However, "trainNetwork" is not agnostic to the type of data in the sense of what layers make sense to use. If you want to use non image data, then the variety of layer that would make sense to use is reduced.
Further general information:
In regards to performing classification/regression with non-image, non-sequence data, it is beneficial to use the "FullyConnected" and "ReLU" layers instead of layers like "Convolution2DLayer", "LSTM", "MaxPooling2D". Using the later layers will not make much sense since they all assume some spatially or temporal correlation on the data.
There is also functionality of creating custom layers to implement other custom functionalities based on your requirements. You can refer to the examples in the following documentation link:
  4 Comments

Sign in to comment.

More Answers (2)

Ruida Xie
Ruida Xie on 21 Sep 2020
Edited: Ruida Xie on 24 Sep 2020
Updates: Download Matlab 2020b version or higher version in future, you can use 'featureInputLayer' without any reshaping of you raw data. But I guess this won't influence the training process, just more convienient.

NGR MNFD
NGR MNFD on 4 Jul 2021
Hello . I hope you have a good day. I sent the article to your service. I implemented the coding part in the MATLAB software, but to implement my network, two lines of setlayers, training MATLAB 2014 give me an error. What other function do you think I should replace? Do you think the codes I wrote are correct?( I used gait-in-neurodegenerative-disease-database in physionet website.) Thanks a lot

Community Treasure Hunt

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

Start Hunting!