gluonts.torch.model.patch_tst 包#

class gluonts.torch.model.patch_tst.PatchTSTEstimator(prediction_length: int, patch_len: int, context_length: Optional[int] = None, stride: int = 8, padding_patch: str = 'end', d_model: int = 32, nhead: int = 4, dim_feedforward: int = 128, dropout: float = 0.1, activation: str = 'relu', norm_first: bool = False, num_encoder_layers: int = 2, lr: float = 0.001, weight_decay: float = 1e-08, scaling: Optional[str, None] = 'mean', distr_output: gluonts.torch.distributions.output.Output = gluonts.torch.distributions.studentT.StudentTOutput(beta=0.0), batch_size: int = 32, num_batches_per_epoch: int = 50, trainer_kwargs: Optional[Dict[str, Any]] = None, train_sampler: Optional[gluonts.transform.sampler.InstanceSampler] = None, validation_sampler: Optional[gluonts.transform.sampler.InstanceSampler] = None)[source]#

基类: gluonts.torch.model.estimator.PyTorchLightningEstimator

一个用于训练 PatchTST 模型进行预测的估计器,如 https://arxiv.org/abs/2211.14730 中所述,并扩展为概率模型。

此类使用 PatchTSTModel 中定义的模型,并将其包装到 PatchTSTLightningModule 中用于训练:训练使用 PyTorch Lightning 的 pl.Trainer 类执行。

参数
  • prediction_length (int) – 预测范围的长度。

  • context_length – 模型作为输入的时间步数,位于预测时间之前(默认值: 10 * prediction_length)。

  • patch_len – patch 的长度。

  • stride – patch 的步长。

  • padding_patch – patch 的填充方式。

  • d_model – Transformer 编码器中隐藏层的大小。

  • nhead – Transformer 编码器中注意力头的数量,必须能整除 d_model。

  • dim_feedforward – Transformer 编码器中隐藏层的大小。

  • dropout – Transformer 编码器中的 Dropout 概率。

  • activation – Transformer 编码器中的激活函数。

  • norm_first – 是否在注意力机制之前或之后应用归一化。

  • num_encoder_layers – Transformer 编码器中的层数。

  • lr – 学习率(默认值: 1e-3)。

  • weight_decay – 权重衰减正则化参数(默认值: 1e-8)。

  • scaling – 缩放参数可以是 "mean"、"std" 或 None。

  • distr_output – 用于评估观测值和采样预测的分布(默认值: StudentTOutput())。

  • batch_size – 用于训练的批次大小(默认值: 32)。

  • num_batches_per_epoch

    每个训练 epoch 中需要处理的批次数量

    (默认值: 50)。

  • trainer_kwargs – 提供给 pl.Trainer 用于构建的额外参数。

  • train_sampler – 控制训练期间窗口的采样。

  • validation_sampler – 控制验证期间窗口的采样。

create_lightning_module() lightning.pytorch.core.module.LightningModule[source]#

创建并返回用于训练(即计算损失)的网络。

返回

给定输入数据后计算损失的网络。

返回类型

pl.LightningModule

create_predictor(transformation: gluonts.transform._base.Transformation, module) gluonts.torch.model.predictor.PyTorchPredictor[source]#

创建并返回一个预测器对象。

参数
  • transformation – 在数据进入模型之前应用于数据的转换。

  • module – 一个训练好的 pl.LightningModule 对象。

返回

包装用于推理的 nn.Module 的预测器。

返回类型

预测器

create_training_data_loader(data: gluonts.dataset.Dataset, module: gluonts.torch.model.patch_tst.lightning_module.PatchTSTLightningModule, shuffle_buffer_length: Optional[int] = None, **kwargs) Iterable[source]#

创建一个用于训练目的的数据加载器。

参数
  • data – 用于创建数据加载器的数据集。

  • module – 将接收数据加载器批次的 pl.LightningModule 对象。

返回

数据加载器,即数据的批次上的可迭代对象。

返回类型

Iterable

create_transformation() gluonts.transform._base.Transformation[source]#

创建并返回训练和推理所需的转换。

返回

将在训练和推理时逐个应用于数据集的转换。

返回类型

Transformation

create_validation_data_loader(data: gluonts.dataset.Dataset, module: gluonts.torch.model.patch_tst.lightning_module.PatchTSTLightningModule, **kwargs) Iterable[source]#

创建一个用于验证目的的数据加载器。

参数
  • data – 用于创建数据加载器的数据集。

  • module – 将接收数据加载器批次的 pl.LightningModule 对象。

返回

数据加载器,即数据的批次上的可迭代对象。

返回类型

Iterable

lead_time: int#
prediction_length: int#
class gluonts.torch.model.patch_tst.PatchTSTLightningModule(model_kwargs: dict, lr: float = 0.001, weight_decay: float = 1e-08)[source]#

基类: lightning.pytorch.core.module.LightningModule

一个 pl.LightningModule 类,可用于使用 PyTorch Lightning 训练 PatchTSTModel

这是对(包装后的)PatchTSTModel 对象的薄层封装,暴露了评估训练损失和验证损失的方法。

参数
  • model_kwargs – 构建要训练的 PatchTSTModel 的关键字参数。

  • loss – 用于训练的损失函数。

  • lr – 学习率。

  • weight_decay – 权重衰减正则化参数。

configure_optimizers()[source]#

返回要使用的优化器。

forward(*args, **kwargs)[source]#

torch.nn.Module.forward() 相同。

参数
  • *args – 你决定传递给 forward 方法的任何内容。

  • **kwargs – 关键字参数也是可能的。

返回

你的模型的输出

training_step(batch, batch_idx: int)[source]#

执行训练步骤。

validation_step(batch, batch_idx: int)[source]#

执行验证步骤。

class gluonts.torch.model.patch_tst.PatchTSTModel(prediction_length: int, context_length: int, patch_len: int, stride: int, padding_patch: str, d_model: int, nhead: int, dim_feedforward: int, dropout: float, activation: str, norm_first: bool, num_encoder_layers: int, scaling: str, distr_output=gluonts.torch.distributions.studentT.StudentTOutput(beta=0.0))[source]#

基类: torch.nn.modules.module.Module

实现了 PatchTST 模型用于预测的模块,如 https://arxiv.org/abs/2211.14730 中所述,并扩展为概率模型。

参数
  • prediction_length – 要预测的时间点数量。

  • context_length – 模型在预测时间之前作为输入的时间步数。

  • distr_output – 用于评估观测值和采样预测的分布。默认值: StudentTOutput()

describe_inputs(batch_size=1) gluonts.model.inputs.InputSpec[source]#
forward(past_target: torch.Tensor, past_observed_values: torch.Tensor) Tuple[Tuple[torch.Tensor, ...], torch.Tensor, torch.Tensor][source]#

定义每次调用时执行的计算。

应由所有子类重写。

注意

虽然前向传播的实现需要在该函数中定义,但之后应该调用 Module 实例而不是直接调用该函数,因为前者负责运行注册的钩子,而后者会默默忽略它们。

loss(past_target: torch.Tensor, past_observed_values: torch.Tensor, future_target: torch.Tensor, future_observed_values: torch.Tensor) torch.Tensor[source]#
training: bool#
class gluonts.torch.model.patch_tst.SinusoidalPositionalEmbedding(num_positions: int, embedding_dim: int)[source]#

基类: torch.nn.modules.sparse.Embedding

此模块生成任意长度的正弦位置嵌入。

embedding_dim: int#
forward(input_ids_shape: torch.Size, past_key_values_length: int = 0) torch.Tensor[source]#

期望 input_ids_shape 的形状为 [bsz x seqlen x …]。

freeze: bool#
max_norm: Optional[float]#
norm_type: float#
num_embeddings: int#
padding_idx: Optional[int]#
scale_grad_by_freq: bool#
sparse: bool#
weight: torch.Tensor#