Transformers 文档

PatchTSMixer

Hugging Face's logo
加入 Hugging Face 社区

并获取增强的文档体验

开始使用

PatchTSMixer

PyTorch

概述

PatchTSMixer 模型由 Vijay Ekambaram, Arindam Jati, Nam Nguyen, Phanwadee Sinthong 和 Jayant Kalagnanam 在 TSMixer: Lightweight MLP-Mixer Model for Multivariate Time Series Forecasting 中提出。

PatchTSMixer 是一种基于 MLP-Mixer 架构的轻量级时间序列建模方法。在这个 HuggingFace 实现中,我们提供了 PatchTSMixer 的功能,可以轻松地促进跨补丁、通道和隐藏特征的轻量级混合,以实现有效​​的多元时间序列建模。它还支持从简单的门控注意力机制到更复杂的自注意力模块等各种注意力机制,可以根据需要进行自定义。该模型可以进行预训练,随后用于各种下游任务,例如预测、分类和回归。

以下是论文的摘要

TSMixer 是一种轻量级神经架构,专门由多层感知器 (MLP) 模块组成,旨在对修补的时间序列进行多元预测和表示学习。我们的模型从计算机视觉中 MLP-Mixer 模型的成功中汲取灵感。我们展示了将 Vision MLP-Mixer 应用于时间序列所涉及的挑战,并引入了经验证的组件来提高准确性。这包括一种新颖的设计范例,即将在线协调头连接到 MLP-Mixer 主干,以显式建模时间序列属性,例如层次结构和通道相关性。我们还提出了一种混合通道建模方法,以有效处理嘈杂的通道交互以及跨各种数据集的泛化,这是现有补丁通道混合方法中的常见挑战。此外,在主干中引入了简单的门控注意力机制,以优先考虑重要特征。通过结合这些轻量级组件,我们显着增强了简单 MLP 结构的学习能力,以最小的计算使用量优于复杂的 Transformer 模型。此外,TSMixer 的模块化设计使其与监督和掩码自监督学习方法兼容,使其成为时间序列基础模型的有希望的构建块。 TSMixer 在预测方面明显优于最先进的 MLP 和 Transformer 模型,幅度为 8-60%。它还在内存和运行时间显着减少(2-3 倍)的情况下,优于最新的强大的 Patch-Transformer 模型基准(1-2%)。

该模型由 ajati, vijaye12, gsinthong, namctin, wmgifford, kashif 贡献。

使用示例

以下代码片段展示了如何随机初始化 PatchTSMixer 模型。该模型与 Trainer API 兼容。


from transformers import PatchTSMixerConfig, PatchTSMixerForPrediction
from transformers import Trainer, TrainingArguments,


config = PatchTSMixerConfig(context_length = 512, prediction_length = 96)
model = PatchTSMixerForPrediction(config)
trainer = Trainer(model=model, args=training_args, 
            train_dataset=train_dataset,
            eval_dataset=valid_dataset)
trainer.train()
results = trainer.evaluate(test_dataset)

使用技巧

该模型还可以用于时间序列分类和时间序列回归。 请参阅相应的 PatchTSMixerForTimeSeriesClassificationPatchTSMixerForRegression 类。

资源

  • 一篇深入解释 PatchTSMixer 的博客文章可以在这里找到。该博客也可以在 Google Colab 中打开。

PatchTSMixerConfig

class transformers.PatchTSMixerConfig

< >

( context_length: int = 32 patch_length: int = 8 num_input_channels: int = 1 patch_stride: int = 8 num_parallel_samples: int = 100 d_model: int = 8 expansion_factor: int = 2 num_layers: int = 3 dropout: float = 0.2 mode: str = 'common_channel' gated_attn: bool = True norm_mlp: str = 'LayerNorm' self_attn: bool = False self_attn_heads: int = 1 use_positional_encoding: bool = False positional_encoding_type: str = 'sincos' scaling: typing.Union[str, bool, NoneType] = 'std' loss: str = 'mse' init_std: float = 0.02 post_init: bool = False norm_eps: float = 1e-05 mask_type: str = 'random' random_mask_ratio: float = 0.5 num_forecast_mask_patches: typing.Union[typing.List[int], int, NoneType] = [2] mask_value: int = 0 masked_loss: bool = True channel_consistent_masking: bool = True unmasked_channel_indices: typing.Optional[typing.List[int]] = None head_dropout: float = 0.2 distribution_output: str = 'student_t' prediction_length: int = 16 prediction_channel_indices: list = None num_targets: int = 3 output_range: list = None head_aggregation: str = 'max_pool' **kwargs )

参数

  • context_length (int, 可选, 默认为 32) — 输入序列的上下文/历史长度。
  • patch_length (int, 可选, 默认为 8) — 输入序列的补丁长度。
  • num_input_channels (int, 可选, 默认为 1) — 输入变量的数量。对于单变量时间序列,请设置为 1。
  • patch_stride (int, 可选, 默认为 8) — 确定两个连续补丁之间的重叠。如果需要非重叠补丁,请将其设置为 patch_length(或更大)。
  • num_parallel_samples (int, 可选, 默认为 100) — 用于概率预测并行生成的样本数。
  • d_model (int, 可选, 默认为 8) — 模型的隐藏维度。建议将其设置为补丁长度的倍数(即补丁长度的 2-5 倍)。值越大表示模型越复杂。
  • expansion_factor (int, 可选, 默认为 2) — 在 MLP 内部使用的扩展因子。建议范围为 2-5。值越大表示模型越复杂。
  • num_layers (int, 可选, 默认为 3) — 要使用的层数。建议范围为 3-15。值越大表示模型越复杂。
  • dropout (float, 可选, 默认为 0.2) — PatchTSMixer 主干的 dropout 概率。建议范围为 0.2-0.7
  • mode (str, 可选, 默认为 "common_channel") — Mixer 模式。 决定如何处理通道。 允许的值:“common_channel”, “mix_channel”。 在 “common_channel” 模式中,我们遵循通道独立建模,没有显式的通道混合。 通道混合以隐式方式通过跨通道共享权重发生。(首选的第一种方法) 在 “mix_channel” 模式中,除了 patch 和特征 mixer 之外,我们还遵循显式的通道混合。(当通道相关性对于建模非常重要时的首选方法)
  • gated_attn (bool, 可选, 默认为 True) — 启用门控注意力。
  • norm_mlp (str, 可选, 默认为 "LayerNorm") — 归一化层 (BatchNorm 或 LayerNorm)。
  • self_attn (bool, 可选, 默认为 False) — 启用跨 patch 的微型自注意力机制。 当带有门控注意力的 Vanilla PatchTSMixer 的输出不令人满意时,可以启用此功能。 启用此功能可以实现跨 patch 的显式成对注意力建模。
  • self_attn_heads (int, 可选, 默认为 1) — 自注意力头的数量。 仅当 self_attn 设置为 True 时才有效。
  • use_positional_encoding (bool, 可选, 默认为 False) — 启用微型自注意力层的位置编码。 仅当 self_attn 设置为 True 时才有效。
  • positional_encoding_type (str, 可选, 默认为 "sincos") — 位置编码。 支持的选项 "random""sincos"。 仅当 use_positional_encoding 设置为 True 时才有效
  • scaling (stringbool, 可选, 默认为 "std") — 是否通过 “mean” 缩放器、“std” 缩放器缩放输入目标,如果为 None 则不缩放。 如果为 True,则缩放器设置为 “mean”。
  • loss (string, 可选, 默认为 "mse") — 模型的损失函数,对应于 distribution_output 头。 对于参数分布,它是负对数似然 (“nll”),对于点估计,它是均方误差 “mse”。
  • init_std (float, 可选, 默认为 0.02) — 截断正态权重初始化分布的标准差。
  • post_init (bool, 可选, 默认为 False) — 是否使用来自 transformers 库的自定义权重初始化,还是使用 PyTorch 中的默认初始化。 设置为 False 将执行 PyTorch 权重初始化。
  • norm_eps (float, 可选, 默认为 1e-05) — 添加到分母的值,用于归一化的数值稳定性。
  • mask_type (str, 可选, 默认为 "random") — 用于掩码预训练模式的掩码类型。 允许的值为 “random”、“forecast”。 在随机掩码中,点是随机掩码的。 在预测掩码中,点在末尾附近被掩码。
  • random_mask_ratio (float, 可选, 默认为 0.5) — 当 mask_typerandom 时使用的掩码比例。 值越高表示掩码越多。
  • num_forecast_mask_patches (intlist, 可选, 默认为 [2]) — 每个批次样本末尾要掩码的 patch 数量。 如果是整数,则批次中的所有样本将具有相同数量的掩码 patch。 如果是列表,则批次中的样本将按列表中定义的数字随机掩码。 此参数仅用于预测预训练。
  • mask_value (float, 可选, 默认为 0.0) — 要使用的掩码值。
  • masked_loss (bool, 可选, 默认为 True) — 是否仅在掩码部分计算预训练损失,还是在整个输出上计算。
  • channel_consistent_masking (bool, 可选, 默认为 True) — 如果为 true,则掩码将在时间序列的所有通道中保持相同。 否则,掩码位置将在通道之间变化。
  • unmasked_channel_indices (list, 可选) — 在预训练期间未被掩码的通道。
  • head_dropout (float, 可选, 默认为 0.2) — PatchTSMixer 头的 dropout 概率。
  • distribution_output (string, 可选, 默认为 "student_t") — 当损失为 “nll” 时,模型的分布发射头。 可以是 “student_t”、“normal” 或 “negative_binomial”。
  • prediction_length (int, 可选, 默认为 16) — 用于预测任务的预测时间步数。 也称为预测范围。
  • prediction_channel_indices (list, 可选) — 要预测的通道索引列表。 如果为 None,则预测所有通道。 目标数据应包含所有通道,我们在损失计算之前显式过滤预测和目标中的通道。
  • num_targets (int, 可选, 默认为 3) — 回归任务的目标数(回归变量的维度)。
  • output_range (list, 可选) — 用于限制回归任务的输出范围。 默认为 None。
  • head_aggregation (str, 可选, 默认为 "max_pool") — 为分类或回归任务启用的聚合模式。 允许的值为 None, “use_last”, “max_pool”, “avg_pool”。

这是配置类,用于存储 PatchTSMixerModel 的配置。 它用于根据指定的参数实例化 PatchTSMixer 模型,定义模型架构。 使用默认值实例化配置将产生类似于 PatchTSMixer ibm/patchtsmixer-etth1-pretrain 架构的配置。

配置对象继承自 PretrainedConfig,可用于控制模型输出。 有关更多信息,请阅读 PretrainedConfig 的文档。

示例

>>> from transformers import PatchTSMixerConfig, PatchTSMixerModel

>>> # Initializing a default PatchTSMixer configuration
>>> configuration = PatchTSMixerConfig()

>>> # Randomly initializing a model (with random weights) from the configuration
>>> model = PatchTSMixerModel(configuration)

>>> # Accessing the model configuration
>>> configuration = model.config

PatchTSMixerModel

class transformers.PatchTSMixerModel

< >

( config: PatchTSMixerConfig mask_input: bool = False )

参数

  • config (PatchTSMixerConfig) — 包含模型所有参数的模型配置类。 使用配置文件初始化不会加载与模型关联的权重,仅加载配置。 查看 from_pretrained() 方法以加载模型权重。
  • mask_input (bool, 可选, 默认为 False) — 如果为 True,则启用 Masking。 否则为 False。

用于时间序列预测的 PatchTSMixer 模型。

此模型继承自 PreTrainedModel。 查看超类文档,了解库为其所有模型实现的通用方法(例如下载或保存、调整输入嵌入大小、剪枝头等)。

此模型也是 PyTorch torch.nn.Module 子类。 将其用作常规 PyTorch 模块,并参阅 PyTorch 文档,了解与常规用法和行为相关的所有事项。

forward

< >

( past_values: Tensor observed_mask: typing.Optional[torch.Tensor] = None output_hidden_states: typing.Optional[bool] = False return_dict: typing.Optional[bool] = None ) transformers.models.patchtsmixer.modeling_patchtsmixer.PatchTSMixerModelOutputtuple(torch.FloatTensor)

参数

  • past_values (形状为 (batch_size, seq_length, num_input_channels)torch.FloatTensor) — 时间序列的上下文值。 对于预训练任务,这表示用于预测掩码部分的输入时间序列。 对于预测任务,这表示历史/过去的时间序列值。 同样,对于分类或回归任务,它表示时间序列的适当上下文值。

    对于单变量时间序列,num_input_channels 维度应为 1。 对于多变量时间序列,它大于 1。

  • output_hidden_states (bool, 可选) — 是否返回所有层的隐藏状态。
  • return_dict (bool, 可选) — 是否返回 ModelOutput 而不是普通元组。
  • observed_mask (形状为 (batch_size, sequence_length, num_input_channels)torch.FloatTensor, 可选) — 布尔掩码,指示哪些 past_values 被观察到,哪些缺失。 掩码值在 [0, 1] 中选择:

    • 1 表示 已观察到 的值,
    • 0 表示 缺失 的值(即被零替换的 NaN)。

返回值

transformers.models.patchtsmixer.modeling_patchtsmixer.PatchTSMixerModelOutputtuple(torch.FloatTensor)

一个 transformers.models.patchtsmixer.modeling_patchtsmixer.PatchTSMixerModelOutput 或一个 torch.FloatTensor 元组 (如果传递了 return_dict=False 或当 config.return_dict=False 时),其中包含取决于配置 (PatchTSMixerConfig) 和输入的各种元素。

  • last_hidden_state (形状为 (batch_size, num_channels, num_patches, d_model)torch.FloatTensor) — 模型最后一层输出的隐藏状态。
  • hidden_states (tuple(torch.FloatTensor), 可选) — 模型在每一层输出的隐藏状态。
  • patch_input (形状为 (batch_size, num_channels, num_patches, patch_length)torch.FloatTensor) — 模型的 Patched 输入数据。
  • mask: (形状为 (batch_size, num_channels, num_patches)torch.FloatTensor,可选) — 布尔张量,指示在掩码补丁中为 True,否则为 False。
  • loc: (形状为 (batch_size, 1, num_channels)torch.FloatTensor,可选) — 给出每个通道上下文窗口的均值。 如果启用了 revin,则用于模型外部的 revin 反归一化。
  • scale: (形状为 (batch_size, 1, num_channels)torch.FloatTensor,可选) — 给出每个通道上下文窗口的标准差。 如果启用了 revin,则用于模型外部的 revin 反归一化。

PatchTSMixerModel forward 方法,覆盖了 __call__ 特殊方法。

虽然 forward 传递的配方需要在该函数中定义,但应该在之后调用 Module 实例而不是此函数,因为前者负责运行预处理和后处理步骤,而后者会静默地忽略它们。

PatchTSMixerForPrediction

class transformers.PatchTSMixerForPrediction

< >

( config: PatchTSMixerConfig )

参数

  • config (PatchTSMixerConfig) — 配置。

用于预测应用的 PatchTSMixer

forward

< >

( past_values: Tensor observed_mask: typing.Optional[torch.Tensor] = None future_values: typing.Optional[torch.Tensor] = None output_hidden_states: typing.Optional[bool] = False return_loss: bool = True return_dict: typing.Optional[bool] = None ) transformers.models.patchtsmixer.modeling_patchtsmixer.PatchTSMixerForPredictionOutputtuple(torch.FloatTensor)

参数

  • past_values (形状为 (batch_size, seq_length, num_input_channels)torch.FloatTensor) — 时间序列的上下文值。 对于预训练任务,这表示用于预测掩码部分的输入时间序列。 对于预测任务,这表示历史/过去的时间序列值。 同样,对于分类或回归任务,它表示时间序列的适当上下文值。

    对于单变量时间序列,num_input_channels 维度应为 1。 对于多变量时间序列,它大于 1。

  • output_hidden_states (bool, 可选) — 是否返回所有层的隐藏状态。
  • return_dict (bool, 可选) — 是否返回 ModelOutput 而不是普通元组。
  • observed_mask (形状为 (batch_size, sequence_length, num_input_channels)torch.FloatTensor, 可选) — 布尔掩码,指示哪些 past_values 被观察到,哪些缺失。 掩码值在 [0, 1] 中选择:

    • 1 表示 已观察到 的值,
    • 0 表示 缺失 的值(即被零替换的 NaN)。
  • future_values (对于预测,形状为 (batch_size, target_len, num_input_channels)torch.FloatTensor,— 对于回归,形状为 (batch_size, num_targets),或对于分类,形状为 (batch_size,)可选): 时间序列的目标值,用作模型的标签。 future_values 是 Transformer 在训练期间需要学习输出的内容,给定 past_values。 请注意,这对于预训练任务不是必需的。

    对于预测任务,形状应为 (batch_size, target_len, num_input_channels)。 即使我们只想通过在 prediction_channel_indices 参数中设置索引来预测特定通道,也请传递包含所有通道的目标数据,因为预测和目标通道过滤都将在损失计算之前手动应用。

  • return_loss (bool, 可选) — 是否在 forward 调用中返回损失。

返回值

transformers.models.patchtsmixer.modeling_patchtsmixer.PatchTSMixerForPredictionOutputtuple(torch.FloatTensor)

一个 transformers.models.patchtsmixer.modeling_patchtsmixer.PatchTSMixerForPredictionOutput 或一个 torch.FloatTensor 元组 (如果传递了 return_dict=False 或当 config.return_dict=False 时),其中包含取决于配置 (PatchTSMixerConfig) 和输入的各种元素。

  • prediction_outputs (形状为 (batch_size, prediction_length, num_input_channels)torch.FloatTensor) — 来自预测头的预测输出。
  • last_hidden_state (形状为 (batch_size, num_input_channels, num_patches, d_model)torch.FloatTensor) — 通过 head 之前的 Backbone 嵌入。
  • hidden_states (tuple(torch.FloatTensor), 可选) — 模型在每一层输出的隐藏状态,加上可选的初始嵌入输出。
  • loss (可选,当提供 y 时返回,形状为 ()torch.FloatTensor) — 总损失。
  • loc (torch.FloatTensor, 可选,形状为 (batch_size, 1, num_input_channels)) — 输入均值
  • scale (torch.FloatTensor, 可选,形状为 (batch_size, 1, num_input_channels)) — 输入标准差

PatchTSMixerForPrediction forward 方法,覆盖了 __call__ 特殊方法。

虽然 forward 传递的配方需要在该函数中定义,但应该在之后调用 Module 实例而不是此函数,因为前者负责运行预处理和后处理步骤,而后者会静默地忽略它们。

PatchTSMixerForTimeSeriesClassification

class transformers.PatchTSMixerForTimeSeriesClassification

< >

( config: PatchTSMixerConfig )

参数

  • config (PatchTSMixerConfig) — 配置。

用于分类应用的 PatchTSMixer

forward

< >

( past_values: Tensor target_values: Tensor = None output_hidden_states: typing.Optional[bool] = False return_loss: bool = True return_dict: typing.Optional[bool] = None ) transformers.models.patchtsmixer.modeling_patchtsmixer.PatchTSMixerForTimeSeriesClassificationOutputtuple(torch.FloatTensor)

参数

  • past_values (形状为 (batch_size, seq_length, num_input_channels)torch.FloatTensor) — 时间序列的上下文值。 对于预训练任务,这表示用于预测掩码部分的输入时间序列。 对于预测任务,这表示历史/过去的时间序列值。 同样,对于分类或回归任务,它表示时间序列的适当上下文值。

    对于单变量时间序列,num_input_channels 维度应为 1。 对于多变量时间序列,它大于 1。

  • output_hidden_states (bool, 可选) — 是否返回所有层的隐藏状态。
  • return_dict (bool, 可选) — 是否返回 ModelOutput 而不是纯粹的元组。
  • target_values (torch.FloatTensor,形状为 (batch_size, target_len, num_input_channels),用于预测;— (batch_size, num_targets),用于回归;或 (batch_size,),用于分类,可选): 时间序列的目标值,作为模型的标签。 target_values 是 Transformer 在训练期间学习输出的内容,给定 past_values。 请注意,这对于预训练任务不是必需的。

    对于预测任务,形状应为 (batch_size, target_len, num_input_channels)。 即使我们只想通过在 prediction_channel_indices 参数中设置索引来预测特定通道,也请传递包含所有通道的目标数据,因为预测和目标的通道过滤将在损失计算之前手动应用。

    对于分类任务,其形状为 (batch_size,)

    对于回归任务,其形状为 (batch_size, num_targets)

  • return_loss (bool, 可选) — 是否在 forward 调用中返回损失值。

返回值

transformers.models.patchtsmixer.modeling_patchtsmixer.PatchTSMixerForTimeSeriesClassificationOutputtuple(torch.FloatTensor)

一个 transformers.models.patchtsmixer.modeling_patchtsmixer.PatchTSMixerForTimeSeriesClassificationOutput 或一个 torch.FloatTensor 元组(如果传递了 return_dict=False 或当 config.return_dict=False 时),包含各种元素,具体取决于配置 (PatchTSMixerConfig) 和输入。

  • prediction_outputs (torch.FloatTensor,形状为 (batch_size, num_labels)) — 来自分类头的预测输出。
  • last_hidden_state (形状为 (batch_size, num_input_channels, num_patches, d_model)torch.FloatTensor) — 通过 head 之前的 Backbone 嵌入。
  • hidden_states (tuple(torch.FloatTensor), 可选) — 模型在每一层输出的隐藏状态,加上可选的初始嵌入输出。
  • loss (可选,当提供 y 时返回,形状为 ()torch.FloatTensor) — 总损失。

PatchTSMixerForTimeSeriesClassification 的 forward 方法,覆盖了 __call__ 特殊方法。

虽然 forward 传递的配方需要在该函数中定义,但应该在之后调用 Module 实例而不是此函数,因为前者负责运行预处理和后处理步骤,而后者会静默地忽略它们。

PatchTSMixerForPretraining

class transformers.PatchTSMixerForPretraining

< >

( config: PatchTSMixerConfig )

参数

  • config (PatchTSMixerConfig) — 配置。

用于掩码预训练的 PatchTSMixer

forward

< >

( past_values: Tensor observed_mask: typing.Optional[torch.Tensor] = None output_hidden_states: typing.Optional[bool] = False return_loss: bool = True return_dict: typing.Optional[bool] = None ) transformers.models.patchtsmixer.modeling_patchtsmixer.PatchTSMixerForPreTrainingOutputtuple(torch.FloatTensor)

参数

  • past_values (torch.FloatTensor,形状为 (batch_size, seq_length, num_input_channels)) — 时间序列的上下文值。 对于预训练任务,这表示用于预测掩码部分的输入时间序列。 对于预测任务,这表示历史/过去的时间序列值。 类似地,对于分类或回归任务,它表示时间序列的适当上下文值。

    对于单变量时间序列,num_input_channels 维度应为 1。 对于多变量时间序列,它大于 1。

  • output_hidden_states (bool, 可选) — 是否返回所有层的隐藏状态。
  • return_dict (bool, 可选) — 是否返回 ModelOutput 而不是纯粹的元组。
  • observed_mask (torch.FloatTensor,形状为 (batch_size, sequence_length, num_input_channels), 可选) — 布尔掩码,指示哪些 past_values 是被观察到的,哪些是缺失的。 掩码值在 [0, 1] 中选择:

    • 1 表示 观察到 的值,
    • 0 表示 缺失 的值 (即被零替换的 NaN)。
  • return_loss (bool, 可选) — 是否在 forward 调用中返回损失值。

返回值

transformers.models.patchtsmixer.modeling_patchtsmixer.PatchTSMixerForPreTrainingOutputtuple(torch.FloatTensor)

一个 transformers.models.patchtsmixer.modeling_patchtsmixer.PatchTSMixerForPreTrainingOutput 或一个 torch.FloatTensor 元组(如果传递了 return_dict=False 或当 config.return_dict=False 时),包含各种元素,具体取决于配置 (PatchTSMixerConfig) 和输入。

  • prediction_outputs (torch.FloatTensor,形状为 (batch_size, num_input_channels, num_patches, patch_length)) — 来自预训练头的预测输出。
  • hidden_states (tuple(torch.FloatTensor), 可选) — 模型在每一层输出的隐藏状态。
  • last_hidden_state (形状为 (batch_size, num_input_channels, num_patches, d_model)torch.FloatTensor) — 通过 head 之前的 Backbone 嵌入。
  • loss (可选,当提供 y 时返回, torch.FloatTensor,形状为 ()) — 总损失

PatchTSMixerForPretraining 的 forward 方法,覆盖了 __call__ 特殊方法。

虽然 forward 传递的配方需要在该函数中定义,但应该在之后调用 Module 实例而不是此函数,因为前者负责运行预处理和后处理步骤,而后者会静默地忽略它们。

PatchTSMixerForRegression

class transformers.PatchTSMixerForRegression

< >

( config: PatchTSMixerConfig )

参数

  • config (PatchTSMixerConfig) — 配置。

用于回归应用的 PatchTSMixer

forward

< >

( past_values: Tensor target_values: Tensor = None output_hidden_states: typing.Optional[bool] = False return_loss: bool = True return_dict: typing.Optional[bool] = None ) transformers.models.patchtsmixer.modeling_patchtsmixer.PatchTSMixerForRegressionOutputtuple(torch.FloatTensor)

参数

  • past_values (torch.FloatTensor,形状为 (batch_size, seq_length, num_input_channels)) — 时间序列的上下文值。 对于预训练任务,这表示用于预测掩码部分的输入时间序列。 对于预测任务,这表示历史/过去的时间序列值。 类似地,对于分类或回归任务,它表示时间序列的适当上下文值。

    对于单变量时间序列,num_input_channels 维度应为 1。 对于多变量时间序列,它大于 1。

  • output_hidden_states (bool, 可选) — 是否返回所有层的隐藏状态。
  • return_dict (bool, 可选) — 是否返回 ModelOutput 而不是纯粹的元组。
  • target_values (torch.FloatTensor,形状为 (batch_size, target_len, num_input_channels),用于预测;— (batch_size, num_targets),用于回归;或 (batch_size,),用于分类,可选): 时间序列的目标值,作为模型的标签。 target_values 是 Transformer 在训练期间学习输出的内容,给定 past_values。 请注意,这对于预训练任务不是必需的。

    对于预测任务,形状应为 (batch_size, target_len, num_input_channels)。 即使我们只想通过在 prediction_channel_indices 参数中设置索引来预测特定通道,也请传递包含所有通道的目标数据,因为预测和目标的通道过滤将在损失计算之前手动应用。

    对于分类任务,其形状为 (batch_size,)

    对于回归任务,其形状为 (batch_size, num_targets)

  • return_loss (bool, 可选) — 是否在 forward 调用中返回损失值。

返回值

transformers.models.patchtsmixer.modeling_patchtsmixer.PatchTSMixerForRegressionOutputtuple(torch.FloatTensor)

一个 transformers.models.patchtsmixer.modeling_patchtsmixer.PatchTSMixerForRegressionOutput 或一个 torch.FloatTensor 元组(如果传递了 return_dict=False 或当 config.return_dict=False 时),包含各种元素,具体取决于配置 (PatchTSMixerConfig) 和输入。

  • regression_outputs (torch.FloatTensor,形状为 (batch_size, num_targets)) — 来自回归头的预测输出。
  • last_hidden_state (形状为 (batch_size, num_input_channels, num_patches, d_model)torch.FloatTensor) — 通过 head 之前的 Backbone 嵌入。
  • hidden_states (tuple(torch.FloatTensor), 可选) — 模型在每一层输出的隐藏状态,加上可选的初始嵌入输出。
  • loss (可选,当提供 y 时返回,形状为 ()torch.FloatTensor) — 总损失。

PatchTSMixerForRegression 的 forward 方法,覆盖了 __call__ 特殊方法。

虽然 forward 传递的配方需要在该函数中定义,但应该在之后调用 Module 实例而不是此函数,因为前者负责运行预处理和后处理步骤,而后者会静默地忽略它们。

< > 更新 在 GitHub 上