MATLAB Answers

Multivariate LSTM mini-batch size error

17 views (last 30 days)
Harry Bannister
Harry Bannister on 18 Apr 2021
Edited: Niccolò Dal Santo on 26 Jul 2021
Coding a multivariate LSTM for power demand regression/forecasting. Please see attached code.
After training the LSTM on a N x 1 cell array (26 x 1 cells) training set of EMD (of the demand data) and some other non-0 change information (renewables, ccgt, etc), trying to predict using the trained LSTM throws the following error:
Error using DAGNetwork/predictRNN>iAssertInitialStateIsValidForPredict (line 67)
Incorrect network state. The network expects mini-batches size of 45, but was
passed a mini-batch of size 1.
Error in DAGNetwork/predictRNN (line 9)
iAssertInitialStateIsValidForPredict(statefulLayers, dispatcher.MiniBatchSize)
Error in DAGNetwork/predictAndUpdateState (line 130)
[Y, finalState, predictNetwork] = this.predictRNN(X, dispatcher, ...
Error in SeriesNetwork/predictAndUpdateState (line 396)
[this.UnderlyingDAGNetwork, Y] =
this.UnderlyingDAGNetwork.predictAndUpdateState(X, varargin{:});
Error in DPM_V2_1 (line 124)
[net,YPred(i,:)] =
I have attempted to change the miniBatchSize in the training options, however I don't think this is the solution, and didn't get any change in results.
Altering the sequenceLength to longest also didn't get any results.
Any help would be appreciated, thanks
  1 Comment
Shashank Gupta
Shashank Gupta on 22 Apr 2021
Hi Harry,
I ran the attached file as it is and I am not able to get the same error as yours. Although looking at the error, it seems like the numFeatures and mini batch size is mismatched. Try looking at this direction.

Sign in to comment.

Answers (1)

Niccolò Dal Santo
Niccolò Dal Santo on 26 Jul 2021
Edited: Niccolò Dal Santo on 26 Jul 2021
Hi Harry,
I was able to reproduce the error you report, which is caused by the fact that you do not reset the state of the network before the for-loop. You can add the following line before the for-loop which invokes the predictAndUpdateState method:
net = resetState(net);
Notice that for training the network in R2021a release I had to changed the definition of YTrain to
YTrain = cell2mat(YTrain(2:end,:));
The format of the inputs to trainNetwork for training on sequences from cell arrays are
  • The input X a cell array of size N x 1, where each element is one time series of size C x S, C being the number of features and S being the number of timesteps. Each element of the cell array is an observation.
  • The input Y a matrix of targets or a cell array of size N x 1, where each element is a matrix of size R x S, R being the number of output responses and S being the number of timestepsv(which must be equal to the corresponding input observation).
Please see the documentation page of trainNetwork function for further details:
I hope this helps.

Community Treasure Hunt

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

Start Hunting!