pypots.representation¶
pypots.representation.ts2vec¶
The package of the partially-observed time-series representation learning and vectorizing model TS2Vec.
Refer to the paper Zhihan Yue, Yujing Wang, Juanyong Duan, Tianmeng Yang, Congrui Huang, Yunhai Tong, Bixiong Xu. “TS2Vec: Towards Universal Representation of Time Series”. In AAAI 2022.
Notes
This implementation is inspired by the official one https://github.com/zhihanyue/ts2vec
- class pypots.representation.ts2vec.TS2Vec(n_steps, n_features, n_output_dims, d_hidden, n_layers, mask_mode='binomial', batch_size=32, epochs=100, patience=None, optimizer=<class 'pypots.optim.adam.Adam'>, num_workers=0, device=None, saving_path=None, model_saving_strategy='best', verbose=True)[source]¶
Bases:
BaseNNRepresentor
The PyTorch implementation of the TS2Vec model [2].
- Parameters:
n_steps (
int
) – The number of time steps in the time-series data sample.n_features (
int
) – The number of features in the time-series data sample.n_output_dims (
int
) – The number of output dimensions for the representation of the time-series data sample.d_hidden (
int
) – The number of hidden dimensions for the TS2VEC encoder.n_layers (
int
) – The number of layers for the TS2VEC encoder.mask_mode (
str
) – The mode for generating the mask for the TS2VEC encoder. It has to be one of [‘binomial’, ‘continuous’, ‘all_true’, ‘all_false’, ‘mask_last’].batch_size (
int
) – The batch size for training and evaluating the model.epochs (
int
) – The number of epochs for training the model.patience (
Optional
[int
]) – The patience for the early-stopping mechanism. Given a positive integer, the training process will be stopped when the model does not perform better after that number of epochs. Leaving it default as None will disable the early-stopping.optimizer (
Union
[Optimizer
,type
]) – The optimizer for model training. If not given, will use a default Adam optimizer.num_workers (
int
) – The number of subprocesses to use for data loading. 0 means data loading will be in the main process, i.e. there won’t be subprocesses.device (
Union
[str
,device
,list
,None
]) – The device for the model to run on. It can be a string, atorch.device
object, or a list of them. If not given, will try to use CUDA devices first (will use the default CUDA device if there are multiple), then CPUs, considering CUDA and CPU are so far the main devices for people to train ML models. If given a list of devices, e.g. [‘cuda:0’, ‘cuda:1’], or [torch.device(‘cuda:0’), torch.device(‘cuda:1’)] , the model will be parallely trained on the multiple devices (so far only support parallel training on CUDA devices). Other devices like Google TPU and Apple Silicon accelerator MPS may be added in the future.saving_path (
Optional
[str
]) – The path for automatically saving model checkpoints and tensorboard files (i.e. loss values recorded during training into a tensorboard file). Will not save if not given.model_saving_strategy (
Optional
[str
]) – The strategy to save model checkpoints. It has to be one of [None, “best”, “better”, “all”]. No model will be saved when it is set as None. The “best” strategy will only automatically save the best model after the training finished. The “better” strategy will automatically save the model during training whenever the model performs better than in previous epochs. The “all” strategy will save every model after each epoch training.verbose (
bool
) – Whether to print out the training logs during the training process.
- fit(train_set, val_set=None, file_type='hdf5')[source]¶
Train the representor on the given data.
- Parameters:
train_set (
Union
[dict
,str
]) – The dataset for model training, should be a dictionary including keys as ‘X’ and ‘y’, or a path string locating a data file. If it is a dict, X should be array-like with shape [n_samples, n_steps, n_features], which is time-series data for training, can contain missing values, and y should be array-like of shape [n_samples], which is representation labels of X. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include keys as ‘X’ and ‘y’.val_set (
Union
[dict
,str
,None
]) – The dataset for model validating, should be a dictionary including keys as ‘X’ and ‘y’, or a path string locating a data file. If it is a dict, X should be array-like with shape [n_samples, n_steps, n_features], which is time-series data for validating, can contain missing values, and y should be array-like of shape [n_samples], which is representation labels of X. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include keys as ‘X’ and ‘y’.file_type (
str
) – The type of the given file if train_set and val_set are path strings.
- Return type:
- predict(test_set, file_type='hdf5', mask=None, encoding_window=None, causal=False, sliding_length=None, sliding_padding=0)[source]¶
Make predictions for the input data with the trained model.
- Parameters:
test_set (
Union
[dict
,str
]) – The test dataset for model to process, should be a dictionary including keys as ‘X’, or a path string locating a data file supported by PyPOTS (e.g. h5 file). If it is a dict, X should be array-like with shape [n_samples, n_steps, n_features], which is the time-series data for processing. If it is a path string, the path should point to a data file, e.g. a h5 file, which contains key-value pairs like a dict, and it has to include ‘X’ key.file_type (
str
) – The type of the given file if test_set is a path string.
- Returns:
The dictionary containing the representation results as key ‘representation’ and latent variables if necessary.
- Return type:
result_dict
- load(path)¶
Load the saved model from a disk file.
Notes
If the training environment and the deploying/test environment use the same type of device (GPU/CPU), you can load the model directly with torch.load(model_path).
- represent(test_set, file_type='hdf5', mask=None, encoding_window=None, causal=False, sliding_length=None, sliding_padding=0)[source]¶
Vectorize the input data with the trained model.
- Parameters:
- Returns:
Classification results of the given samples.
- Return type:
array-like, shape [n_samples],
- save(saving_path, overwrite=False)¶
Save the model with current parameters to a disk file.
A
.pypots
extension will be appended to the filename if it does not already have one. Please note that such an extension is not necessary, but to indicate the saved model is from PyPOTS framework so people can distinguish.