etna.models.nn.TimesFMModel#
- class TimesFMModel(path_or_url: str, encoder_length: int = 512, device: Literal['cpu', 'gpu'] = 'cpu', batch_size: int = 128, static_reals: List[str] | None = None, static_categoricals: List[str] | None = None, time_varying_reals: List[str] | None = None, time_varying_categoricals: List[str] | None = None, cache_dir: Path = PosixPath('/home/runner/.etna/timesfm'))[source]#
Bases:
NonPredictionIntervalContextRequiredAbstractModel
Class for pretrained timesfm models.
This model is only for zero-shot forecasting: it doesn’t support training on data during
fit
.This model doesn’t support forecasting on misaligned data with freq=None without exogenous features.
This model doesn’t support NaN in the middle or at the end of target and exogenous features. Use
TimeSeriesImputerTransform
to fill them.Official implementation: google-research/timesfm
Note
This model requires
timesfm
extension to be installed. Read more about this at installation page.Init TimesFM model.
- Parameters:
path_or_url (str) –
Path to the model. It can be huggingface repository, local path or external url.
If huggingface repository, the available models are:
’google/timesfm-1.0-200m-pytorch’.
During the first initialization model is downloaded from huggingface and saved to local
cache_dir
. All following initializations model will be loaded fromcache_dir
.If local path, it should be a file with model weights, that can be loaded by
torch.load()
.If external url, it must be a file with model weights, that can be loaded by
torch.load()
. Model will be downloaded tocache_dir
.
device (Literal['cpu', 'gpu']) – Device type. Can be “cpu” or “gpu”.
encoder_length (int) – Number of last timestamps to use as a context. It needs to be a multiplier of 32.
batch_size (int) – Batch size. It can be useful when inference is done on gpu.
static_reals (List[str] | None) – Continuous features that have one unique feature value for the whole series. The first value in the series will be used for each feature.
static_categoricals (List[str] | None) – Categorical features that have one unique feature value for the whole series. The first value in the series will be used for each feature.
time_varying_reals (List[str] | None) – Time varying continuous features known for future.
time_varying_categoricals (List[str] | None) – Time varying categorical features known for future.
cache_dir (Path) – Local path to save model from huggingface during first model initialization. All following class initializations appropriate model version will be downloaded from this path.
Methods
fit
(ts)Fit model.
forecast
(ts, prediction_size[, ...])Make autoregressive forecasts.
Get model.
Return a list of available pretrained timesfm models.
load
(path)Load the model.
Get default grid for tuning hyperparameters.
predict
(ts, prediction_size[, return_components])Make predictions using true values as autoregression context (teacher forcing).
save
(path)Save the model.
set_params
(**params)Return new object instance with modified parameters.
to_dict
()Collect all information about etna object in dict.
Attributes
This class stores its
__init__
parameters as attributes.Context size for model.
- fit(ts: TSDataset)[source]#
Fit model.
For this model, fit does nothing.
- Parameters:
ts (TSDataset) – Dataset with features.
- Returns:
Model after fit
- forecast(ts: TSDataset, prediction_size: int, return_components: bool = False) TSDataset [source]#
Make autoregressive forecasts.
- Parameters:
- Returns:
Dataset with predictions.
- Raises:
NotImplementedError: – if return_components mode is used.
ValueError: – if dataset doesn’t have any context timestamps.
ValueError: – if there are NaNs in the middle or end of the time series.
NotImplementedError: – if forecasting is done without exogenous features and dataset has None frequency.
- Return type:
- classmethod load(path: Path)[source]#
Load the model.
- Parameters:
path (Path) – Path to load object from.
- params_to_tune() Dict[str, BaseDistribution] [source]#
Get default grid for tuning hyperparameters.
This grid is empty.
- Returns:
Grid to tune.
- Return type:
- predict(ts: TSDataset, prediction_size: int, return_components: bool = False) TSDataset [source]#
Make predictions using true values as autoregression context (teacher forcing).
- Parameters:
- Returns:
Dataset with predictions.
- Return type:
- save(path: Path)[source]#
Save the model. This method doesn’t save model’s weights.
During
load
weights are loaded from the path where they were saved duringinit
- Parameters:
path (Path) – Path to save object to.
- set_params(**params: dict) Self [source]#
Return new object instance with modified parameters.
Method also allows to change parameters of nested objects within the current object. For example, it is possible to change parameters of a
model
in aPipeline
.Nested parameters are expected to be in a
<component_1>.<...>.<parameter>
form, where components are separated by a dot.- Parameters:
**params (dict) – Estimator parameters
- Returns:
New instance with changed parameters
- Return type:
Self
Examples
>>> from etna.pipeline import Pipeline >>> from etna.models import NaiveModel >>> from etna.transforms import AddConstTransform >>> model = NaiveModel(lag=1) >>> transforms = [AddConstTransform(in_column="target", value=1)] >>> pipeline = Pipeline(model, transforms=transforms, horizon=3) >>> pipeline.set_params(**{"model.lag": 3, "transforms.0.value": 2}) Pipeline(model = NaiveModel(lag = 3, ), transforms = [AddConstTransform(in_column = 'target', value = 2, inplace = True, out_column = None, )], horizon = 3, )