You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行HuggingFace Informer示例代码遇矩阵维度不匹配RuntimeError

问题描述

运行HuggingFace Informer极简示例代码时出现矩阵维度不匹配错误,代码如下:

from transformers import InformerConfig, InformerModel
from huggingface_hub import hf_hub_download
import torch

# Initializing an Informer configuration with 12 time steps for prediction
configuration = InformerConfig(prediction_length=12)

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

# Accessing the model configuration
configuration = model.config

file = hf_hub_download(
    repo_id="kashif/tourism-monthly-batch", filename="train-batch.pt", repo_type="dataset"
)
batch = torch.load(file)

model = InformerModel.from_pretrained("huggingface/informer-tourism-monthly")

# during training, one provides both past and future values
# as well as possible additional features
outputs = model(
    past_values=batch["past_values"],
    past_time_features=batch["past_time_features"],
    past_observed_mask=batch["past_observed_mask"],
    static_categorical_features=batch["static_categorical_features"],
    static_real_features=batch["static_real_features"],
    future_values=batch["future_values"],
    future_time_features=batch["future_time_features"],
)

last_hidden_state = outputs.last_hidden_state

报错信息:

Traceback (most recent call last):
  File "D:\data\test.py", line 25, in <module>
    outputs = model(
  File "C:\Users\LSTM\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\nn\modules\module.py", line 1190, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:\Users\LSTM\AppData\Local\Programs\Python\Python310\lib\site-packages\transformers\models\informer\modeling_informer.py", line 1711, in forward
    encoder_outputs = self.encoder(
  File "C:\Users\LSTM\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\nn\modules\module.py", line 1190, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:\Users\LSTM\AppData\Local\Programs\Python\Python310\lib\site-packages\transformers\models\informer\modeling_informer.py", line 1180, in forward
    hidden_states = self.value_embedding(inputs_embeds)
  File "C:\Users\LSTM\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\nn\modules\module.py", line 1190, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:\Users\LSTM\AppData\Local\Programs\Python\Python310\lib\site-packages\transformers\models\informer\modeling_informer.py", line 307, in forward
    return self.value_projection(x)
  File "C:\Users\LSTM\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\nn\modules\module.py", line 1190, in _call_impl
    return forward_call(*input, **kwargs)
  File "C:\Users\LSTM\AppData\Local\Programs\Python\Python310\lib\site-packages\torch\nn\modules\linear.py", line 114, in forward
    return F.linear(input, self.weight, self.bias)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (1536x23 and 22x32)
问题原因分析
  • 预训练模型与输入数据特征维度不匹配:报错显示矩阵乘法时1536x23与22x32无法相乘,说明加载的train-batch.pt数据输入特征维度为23,而预训练模型huggingface/informer-tourism-monthly的线性层是基于22维输入设计的,两者维度冲突导致计算失败。
  • 冗余代码易引发混淆:代码中先初始化了随机配置的Informer模型,随后又加载预训练模型,这部分冗余代码虽不直接触发错误,但会干扰对模型配置的判断,建议删除。

内容的提问来源于stack exchange,提问作者ah bon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 13:20:33