DeepAR prediction confusion
@djarpin
I split my dataset to the training set and validation set. Each data point represented 1D intervals.
My prediction length was set 16 (I want to predict 16D from the start of the validation set, which is also end of training set)
The documentation states I need to provide a "start" and "target" field. I am using dynamic features so the documentation states I need to provide len(target)+16) items for that field.
Since I want to "predict" the beginning of the validation set, the required "target" values has to be from the training set. Why do I need to provide the training set again, when it was used initially to develop the model in the first place?
What is the role of providing "target" values for the purposes of predicting values. If it was linear regression and the model was already generated, then all I would need to provide is the 16 timestamps for what I want to predict and nothing more. Why does RNN models need data that was already used for training?
Question 2

The screenshot is from the official blog. It says mean of 50 predictions.
On the other hand the documentation states:
The configuration field is optional. configuration.num_samples sets the number of sample paths that the model generates to estimate the mean and quantiles.
Does that mean that the model, for the same given input data, produces a range of values (i.e. not deterministic) and configuration.num_samples indicates that the model is applied 50 times and the predicted result is the mean? (i.e. for each timestamp that a prediction is sought, 50 predictions are made for that timestamp and the mean is returned)
Sorry for bothering you and I want to thank you in advance. I'm hoping my questions would provide clarity to other prospective users of the service.
@pjebs - Note that the trained DeepAR model can be used to forecast time series that are not necessarily part of the training set. So during inference, one is given the "target" values of the time series to be forecast until the forecast start date (along with the features and start date) and then the trained model will predict/extends the given time series for required number of time steps. So, even if you want to predict only the time series in the training set, you still need to specify the target values of these time series until the forecast start date as well as other features.
For your second question: the model will produce configuration.num_samples values for each time step. But this is done in one go and the model is not applied configuration.num_samples times. The mean value of the prediction is the mean of these samples. Similarly quantiles are computed from these samples.
@rshyamsundar This is still not clear to me, if you can explain little bit more that would be great
Lets say way have time series and data point between T1 and Tn and we trained out model using the data up to Tm (T1 < Tm < Tn)
|<------------------------------------------->|<----------->|..................|
T1 Tm Tn unknown Tn+30
Now most of the examples I've seen uses Tm-Tn period to predict and validate with the target values of this range,
Question is what is the target value(s) in the request when we want to predict 30 data points beyond Tn
Thank you
@rasikas To predict 30 time points after Tn (i.e., from Tn+1 to Tn+30), pass all the target values until Tn (i.e., T1 to Tn) in the request. For getting these predictions, you can still query the model trained on the data from T1 up to Tm (you would still pass the target values from T1 to Tn in your inference request). However, in this case it is recommended to retrain the model on the whole data from T1 to Tn especially when n is much larger than m.
In a typical scenario, one reserves part of the data available (Tm+1 to Tn in your case) as test data for selecting the best model by tuning hyper-parameters. Once the best hyper-parameters are found using the test data, one retrains the model one more time on the whole data (T1 to Tn) using the best hyper-parameters. This model is then used to predict future unseen data (Tn+1 to Tn+30).
Hope this clarifies. Let me know if something is still not clear.
@rshyamsundar Thanks a lot for taking time to answer, and yes now I get it after your explanation, so now my questions are (hope you don't mind)
- Does that mean if we have data between 2015-2018 (daily data point), every time we want to predict first 30 days of 2019 (ex for the month of January 2019) we need to send all the daily values from 2015-2018 with the request? - this is huge amount of data in every single request,
- or -
Should we only send the
context_lengthamount of data to the past from end of the year 2018,
- Now when we need to predict the second 30 day slot in 2019 (roughly for the month Februrary), should we now send the mean values we obtained for the first 30 days in the previous request?
- or - should we still send 2015-2018 data
- or - should we still send 2015-2018 data and predict for 60 days instead of 30 days starting from 2019
- or - we retrain the model including first 30 days we already predicted and repeat step 1.
- If we always need to send the history data with the request, why the DeepAR model itself can keep that data and use it (based on the time paramters we send) instead of us having to send the history data as
targetfield in every single request
Thank you very much
@rasikas Here are more clarifications for the questions you have.
-
It depends on the data. If there is yearly seasonality that you want to capture then sending at least two years of data is helpful (both during training and prediction). On the other hand, recent past typically plays more important role than very old data in forecasting future values. In this case, you could evaluate different models trained with decreasing history length and then select the one that is suitable for your criteria. Note that history length is the total length of the time series you provide as a training target (i.e., all the target values just before forecast start time).
context_lengthshould not be confused with history length. DeepAR model internally takes the target values that occur before thecontext_lengthtime points as features (known as lags in auto-regressive models). So you should always provide much more thancontext_lengthdata points since context length is typically small (usually equal to prediction length). -
In this case you have two options (assuming that we are at the end of January 2019 and we have the true target values for the month of January 2019)
a. Retrain the model by including the January data in your training and then query the new model as usual (i.e., sending true targets until January and predicting for February).
b. You can still use the old model trained using the data until December but it might be less accurate. In this case, you send the true target values until January 2019 in the prediction request. Side note: since it was not clear in the question, I am addressing one more scenario: you have data until end of 2018 and you want to predict directly for February 2019. In this case your option is to predict for both January and February 2019 by training the model using the data until end of 2018.
-
The reason for keeping the interface for prediction requests this way is partly due to the more advanced capabilities of DeepAR model as explained below:
a. Forecasting new/unseen time series: You can query the trained DeepAR model to forecast time series that was not at all used during training. In this case one would have to send the past values of the target to the model to tell it what is the time series that needs to be forecast. As an example for this use-case, consider that you have trained a DeepAR model on hourly taxi-demand time series for some geographic locations. You can then use this model to forecast the demand for other new (but similar) geographic locations as and when the data for these locations gets available without retraining the model.
b. Rolling forecasts: Suppose you have a use-case where you have to forecast for the next 7 days. You already trained a model using the data until the end of 2018 and have generated forecasts for the first week of January. After one week passes, you want to forecast for the second week of January but you now have the target values for the first week of January which you want to make use of. In this case, DeepAR allows you to query the same model (trained using the data until end of 2018) to get the predictions for the second week and still use the new information that got available in the first week of January; here you just have to pass the target values until the end of first week of January in the prediction request to the old model.
This way you do not have to retrain the whole model every time the new data becomes available. This is especially useful in production setting where you retrain your models less frequently, e.g., once in a month or quarter and still want to use the new information that becomes available between two consecutive trainings runs.
@rshyamsundar Thanks a lot for your time answering my question
Rolling forecasts: Suppose you have a use-case where you have to forecast for the next 7 days. You already trained a model using the data until the end of 2018 and have generated forecasts for the first week of January. After one week passes, you want to forecast for the second week of January but you now have the target values for the first week of January which you want to make use of. In this case, DeepAR allows you to query the same model (trained using the data until end of 2018) to get the predictions for the second week and still use the new information that got available in the first week of January; here you just have to pass the target values until the end of first week of January in the prediction request to the old model.
This is what I couldn't undertstand before ,
So the target field in the request simply a certain amount of immidiate history data from the starting point which we need to predict?
So the
targetfield in therequestsimply a certain amount of immidiate history data from the starting point which we need to predict?
Exactly.
The way I think of it is like this:
- We create the model with as much data as we have.
- When we forecast, we provide as much or as little historical data (even if it was originally used to train the model, but it is preferred we satisfy the context_length worth of data). Somehow the inference process uses that data provided and returns the forecast data.
When we forecast, if we have the full range of data, should we provide it all or just the context_length worth of data?
Thanks @pjebs , yes context_length worth data makes sense, It was not clear in most existing examples that it shows how it predict future, existing example shows the prediction within the data set it originally had, this is where I had the confusion. Now it all clear thanks a lot
When we forecast, if we have the full range of data, should we provide it all or just the context_length worth of data?
@pjebs Provide all the data that is available in your prediction request (not just context_length worth of data). The model can use only those values that are provided in the request as lagged features. context_length is typically small and you should always send more data than context_length both for training and prediction.
For me it's no issue whether I provide full set of data or just context_length worth of data for inference purposes. My question is which provides a more accurate forecast? @rshyamsundar
@pjebs Sending full set of data during inference usually provides more accurate forecasts as the model uses more information in this case.
Provide all the data that is available in your prediction request (not just context_length worth of data)
That's where I have the problem with the model, if it is historical data, instead of sending a very bulky request (probably in many hundreds of megabytes) with every predict request, why can't we simply pass the target_start and target_end time slots instead of target data field
and modify how DeepAR works where it keeps the history data (in addition to trained model) where it can find the target values using the target_start and target_end time periods we send in the request to predict
Your suggesting that the model file should retain the originally trained data inside and provide a shortcut way of using that data instead of re-providing it during inference process?
Your suggesting that the model file should retain the originally trained data inside and provide a shortcut way of using that data instead of re-providing it during inference process?
Yes, because we anyway save data in S3, (i know it can use cache to make the requests faster) but just imagine the request payload size everytime if we want to send entire history worth of data to predict future dates, not only network data size, but also to serialize deserialize the data at both end of the communication, and the memory usage at client side
It doesn't need to be inside the model but somewhere accessible to it, it could be specified when creating the estimator, similar to output_path another with training_data_path which keeps the original time series data
estimator = sagemaker.estimator.Estimator(
sagemaker_session=sagemaker_session,
image_name=image_name,
role=role,
train_instance_count=1,
train_instance_type='ml.c4.xlarge',
base_job_name='DEMO-deepar',
output_path="s3://" + s3_output_path,
)
or something in the endpoint or combination of both
endpoint_name = sagemaker_session.endpoint_from_job(
job_name=job_name,
initial_instance_count=1,
instance_type='ml.m4.xlarge',
deployment_image=image_name,
role=role,
training_data_path="s3://" + s3_data_path,
)
@rasikas In this case you can use Amazon Sagemaker Batch Transform as mentioned in the DeepAR documentation: https://docs.aws.amazon.com/sagemaker/latest/dg/deepar-in-formats.html#deepar-batch
Batch Transform allows you to get inferences on the entire dataset that is stored in S3. In your case you provide training_data_path as the input location for the data to be transformed by the trained model. The inferences are saved in the S3 bucket you specify.
See here for information on how to use Sagemaker Batch Transform for getting inferences: https://docs.aws.amazon.com/sagemaker/latest/dg/ex1-batch-transform.html
Hope this helps.
thank you @rshyamsundar, I will check your links
Shall I close this issue?