from gluonts.torch.model.deepar.lightning_module import DeepARLightningModule
if not TRAINING:
best_checkpoint_path = os.path.join(checkpoint_path, 'epoch=44-step=4500.ckpt')
loaded_module = DeepARLightningModule.load_from_checkpoint(best_checkpoint_path)
what next how create predictor? for making prediction based on hparams
To create a predictor using the DeepAREstimator and make predictions based on your existing hyperparameters and model, follow these steps:
Step 1: Load Necessary Packages
Ensure you have imported the necessary packages:
python
import os
from gluonts.torch.model.deepar import DeepAREstimator
from gluonts.torch.model.deepar.lightning_module import DeepARLightningModule
from gluonts.torch.model.predictor import PyTorchPredictor
from gluonts.dataset.common import ListDataset
Step 2: Define Your Estimator
It appears you have already defined your estimator with hyperparameters. Just ensure the code is correct:
You've already loaded your trained DeepAR model's checkpoint:
python
if not TRAINING:
best_checkpoint_path = os.path.join(checkpoint_path, 'epoch=44-step=4500.ckpt')
loaded_module = DeepARLightningModule.load_from_checkpoint(best_checkpoint_path)
Step 4: Create Predictor
You can now create a predictor using the loaded model:
Assuming you have your data in a ListDataset or any other appropriate data format required by GluonTS, you prepare your data:
python
# Example data
data = ListDataset(
[{
"start": "2023-01-01",
"target": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Your time series data here
}],
freq=time_freq
)
Step 6: Make Predictions
You can now use the predictor to make forecasts:
python
for forecast in predictor.predict(data):
print(forecast.mean) # You can also access other attributes of the forecast
Full Code Example
Here’s everything combined into one example:
python
import os
from gluonts.torch.model.deepar import DeepAREstimator
from gluonts.torch.model.deepar.lightning_module import DeepARLightningModule
from gluonts.torch.model.predictor import PyTorchPredictor
from gluonts.dataset.common import ListDataset
# Define hparams and other required variables
time_freq = "D" # for example, daily frequency
prediction_length = 10
context_length = 20
lags_sequence = [1, 2]
time_features = [] # Define appropriate time features as needed
batch_size = 32
trainer_kwargs = {} # Define any required trainer kwargs
# Define your estimator
estimator = DeepAREstimator(
freq=time_freq,
prediction_length=prediction_length,
context_length=context_length,
patience=10,
num_layers=2,
hidden_size=40,
lr=1e-4,
weight_decay=1e-2,
lags_seq=lags_sequence,
embedding_dimension=[2],
time_features=time_features,
num_batches_per_epoch=100,
num_feat_static_cat=1,
cardinality=[2],
batch_size=batch_size,
trainer_kwargs=trainer_kwargs,
)
# Load your trained model checkpoint
if not TRAINING:
best_checkpoint_path = os.path.join(checkpoint_path, 'epoch=44-step=4500.ckpt')
loaded_module = DeepARLightningModule.load_from_checkpoint(best_checkpoint_path)
# Create predictor
predictor = PyTorchPredictor.from_hparams(
loaded_module.hparams,
module=loaded_module,
prediction_length=prediction_length,
freq=time_freq
)
# Prepare your data
data = ListDataset(
[{
"start": "2023-01-01",
"target": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Your time series data here
}],
freq=time_freq
)
# Make predictions
for forecast in predictor.predict(data):
print(forecast.mean)
That's it! You can now use this to make predictions on your time series data. Ensure that all dependent libraries are correctly installed and imported as needed.