Clear Filters
Clear Filters

How to apply a 2D matrix input to a trainNetwork?

22 views (last 30 days)
I have the following piece of code for training a network:
layers = [
maxEpochs = 100;
options = trainingOptions('adam', ...
'ExecutionEnvironment','gpu', ...
'SequenceLength','longest', ...
'Shuffle','never', ...
net = trainNetwork(xt,yt,layers,options);
screenshot of a sample xt and yt is given below:
xt and yt
Basically each yt corresponds to a 3 by 5 matrix in xt. where 3 is the number of rows in the sequence and 5 is the number of features. Basically xt contains a sequence of stock prices - the 4 prices and volume, after scaling. yt corresponds to future close price.
I want to train the network with this. And I have tried various combinations, reshaping, converting it to cells etc. But its one error or the other.
Does someone know if its possible to do what I am trying to do? I am using version 2020b.
The current error is:
Error using trainNetwork (line 183)
The training sequences are of feature dimension 4 5 but the input layer expects sequences of feature dimension 5.

Answers (1)

Joss Knight
Joss Knight on 8 Sep 2023
Typically sequence data is passed in as a cell array. In each cell you would be passing one sequence. If you want to pass a single numeric array then I think all you need to do is permute dims 1 and 2 - you have to put the features down the columns, i.e. each column is an observation. Arrangement is C-by-B-by-T.
Matt J
Matt J on 8 Sep 2023
Edited: Matt J on 8 Sep 2023
Arrangement is C-by-B-by-T.
I have to agree with the OP. I don't see anywhere in the documentation that indicates that.
It is also a bit puzzling to me that trainNetwork cannot accept formatted dlarrays as input. That would let the user decide which dimensions are to be interpreted as S, C, B,T etc... and save much confusion.
Joss Knight
Joss Knight on 8 Sep 2023
Glad to hear an advocate for dlarray format labels!!
No, DAGNetwork APIs do not interact with dlarrays. They are for autodiff. Prepare yourself for upcoming R2023b to see that solution.
My understanding is that the intention is that you pass in a numeric array if you want to process a single sequence for inference, otherwise it's really intended that you use cell arrays. The documentation, in a noble attempt to provide information about every single possible kind of input data, says the data should be a
"c-by-s matrix, where c is the number of features of the sequence and s is the sequence length."
While the format of the responses for sequence-to-vector regression should be a
"Numeric row vector"
These are then put into cell arrays, one per observation.
Also, now that I look at it, the flatten layer doesn't seem right, and you'd need to unfold the sequence to pass through the fullyConnectedLayer. But shouldn't the lstmLayer be in OutputMode 'last' for that to work?
I guess I'll just defer to someone who knows more about sequence networks and stop interfering...

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!