DeepAR#
- class pytorch_forecasting.models.deepar._deepar.DeepAR(cell_type: str = 'LSTM', hidden_size: int = 10, rnn_layers: int = 2, dropout: float = 0.1, static_categoricals: List[str] | None = None, static_reals: List[str] | None = None, time_varying_categoricals_encoder: List[str] | None = None, time_varying_categoricals_decoder: List[str] | None = None, categorical_groups: Dict[str, List[str]] | None = None, time_varying_reals_encoder: List[str] | None = None, time_varying_reals_decoder: List[str] | None = None, embedding_sizes: Dict[str, Tuple[int, int]] | None = None, embedding_paddings: List[str] | None = None, embedding_labels: Dict[str, ndarray] | None = None, x_reals: List[str] | None = None, x_categoricals: List[str] | None = None, n_validation_samples: int = None, n_plotting_samples: int = None, target: str | List[str] = None, target_lags: Dict[str, List[int]] | None = None, loss: 分布损失 = None, logging_metrics: ModuleList = None, **kwargs)[来源]#
基础:
AutoRegressiveBaseModelWithCovariates
DeepAR 网络。
该代码基于文章 DeepAR: Probabilistic forecasting with autoregressive recurrent networks。
通过使用多元损失,例如
MultivariateNormalDistributionLoss
,网络被转换为一个 DeepVAR network。- Parameters:
cell_type (str, optional) – 循环单元类型 [“LSTM”, “GRU”]。默认为 “LSTM”。
hidden_size (int, 可选) – 隐藏递归大小 - 最重要的超参数之一,与
rnn_layers
一起使用。默认为10。rnn_layers (int, optional) – RNN 层数 - 重要的超参数。默认为 2。
dropout (float, 可选) – RNN层中的丢弃。默认为0.1。
static_categoricals – 静态分类变量的位置整数
static_reals – 静态连续变量的位置的整数
time_varying_categoricals_encoder – 编码器的分类变量的位置的整数
time_varying_categoricals_decoder – 解码器中分类变量的位置的整数
time_varying_reals_encoder – 编码器的连续变量位置的整数
time_varying_reals_decoder – 解码器中连续变量的位置的整数
categorical_groups – 字典,其中值是分类变量的列表,这些变量共同形成一个新的分类变量,该变量是字典中的键
x_reals – 传递给前向函数的张量中连续变量的顺序
x_categoricals – 传递给前向函数的张量中的分类变量的顺序
embedding_sizes – 字典,将(字符串)索引映射到分类类的数量和嵌入大小的元组
embedding_paddings – 嵌入的索引列表,负责将零的嵌入转化为零向量
embedding_labels – 字典,将(字符串)索引映射到分类标签的列表
n_validation_samples (int, 可选) – 用于计算验证指标的样本数量。默认为 None,即在验证阶段不进行采样,并使用分布的“均值”计算日志指标。
n_plotting_samples (int, 可选) – 训练期间生成用于绘制预测的样本数。如果不为 None,默认为
n_validation_samples
,否则默认为 100。target (str, 可选) – 目标变量或目标变量列表。默认为 None。
target_lags (Dict[str, Dict[str, int]]) – 目标名称到时间步列表的字典,表示变量应滞后的时间步。 滞后可以用来向模型指示季节性。如果你知道数据的季节性, 至少添加具有相应滞后的目标变量以改善性能。 默认情况下没有滞后,即一个空字典。
损失 (DistributionLoss, 可选) – 分布损失函数。请注意,每个分布损失函数可能对目标规范化有特定要求。 默认为
NormalDistributionLoss
。logging_metrics (nn.ModuleList, 可选) – 训练期间记录的指标。默认为 nn.ModuleList([SMAPE(), MAE(), RMSE(), MAPE(), MASE()]).
方法
construct_input_vector
(x_cat, x_cont[, ...])将输入向量创建到RNN网络中
create_log
(x, y, out, batch_idx)创建在训练和验证步骤中使用的日志。
decode
(输入向量, 目标缩放, ...[, ...])将RNN的隐藏状态解码为预测。
decode_all
(x, hidden_state[, lengths])encode
(x)将序列编码为隐藏状态
forward
(x[, n_samples])前馈网络
from_dataset
(数据集[, ...])从数据集中创建模型。
predict
(数据[, 模式, 返回索引, ...])预测数据加载器
- construct_input_vector(x_cat: Tensor, x_cont: Tensor, one_off_target: Tensor = None) Tensor [来源]#
将输入向量创建到RNN网络中
- Parameters:
one_off_target – 要插入到目标第一个位置的张量。如果为 None(默认),则移除第一个时间步。
- create_log(x, y, out, batch_idx)[来源]#
创建在训练和验证步骤中使用的日志。
- Parameters:
x (字典[字符串, torch.Tensor]) – 通过数据加载器传递给网络的 x
y (Tuple[torch.Tensor, torch.Tensor]) – 作为由数据加载器传递给损失函数的 y
out (字典[字符串, torch.Tensor]) – 网络的输出
batch_idx (int) – 批次编号
prediction_kwargs (字典[字符串, 任意类型], 可选) – 传递给
to_prediction()
的参数。默认为 {}。quantiles_kwargs (字典[字符串, 任何类型], 可选) –
to_quantiles()
. 默认值为 {}.
- Returns:
训练和验证步骤返回的日志字典
- Return type:
字典[str, 任何]
- decode(input_vector: Tensor, target_scale: Tensor, decoder_lengths: Tensor, hidden_state: Tuple[Tensor, Tensor] | Tensor, n_samples: int = None) Tuple[Tensor, bool] [来源]#
将RNN的隐藏状态解码为预测。如果给定n_samples,解码时不是使用实际值,而是通过迭代从过去的预测中采样新的目标。
- classmethod from_dataset(dataset: 时间序列数据集, allowed_encoder_known_variable_names: List[str] = None, **kwargs)[来源]#
从数据集中创建模型。
- Parameters:
dataset – 时间序列数据集
allowed_encoder_known_variable_names – 允许在编码器中使用的已知变量列表,默认为所有
**kwargs – 额外的参数,例如模型的超参数(见
__init__()
)
- Returns:
DeepAR 网络
- predict(data: DataLoader | DataFrame | 时间序列数据集, mode: str | Tuple[str, str] = 'prediction', return_index: bool = False, return_decoder_lengths: bool = False, batch_size: int = 64, num_workers: int = 0, fast_dev_run: bool = False, return_x: bool = False, return_y: bool = False, mode_kwargs: Dict[str, Any] = None, trainer_kwargs: Dict[str, Any] | None = None, write_interval: Literal['batch', 'epoch', 'batch_and_epoch'] = 'batch', output_dir: str | None = None, n_samples: int = 100, **kwargs) 预测 [来源]#
预测数据加载器
- Parameters:
dataloader – 数据加载器、数据框或数据集
mode – 可以是“prediction”、“quantiles”、“samples”或“raw”中的一个,或者是元组
("raw", output_name)
,其中 output_name 是由forward()
返回的字典中的一个名称return_index – 如果返回预测索引(与输出的顺序相同,即数据框的行对应于输出的第一维,给定的时间索引是第一个预测的时间索引)
return_decoder_lengths – 是否返回解码器长度(与输出顺序相同)
batch_size – 数据加载器的批量大小 - 仅在未传递数据加载器时使用
num_workers – dataloader 的工作线程数量 - 仅在未传递 dataloader 时使用
fast_dev_run – 是否仅返回第一批的结果
show_progress_bar – 是否显示进度条。默认为 False。
return_x – 是否返回网络输入(与预测输出的顺序相同)
return_y – 是否返回网络目标(与预测输出的顺序相同)
mode_kwargs (字典[字符串, 任意类型]) – 用于
to_prediction()
或to_quantiles()
的关键字参数,适用于“预测”和“分位数”模式trainer_kwargs (字典[字符串, 任意], 可选) – 训练器的关键字参数
write_interval – 将预测写入磁盘的时间间隔
output_dir – 用于写入预测的目录。默认值为 None。如果设置,则函数将返回空列表。
n_samples – 要抽取的样本数量。默认为100。
- Returns:
- 如果存在
`return
参数之一, 带有字段的预测元组
prediction
,x
,y
,index
和decoder_lengths
- 如果存在
- Return type: