amazon-sagemaker-examples icon indicating copy to clipboard operation
amazon-sagemaker-examples copied to clipboard

DeepAR prediction confusion

Open pjebs opened this issue 8 years ago • 26 comments

@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?

pjebs avatar Aug 28 '18 04:08 pjebs

Question 2

screen shot 2018-08-28 at 3 21 30 pm

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)

pjebs avatar Aug 28 '18 05:08 pjebs

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 avatar Aug 28 '18 22:08 pjebs

@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 avatar Sep 14 '18 12:09 rshyamsundar

@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 avatar Oct 19 '18 05:10 rasikas

@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 avatar Oct 19 '18 20:10 rshyamsundar

@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)

  1. 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_length amount of data to the past from end of the year 2018,
  1. 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.
  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 target field in every single request

Thank you very much

rasikas avatar Oct 19 '18 21:10 rasikas

@rasikas Here are more clarifications for the questions you have.

  1. 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_length should not be confused with history length. DeepAR model internally takes the target values that occur before the context_length time points as features (known as lags in auto-regressive models). So you should always provide much more than context_length data points since context length is typically small (usually equal to prediction length).

  2. 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.

  3. 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 avatar Oct 23 '18 01:10 rshyamsundar

@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?

rasikas avatar Oct 23 '18 01:10 rasikas

So the target field in the request simply a certain amount of immidiate history data from the starting point which we need to predict?

Exactly.

rshyamsundar avatar Oct 23 '18 01:10 rshyamsundar

The way I think of it is like this:

  1. We create the model with as much data as we have.
  2. 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?

pjebs avatar Oct 23 '18 01:10 pjebs

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

rasikas avatar Oct 23 '18 01:10 rasikas

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.

rshyamsundar avatar Oct 23 '18 01:10 rshyamsundar

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 avatar Oct 23 '18 02:10 pjebs

@pjebs Sending full set of data during inference usually provides more accurate forecasts as the model uses more information in this case.

rshyamsundar avatar Oct 23 '18 02:10 rshyamsundar

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

rasikas avatar Oct 23 '18 02:10 rasikas

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?

pjebs avatar Oct 23 '18 02:10 pjebs

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 avatar Oct 23 '18 02:10 rasikas

@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.

rshyamsundar avatar Oct 23 '18 12:10 rshyamsundar

thank you @rshyamsundar, I will check your links

rasikas avatar Oct 23 '18 22:10 rasikas

Shall I close this issue?

pjebs avatar Oct 23 '18 23:10 pjebs