User Guide Tutorial 04: Prediction¶
This tutorial shows how to use TemporAI prediction plugins.
All prediction plugins¶
To see all the relevant plugins:
[ ]:
from tempor import plugin_loader
from rich.pretty import pprint
all_prediction_plugins = plugin_loader.list()["prediction"]
pprint(all_prediction_plugins, indent_guides=False)
{ 'one_off': { 'classification': ['cde_classifier', 'ode_classifier', 'nn_classifier', 'laplace_ode_classifier'], 'regression': ['laplace_ode_regressor', 'nn_regressor', 'ode_regressor', 'cde_regressor'] }, 'temporal': {'classification': ['seq2seq_classifier'], 'regression': ['seq2seq_regressor']} }
Now also load data source(s) we will use:
[ ]:
SineDataSource = plugin_loader.get_class("prediction.one_off.sine", plugin_type="datasource")
DummyTemporalPredictionDataSource = plugin_loader.get_class(
"prediction.temporal.dummy_prediction", plugin_type="datasource"
)
Using a one-off prediction plugin¶
One-off prediction is the task of predicting a single value for each sample (may be classification or regression).
[ ]:
from tempor import plugin_loader
dataset = SineDataSource(random_state=42).load()
print(dataset)
model = plugin_loader.get("prediction.one_off.classification.nn_classifier", n_iter=50)
print(model)
OneOffPredictionDataset(
time_series=TimeSeriesSamples([100, *, 5]),
static=StaticSamples([100, 4]),
predictive=OneOffPredictionTaskData(targets=StaticSamples([100, 1]))
)
NeuralNetClassifier(
name='nn_classifier',
category='prediction.one_off.classification',
plugin_type='method',
params={
'n_static_units_hidden': 100,
'n_static_layers_hidden': 2,
'n_temporal_units_hidden': 102,
'n_temporal_layers_hidden': 2,
'n_iter': 50,
'mode': 'RNN',
'n_iter_print': 10,
'batch_size': 100,
'lr': 0.001,
'weight_decay': 0.001,
'window_size': 1,
'device': None,
'dataloader_sampler': None,
'dropout': 0,
'nonlin': 'relu',
'random_state': 0,
'clipping_value': 1,
'patience': 20,
'train_ratio': 0.8
}
)
[ ]:
# Targets:
dataset.predictive.targets
StaticSamples with data:
| 0 | |
|---|---|
| sample_idx | |
| 0 | 0 |
| 1 | 1 |
| 2 | 0 |
| 3 | 0 |
| 4 | 1 |
| ... | ... |
| 95 | 1 |
| 96 | 1 |
| 97 | 1 |
| 98 | 0 |
| 99 | 0 |
100 rows × 1 columns
[ ]:
# Train.
model.fit(dataset)
2023-11-16 22:18:45 | INFO | tempor.models.ts_model:_train:388 | Epoch:0| train loss: 0.68824702501297, validation loss: 0.6883962154388428
2023-11-16 22:18:45 | INFO | tempor.models.ts_model:_train:388 | Epoch:10| train loss: 0.6999298930168152, validation loss: 0.6879696249961853
2023-11-16 22:18:45 | INFO | tempor.models.ts_model:_train:388 | Epoch:20| train loss: 0.6818333268165588, validation loss: 0.6867876052856445
2023-11-16 22:18:45 | INFO | tempor.models.ts_model:_train:388 | Epoch:30| train loss: 0.694374680519104, validation loss: 0.6851812601089478
2023-11-16 22:18:45 | INFO | tempor.models.ts_model:_train:388 | Epoch:40| train loss: 0.6282318234443665, validation loss: 0.6936542391777039
NeuralNetClassifier(
name='nn_classifier',
category='prediction.one_off.classification',
plugin_type='method',
params={
'n_static_units_hidden': 100,
'n_static_layers_hidden': 2,
'n_temporal_units_hidden': 102,
'n_temporal_layers_hidden': 2,
'n_iter': 50,
'mode': 'RNN',
'n_iter_print': 10,
'batch_size': 100,
'lr': 0.001,
'weight_decay': 0.001,
'window_size': 1,
'device': None,
'dataloader_sampler': None,
'dropout': 0,
'nonlin': 'relu',
'random_state': 0,
'clipping_value': 1,
'patience': 20,
'train_ratio': 0.8
}
)
[ ]:
# Predict:
model.predict(dataset)
StaticSamples with data:
| feat_0 | |
|---|---|
| sample_idx | |
| 0 | 0.0 |
| 1 | 1.0 |
| 2 | 1.0 |
| 3 | 0.0 |
| 4 | 0.0 |
| ... | ... |
| 95 | 1.0 |
| 96 | 0.0 |
| 97 | 1.0 |
| 98 | 0.0 |
| 99 | 1.0 |
100 rows × 1 columns
Using a temporal prediction plugin¶
Temporal prediction is the task of predicting a time series for each sample (may be classification or regression).
[ ]:
from tempor import plugin_loader
dataset = DummyTemporalPredictionDataSource(random_state=42, temporal_covariates_missing_prob=0.0).load()
print(dataset)
model = plugin_loader.get("prediction.temporal.regression.seq2seq_regressor", epochs=10)
print(model)
TemporalPredictionDataset(
time_series=TimeSeriesSamples([100, *, 5]),
static=StaticSamples([100, 3]),
predictive=TemporalPredictionTaskData(
targets=TimeSeriesSamples([100, *, 2])
)
)
Seq2seqRegressor(
name='seq2seq_regressor',
category='prediction.temporal.regression',
plugin_type='method',
params={
'encoder_rnn_type': 'LSTM',
'encoder_hidden_size': 100,
'encoder_num_layers': 1,
'encoder_bias': True,
'encoder_dropout': 0.0,
'encoder_bidirectional': False,
'encoder_nonlinearity': None,
'encoder_proj_size': None,
'decoder_rnn_type': 'LSTM',
'decoder_hidden_size': 100,
'decoder_num_layers': 1,
'decoder_bias': True,
'decoder_dropout': 0.0,
'decoder_bidirectional': False,
'decoder_nonlinearity': None,
'decoder_proj_size': None,
'adapter_hidden_dims': [50],
'adapter_out_activation': 'Tanh',
'predictor_hidden_dims': [],
'predictor_out_activation': None,
'max_len': None,
'optimizer_str': 'Adam',
'optimizer_kwargs': {'lr': 0.01, 'weight_decay': 1e-05},
'batch_size': 32,
'epochs': 10,
'padding_indicator': -999.0
}
)
[ ]:
# Targets:
dataset.predictive.targets
TimeSeriesSamples with data:
| 0 | 1 | ||
|---|---|---|---|
| sample_idx | time_idx | ||
| 0 | 0 | -3.110475 | -3.566948 |
| 1 | 1.528495 | -0.653673 | |
| 2 | 2.275307 | -0.695371 | |
| 3 | 4.844060 | 3.469371 | |
| 4 | 4.420301 | 5.147500 | |
| ... | ... | ... | ... |
| 99 | 7 | 5.994185 | 6.225290 |
| 8 | 10.913662 | 5.346697 | |
| 9 | 9.558824 | 7.585175 | |
| 10 | 10.194430 | 5.795619 | |
| 11 | 13.774189 | 8.457336 |
1573 rows × 2 columns
[ ]:
# Train.
model.fit(dataset);
Preparing data for decoder training...
Preparing data for decoder training DONE.
=== Training stage: 1. Train encoder ===
Epoch: 0, Loss: 68.64124755859375
Epoch: 1, Loss: 35.376345291137696
Epoch: 2, Loss: 21.269977149963378
Epoch: 3, Loss: 17.418056030273437
Epoch: 4, Loss: 13.851958045959472
Epoch: 5, Loss: 8.335217628479004
Epoch: 6, Loss: 6.385376873016358
Epoch: 7, Loss: 5.1125048828125
Epoch: 8, Loss: 4.690582141876221
Epoch: 9, Loss: 4.15880220413208
=== Training stage: 2. Train decoder ===
Epoch: 0, Loss: 29.110622051453316
Epoch: 1, Loss: 4.4235033275153635
Epoch: 2, Loss: 3.954428958957566
Epoch: 3, Loss: 3.8989672662446226
Epoch: 4, Loss: 3.8317033024119103
Epoch: 5, Loss: 3.8341090269664964
Epoch: 6, Loss: 3.717077842797864
Epoch: 7, Loss: 3.7477998285170564
Epoch: 8, Loss: 3.859872080239
Epoch: 9, Loss: 3.8217773019903305
[ ]:
# Predict:
model.predict(dataset, n_future_steps=10)
TimeSeriesSamples with data:
| 0 | 1 | ||
|---|---|---|---|
| sample_idx | time_idx | ||
| 0 | 11 | 10.618200 | 8.973257 |
| 12 | 10.728621 | 9.108867 | |
| 13 | 10.735380 | 9.118745 | |
| 14 | 10.728010 | 9.093669 | |
| 15 | 10.738855 | 9.097306 | |
| ... | ... | ... | ... |
| 99 | 17 | 12.021331 | 10.417010 |
| 18 | 12.091549 | 10.484824 | |
| 19 | 12.144796 | 10.523143 | |
| 20 | 12.133350 | 10.510198 | |
| 21 | 12.097664 | 10.484853 |
1000 rows × 2 columns