Model API Cheatsheet¶
pypots.base¶
The base (abstract) classes for models in PyPOTS.
- class pypots.base.BaseModel(device=None, enable_amp=False, saving_path=None, model_saving_strategy='best', verbose=True)[source]¶
Bases:
ABC
The base model class for all model implementations.
- Parameters:
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.enable_amp (
bool
) – Whether to enable automatic mixed precision (AMP), default as False. If the implemented model is based on LLMs that need large-scale operation and AMP, please set it as True.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.
- Attributes:
model (object, default = None) – The underlying model or algorithm to finish the task.
summary_writer (None or torch.utils.tensorboard.SummaryWriter, default = None,) – The event writer to save training logs. Default as None. It only works when parameter tb_file_saving_path is given, otherwise the training events won’t be saved.
It is designed as being set up while initializing the model because it’s created to 1). help visualize the model’s training procedure (during training not after) and 2). assist users to optimize the model’s hyperparameters. If only setting it up after training with a function like setter(), it cannot achieve the 1st purpose.
- save(saving_path, overwrite=False)[source]¶
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.
- load(path)[source]¶
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).
- abstract fit(train_set, val_set=None, file_type='hdf5')[source]¶
Train the classifier on the given data.
- Parameters:
train_set (
Union
[dict
,str
]) – The dataset for model training, 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 time-series data for training, can contain missing values, and y should be array-like of shape [n_samples], which is classification 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’, 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 time-series data for validating, can contain missing values, and y should be array-like of shape [n_samples], which is classification 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:
- abstract predict(test_set, file_type='hdf5')[source]¶
Make predictions for the input data with the trained model.
- Parameters:
test_set (
Union
[dict
,str
]) – The dataset for model validating, 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 time-series data for validating, can contain missing values, and y should be array-like of shape [n_samples], which is classification 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 test_set is a path string.
- Returns:
Prediction results in a Python Dictionary for the given samples. It should be a dictionary including keys as ‘imputation’, ‘classification’, ‘clustering’, and ‘forecasting’. For sure, only the keys that relevant tasks are supported by the model will be returned.
- Return type:
result_dict
- class pypots.base.BaseNNModel(training_loss, validation_metric, batch_size, epochs, patience=None, num_workers=0, device=None, enable_amp=False, saving_path=None, model_saving_strategy='best', verbose=True)[source]¶
Bases:
BaseModel
The abstract class for all neural-network models.
- Parameters:
batch_size (
int
) – Size of the batch input into the model for one step.epochs (
int
) – Training epochs, i.e. the maximum rounds of the model to be trained with.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.training_loss (
Union
[Criterion
,type
]) – The customized loss function designed by users for training the model. If not given, the model will be trained with its own loss defined in its paper and fixed in the implementation.validation_metric (
Union
[Criterion
,type
]) – The customized metric function designed by users for validating the model. If not given, the model’s training loss will be used as the validation metric to select the best model.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.enable_amp (
bool
) – Whether to enable automatic mixed precision (AMP), default as False. If the implemented model is based on LLMs that need large-scale operation and AMP, please set it as True.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.
- Attributes:
best_model_dict (dict, default = None,) – A dictionary contains the trained model that achieves the best performance according to the loss defined, i.e. the lowest loss.
best_loss (float, default = inf,) – The criteria to judge whether the model’s performance is the best so far. Usually the lower, the better.
best_epoch (int, default = -1,) – The epoch number when the best loss is got.
Notes
Optimizers are necessary for training deep-learning neural networks, but we don’t put a parameter
optimizer
here because some models (e.g. GANs) need more than one optimizer (e.g. one for generator, one for discriminator), andoptimizer
is ambiguous for them. Therefore, we leave optimizers as parameters for concrete model implementations, and you can pass any number of optimizers to your model when implementing it,pypots.clustering.crli.CRLI
for example.- abstract fit(train_set, val_set=None, file_type='hdf5')[source]¶
Train the classifier on the given data.
- Parameters:
train_set (
Union
[dict
,str
]) – The dataset for model training, 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 time-series data for training, can contain missing values, and y should be array-like of shape [n_samples], which is classification 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’, 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 time-series data for validating, can contain missing values, and y should be array-like of shape [n_samples], which is classification 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:
- abstract predict(test_set, file_type='hdf5')[source]¶
Make predictions for the input data with the trained model.
- Parameters:
test_set (
Union
[dict
,str
]) – The dataset for model validating, 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 time-series data for validating, can contain missing values, and y should be array-like of shape [n_samples], which is classification 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 test_set is a path string.
- Returns:
Prediction results in a Python Dictionary for the given samples. It should be a dictionary including keys as ‘imputation’, ‘classification’, ‘clustering’, and ‘forecasting’. For sure, only the keys that relevant tasks are supported by the model will be returned.
- 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).
- 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.
pypots.imputation.base¶
The base class for PyPOTS imputation models.
- class pypots.imputation.base.BaseImputer(device=None, enable_amp=False, saving_path=None, model_saving_strategy='best', verbose=True)[source]¶
Bases:
BaseModel
Abstract class for all imputation models.
- Parameters:
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.enable_amp (
bool
) – Whether to enable automatic mixed precision (AMP), default as False. If the implemented model is based on LLMs that need large-scale operation and AMP, please set it as True.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.
- abstract fit(train_set, val_set=None, file_type='hdf5')[source]¶
Train the imputer on the given data.
- Parameters:
train_set (
Union
[dict
,str
]) – The dataset for model training, should be a dictionary including the key ‘X’, 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. 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 the key ‘X’.val_set (
Union
[dict
,str
,None
]) – The dataset for model validating, should be a dictionary including the key ‘X’, 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. 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 the key ‘X’.file_type (
str
) – The type of the given file if train_set and val_set are path strings.
- Return type:
- abstract predict(test_set, file_type='hdf5', **kwargs)[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 imputation results as key ‘imputation’ and latent variables if necessary.
- Return type:
result_dict
- impute(test_set, file_type='hdf5', **kwargs)[source]¶
Impute missing values in the given data with the trained model.
- Parameters:
- Returns:
Imputation results of the given data samples.
- Return type:
results
- 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).
- 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.
- class pypots.imputation.base.BaseNNImputer(training_loss, validation_metric, batch_size, epochs, patience=None, num_workers=0, device=None, enable_amp=False, saving_path=None, model_saving_strategy='best', verbose=True)[source]¶
Bases:
BaseNNModel
The abstract class for all neural-network imputation models in PyPOTS.
- Parameters:
batch_size (
int
) – Size of the batch input into the model for one step.epochs (
int
) – Training epochs, i.e. the maximum rounds of the model to be trained with.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.training_loss (
Union
[Criterion
,type
]) – The customized loss function designed by users for training the model. If not given, will use the default loss as claimed in the original paper.validation_metric (
Union
[Criterion
,type
]) – The customized metric function designed by users for validating the model. If not given, will use the default MSE metric.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.enable_amp (
bool
) – Whether to enable automatic mixed precision (AMP), default as False. If the implemented model is based on LLMs that need large-scale operation and AMP, please set it as True.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.
Notes
Optimizers are necessary for training deep-learning neural networks, but we don’t put a parameter
optimizer
here because some models (e.g. GANs) need more than one optimizer (e.g. one for generator, one for discriminator), andoptimizer
is ambiguous for them. Therefore, we leave optimizers as parameters for concrete model implementations, and you can pass any number of optimizers to your model when implementing it,pypots.clustering.crli.CRLI
for example.- abstract fit(train_set, val_set=None, file_type='hdf5')[source]¶
Train the imputer on the given data.
- Parameters:
train_set (
Union
[dict
,str
]) – The dataset for model training, should be a dictionary including the key ‘X’, 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. 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 the key ‘X’.val_set (
Union
[dict
,str
,None
]) – The dataset for model validating, should be a dictionary including the key ‘X’, 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. 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 the key ‘X’.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', **kwargs)[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 imputation results as key ‘imputation’ and latent variables if necessary.
- Return type:
result_dict
- impute(test_set, file_type='hdf5', **kwargs)[source]¶
Impute missing values in the given data with the trained model.
- Parameters:
- Returns:
Imputation results of the given data samples.
- Return type:
results
- 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).
- 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.
pypots.classification.base¶
The base classes for PyPOTS classification models.
- class pypots.classification.base.BaseClassifier(n_classes, device=None, enable_amp=False, saving_path=None, model_saving_strategy='best', verbose=True)[source]¶
Bases:
BaseModel
The abstract class for all PyPOTS classification models.
- Parameters:
n_classes (
int
) – The number of classes in the classification task.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.
- abstract fit(train_set, val_set=None, file_type='hdf5')[source]¶
Train the classifier 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 classification 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 classification 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:
- abstract predict(test_set, file_type='hdf5', **kwargs)[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 classification results as key ‘classification’ and latent variables if necessary.
- Return type:
result_dict
- predict_proba(test_set, file_type='hdf5', **kwargs)[source]¶
Predict the classification probabilities of the input data with the trained model.
- Parameters:
- Returns:
Classification probabilities of the given samples.
- Return type:
array-like, shape [n_samples],
- classify(test_set, file_type='hdf5', **kwargs)[source]¶
Classify the input data with the trained model.
- Parameters:
- Returns:
Classification results of the given samples.
- Return type:
results
- 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).
- 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.
- class pypots.classification.base.BaseNNClassifier(n_classes, training_loss, validation_metric, batch_size, epochs, patience=None, num_workers=0, device=None, enable_amp=False, saving_path=None, model_saving_strategy='best', verbose=True)[source]¶
Bases:
BaseNNModel
The abstract class for all neural-network classification models in PyPOTS.
- Parameters:
n_classes (
int
) – The number of classes in the classification task.batch_size (
int
) – Size of the batch input into the model for one step.epochs (
int
) – Training epochs, i.e. the maximum rounds of the model to be trained with.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.training_loss (
Union
[Criterion
,type
]) – The customized loss function designed by users for training the model. If not given, will use the default loss as claimed in the original paper.validation_metric (
Union
[Criterion
,type
]) – The customized metric function designed by users for validating the model. If not given, will use the default loss from the original paper as the metric.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.enable_amp (
bool
) – Whether to enable automatic mixed precision (AMP), default as False. If the implemented model is based on LLMs that need large-scale operation and AMP, please set it as True.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.
Notes
Optimizers are necessary for training deep-learning neural networks, but we don’t put a parameter
optimizer
here because some models (e.g. GANs) need more than one optimizer (e.g. one for generator, one for discriminator), andoptimizer
is ambiguous for them. Therefore, we leave optimizers as parameters for concrete model implementations, and you can pass any number of optimizers to your model when implementing it,pypots.clustering.crli.CRLI
for example.- fit(train_set, val_set=None, file_type='hdf5')[source]¶
Train the classifier 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 classification 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 classification 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', **kwargs)[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 classification results as key ‘classification’ and latent variables if necessary.
- Return type:
result_dict
- predict_proba(test_set, file_type='hdf5', **kwargs)[source]¶
Predict the classification probabilities of the input data with the trained model.
- Parameters:
- Returns:
Classification probabilities of the given samples.
- Return type:
results
- classify(test_set, file_type='hdf5', **kwargs)[source]¶
Classify the input data with the trained model.
- Parameters:
- Returns:
Classification results of the given samples.
- Return type:
results
- 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).
- 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.
pypots.forecasting.base¶
The base classes for PyPOTS forecasting models.
- class pypots.forecasting.base.BaseForecaster(device=None, enable_amp=False, saving_path=None, model_saving_strategy='best', verbose=True)[source]¶
Bases:
BaseModel
Abstract class for all forecasting models.
- Parameters:
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.enable_amp (
bool
) – Whether to enable automatic mixed precision (AMP), default as False. If the implemented model is based on LLMs that need large-scale operation and AMP, please set it as True.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.
- abstract fit(train_set, val_set=None, file_type='hdf5')[source]¶
Train the classifier on the given data.
- Parameters:
train_set (
Union
[dict
,str
]) – The dataset for model training, should be a dictionary including the key ‘X’, 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. 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 the key ‘X’.val_set (
Union
[dict
,str
,None
]) – The dataset for model validating, should be a dictionary including the key ‘X’, 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 validation, can contain missing values. 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 the key ‘X’.file_type (
str
) – The type of the given file if train_set and val_set are path strings.
- Return type:
- abstract predict(test_set, file_type='hdf5', **kwargs)[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 forecasting results as key ‘forecasting’ and latent variables if necessary.
- Return type:
result_dict
- forecast(test_set, file_type='hdf5', **kwargs)[source]¶
Forecast the future the input with the trained model.
- Parameters:
- Returns:
Forecasting results.
- Return type:
array-like, shape [n_samples, n_pred_steps, n_features],
- 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).
- 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.
- class pypots.forecasting.base.BaseNNForecaster(training_loss, validation_metric, batch_size, epochs, patience=None, num_workers=0, device=None, enable_amp=False, saving_path=None, model_saving_strategy='best', verbose=True)[source]¶
Bases:
BaseNNModel
The abstract class for all neural-network forecasting models in PyPOTS.
- Parameters:
batch_size (
int
) – Size of the batch input into the model for one step.epochs (
int
) – Training epochs, i.e. the maximum rounds of the model to be trained with.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.training_loss (
Union
[Criterion
,type
]) – The customized loss function designed by users for training the model. If not given, will use the default loss as claimed in the original paper.validation_metric (
Union
[Criterion
,type
]) – The customized metric function designed by users for validating the model. If not given, will use the default loss from the original paper as the metric.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.enable_amp (
bool
) – Whether to enable automatic mixed precision (AMP), default as False. If the implemented model is based on LLMs that need large-scale operation and AMP, please set it as True.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.
Notes
Optimizers are necessary for training deep-learning neural networks, but we don’t put a parameter
optimizer
here because some models (e.g. GANs) need more than one optimizer (e.g. one for generator, one for discriminator), andoptimizer
is ambiguous for them. Therefore, we leave optimizers as parameters for concrete model implementations, and you can pass any number of optimizers to your model when implementing it,pypots.clustering.crli.CRLI
for example.- fit(train_set, val_set=None, file_type='hdf5')[source]¶
Train the classifier on the given data.
- Parameters:
train_set (
Union
[dict
,str
]) – The dataset for model training, should be a dictionary including the key ‘X’, 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. 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 the key ‘X’.val_set (
Union
[dict
,str
,None
]) – The dataset for model validating, should be a dictionary including the key ‘X’, 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 validation, can contain missing values. 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 the key ‘X’.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', **kwargs)[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 forecasting results as key ‘forecasting’ and latent variables if necessary.
- Return type:
result_dict
- forecast(test_set, file_type='hdf5', **kwargs)[source]¶
Forecast the future the input with the trained model.
- Parameters:
- Returns:
Forecasting results.
- Return type:
array-like, shape [n_samples, n_pred_steps, n_features],
- 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).
- 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.
pypots.anomaly_detection.base¶
The base class for PyPOTS anomaly detection models.
- class pypots.anomaly_detection.base.BaseDetector(anomaly_rate, device=None, enable_amp=False, saving_path=None, model_saving_strategy='best', verbose=True)[source]¶
Bases:
BaseModel
Abstract class for all anomaly detection models.
- Parameters:
anomaly_rate – The rate of anomalies in the data, should be in the range (0, 1).
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.enable_amp (
bool
) – Whether to enable automatic mixed precision (AMP), default as False. If the implemented model is based on LLMs that need large-scale operation and AMP, please set it as True.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.
- abstract fit(train_set, val_set=None, file_type='hdf5')[source]¶
Train the detector on the given data.
- Parameters:
train_set (
Union
[dict
,str
]) – The dataset for model training, should be a dictionary including the key ‘X’, 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. 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 the key ‘X’.val_set (
Union
[dict
,str
,None
]) – The dataset for model validating, should be a dictionary including the key ‘X’, 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. 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 the key ‘X’.file_type (
str
) – The type of the given file if train_set and val_set are path strings.
- Return type:
- abstract predict(test_set, file_type='hdf5', **kwargs)[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 anomaly detection results as key ‘anomaly_detection’ and latent variables if necessary.
- Return type:
result_dict
- detect(test_set, file_type='hdf5', **kwargs)[source]¶
Detect anomalies in the given data with the trained model.
- Parameters:
- Returns:
Anomaly detection results in the input data.
- Return type:
array-like, with shape [n_samples, n_steps, n_features],
- 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).
- 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.
- class pypots.anomaly_detection.base.BaseNNDetector(anomaly_rate, training_loss, validation_metric, batch_size, epochs, patience=None, num_workers=0, device=None, enable_amp=False, saving_path=None, model_saving_strategy='best', verbose=True)[source]¶
Bases:
BaseNNModel
The abstract class for all neural-network anomaly detection models in PyPOTS.
- Parameters:
anomaly_rate (
float
) – The rate of anomalies in the data, should be in the range (0, 1).batch_size (
int
) – Size of the batch input into the model for one step.epochs (
int
) – Training epochs, i.e. the maximum rounds of the model to be trained with.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.training_loss (
Union
[Criterion
,type
]) – The customized loss function designed by users for training the model. If not given, will use the default loss as claimed in the original paper.validation_metric (
Union
[Criterion
,type
]) – The customized metric function designed by users for validating the model. If not given, will use the default MSE metric.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.enable_amp (
bool
) – Whether to enable automatic mixed precision (AMP), default as False. If the implemented model is based on LLMs that need large-scale operation and AMP, please set it as True.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.
Notes
Optimizers are necessary for training deep-learning neural networks, but we don’t put a parameter
optimizer
here because some models (e.g. GANs) need more than one optimizer (e.g. one for generator, one for discriminator), andoptimizer
is ambiguous for them. Therefore, we leave optimizers as parameters for concrete model implementations, and you can pass any number of optimizers to your model when implementing it,pypots.clustering.crli.CRLI
for example.- abstract fit(train_set, val_set=None, file_type='hdf5')[source]¶
Train the detector on the given data.
- Parameters:
train_set (
Union
[dict
,str
]) – The dataset for model training, should be a dictionary including the key ‘X’, 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. 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 the key ‘X’.val_set (
Union
[dict
,str
,None
]) – The dataset for model validating, should be a dictionary including the key ‘X’, 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. 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 the key ‘X’.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', **kwargs)[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 anomaly detection results as key ‘anomaly_detection’ and latent variables if necessary.
- Return type:
result_dict
- detect(test_set, file_type='hdf5', **kwargs)[source]¶
Detect anomalies in the given data with the trained model.
- Parameters:
- Returns:
Anomaly detection results of the given samples.
- Return type:
results
- 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).
- 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.
pypots.clustering.base¶
The base classes for PyPOTS clustering models.
- class pypots.clustering.base.BaseClusterer(n_clusters, device=None, enable_amp=False, saving_path=None, model_saving_strategy='best', verbose=True)[source]¶
Bases:
BaseModel
Abstract class for all clustering models.
- Parameters:
n_clusters (
int
) – The number of clusters in the clustering task.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.
- abstract fit(train_set, val_set=None, file_type='hdf5')[source]¶
Train the cluster.
- Parameters:
train_set (
Union
[dict
,str
]) – The dataset for model training, should be a dictionary including the key ‘X’, 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. 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 the key ‘X’.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 classification 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:
- abstract predict(test_set, file_type='hdf5', **kwargs)[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 clustering results as key ‘clustering’ and latent variables if necessary.
- Return type:
result_dict
- cluster(test_set, file_type='hdf5', **kwargs)[source]¶
Cluster the input with the trained model.
- Parameters:
- Returns:
Clustering results of the given data samples.
- Return type:
results
- 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).
- 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.
- class pypots.clustering.base.BaseNNClusterer(n_clusters, training_loss, validation_metric, batch_size, epochs=100, patience=None, num_workers=0, device=None, enable_amp=False, saving_path=None, model_saving_strategy='best', verbose=True)[source]¶
Bases:
BaseNNModel
The abstract class for all neural-network clustering models in PyPOTS.
- Parameters:
n_clusters (
int
) – The number of clusters in the clustering task.batch_size (
int
) – Size of the batch input into the model for one step.epochs (
int
) – Training epochs, i.e. the maximum rounds of the model to be trained with.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.training_loss (
Union
[Criterion
,type
]) – The customized loss function designed by users for training the model. If not given, will use the default loss as claimed in the original paper.validation_metric (
Union
[Criterion
,type
]) – The customized metric function designed by users for validating the model. If not given, will use the default loss from the original paper as the metric.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.enable_amp (
bool
) – Whether to enable automatic mixed precision (AMP), default as False. If the implemented model is based on LLMs that need large-scale operation and AMP, please set it as True.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.
Notes
Optimizers are necessary for training deep-learning neural networks, but we don’t put a parameter
optimizer
here because some models (e.g. GANs) need more than one optimizer (e.g. one for generator, one for discriminator), andoptimizer
is ambiguous for them. Therefore, we leave optimizers as parameters for concrete model implementations, and you can pass any number of optimizers to your model when implementing it,pypots.clustering.crli.CRLI
for example.- abstract fit(train_set, val_set=None, file_type='hdf5')[source]¶
Train the cluster.
- Parameters:
train_set (
Union
[dict
,str
]) – The dataset for model training, should be a dictionary including the key ‘X’, 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. 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 the key ‘X’.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 classification 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:
- abstract predict(test_set, file_type='hdf5')[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 clustering results as key ‘clustering’ and latent variables if necessary.
- Return type:
result_dict
- cluster(test_set, file_type='hdf5', **kwargs)[source]¶
Cluster the input with the trained model.
- Parameters:
- Returns:
Clustering results of the given data samples.
- Return type:
results
- 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).
- 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.