The dofference of predict() and PredictAndUpdateState()

Hi All,
I found the following two ways of predicting performs differently:
(1).
for i=1:size(XTest,2)
[trainedNet,YTest(:,i)]=PredictAndUpdateState(trainedNet,XTest(:,i));
end
(2).
for i=1:size(XTest,2)
[YTest(:,i),state]=predict(trainedNet,XTest(:,i));
trainedNet.State=state;
end
I wonder that what is the reseason for this phenomenon? Or, what is the difference between the ways of predict() and PredictAndUpdateState() to update networks?
Any help will be appreciated!

Answers (1)

The "predict" function in MATLAB predicts the responses of a linear regression model. For example, in this case:
[YTest(:,i),state]=predict(trainedNet,XTest(:,i));
The "predict" function returns the predicted response values of the model "trainedNet" for the points in "XTest(:, i)".
Now, the "predictAndUpdateState" function predicts responses using a trained recurrent neural network and updates the network state. In this case:
[trainedNet,YTest(:,i)]=predictAndUpdateState(trainedNet,XTest(:,i));
The "predictAndUpdateState" function predicts responses for data in "XTest(:, i)" using the trained recurrent neural network "trainedNet" and updates the network state.
Hence, the major difference between these two functions is that "predict" only forecasts the state, while "predictAndUpdateState" both forecasts and refines the state estimate with new data. Use "predict" when you need a forecast without new data, and "predictAndUpdateState" when you want to immediately refine your prediction with a new measurement.
Moreover, "predictAndUpdateState" is not recommended anymore. Instead, use the "predict" function and utilize the state output to update the "State" property of the neural network.
You can refer the following MathWorks documenation for more information:

4 Comments

Hi Animesh,
Thank you for answering my question patiently and your convincing answer.
But I also find a phenomenon as the following: My network is LSTM.
(1). If predict as this case:
YTest=predict(trainedNet,XTest);
The performance will be very good.
(2) If predict as this case:
for i=size(XTest,2)
YTest(:,i)=predict(trainedNet,XTest(:,i));
end
The performance will not so good.
If predict() does not update the state of the network, why this happen? I think the results should be the same.
Many thanks!
By "performance" here, do we mean "execution time" or "model accuracy"?
The performance here means "model accuracy".

Sign in to comment.

Categories

Find more on Deep Learning Toolbox in Help Center and File Exchange

Products

Release

R2024a

Asked:

on 22 Aug 2024

Commented:

on 23 Aug 2024

Community Treasure Hunt

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

Start Hunting!