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

HuBERT微调报错:'PretrainedConfig'无'feat_proj_layer_norm'属性

HuBERT微调时AttributeError错误的原因与修复方案

问题场景

我在Crema-D数据集上微调HuBERT模型做音频分类时,运行以下代码出现错误:

NUM_LABELS = 6
model_id = "facebook/hubert-base-ls960"

config = PretrainedConfig.from_pretrained(model_id, num_labels=NUM_LABELS)
hubert_model = HubertForSequenceClassification.from_pretrained(
    model_id,
    config=config,  # 按照数据集更新类别数
    ignore_mismatched_sizes=True,  # 避免预训练分类器尺寸不匹配
)

错误信息

You are using a model of type hubert to instantiate a model of type . This is not supported for all configurations of models and can yield errors. Downloading pytorch_model.bin: 100% 378M/378M [00:01<00:00, 250MB/s]
--------------------------------------------------------------------------- AttributeError                            Traceback (most recent call last) Cell In[7], line 5
      2 model_id = "facebook/hubert-base-ls960"
      4 config = PretrainedConfig.from_pretrained(model_id, num_labels=NUM_LABELS)
----> 5 hubert_model = HubertForSequenceClassification.from_pretrained(
      6     model_id,
      7     config=config,  # because we need to update num_labels as per our dataset
      8     ignore_mismatched_sizes=True,  # to avoid classifier size mismatch from from_pretrained.
      9 )

File /opt/conda/lib/python3.10/site-packages/transformers/modeling_utils.py:2629, in PreTrainedModel.from_pretrained(cls, pretrained_model_name_or_path,
*model_args, **kwargs)    2626     init_contexts.append(init_empty_weights())    2628 with ContextManagers(init_contexts):
-> 2629     model = cls(config, *model_args, **model_kwargs)    2631 # Check first if we are `from_pt`    2632 if use_keep_in_fp32_modules:

File /opt/conda/lib/python3.10/site-packages/transformers/models/hubert/modeling_hubert.py:1235, in HubertForSequenceClassification.__init__(self, config)    1231 if hasattr(config, "add_adapter") and config.add_adapter:    1232     raise ValueError(    1233         "Sequence classification does not support the use of Hubert adapters (config.add_adapter=True)"    1234  )
-> 1235 self.hubert = HubertModel(config)    1236 num_layers = config.num_hidden_layers + 1  # transformer layers + input embeddings  1237 if config.use_weighted_layer_sum:

File /opt/conda/lib/python3.10/site-packages/transformers/models/hubert/modeling_hubert.py:959, in HubertModel.__init__(self, config)
    957 self.config = config
    958 self.feature_extractor = HubertFeatureEncoder(config)
-> 959 self.feature_projection = HubertFeatureProjection(config)
    961 if config.mask_time_prob > 0.0 or config.mask_feature_prob > 0.0:
    962     self.masked_spec_embed = nn.Parameter(torch.FloatTensor(config.hidden_size).uniform_())

File /opt/conda/lib/python3.10/site-packages/transformers/models/hubert/modeling_hubert.py:376, in HubertFeatureProjection.__init__(self, config)
    374 def __init__(self, config):
    375     super().__init__()
-> 376     self.feat_proj_layer_norm = config.feat_proj_layer_norm
    377     if self.feat_proj_layer_norm:
    378         self.layer_norm = nn.LayerNorm(config.conv_dim[-1], eps=config.layer_norm_eps)

File /opt/conda/lib/python3.10/site-packages/transformers/configuration_utils.py:260, in PretrainedConfig.__getattribute__(self, key)
    258 if key != "attribute_map" and key in super().__getattribute__("attribute_map"):
    259     key = super().__getattribute__("attribute_map")[key]
-> 260 return super().__getattribute__(key)

AttributeError: 'PretrainedConfig' object has no attribute 'feat_proj_layer_norm'

错误原因

你使用了通用的PretrainedConfig类加载HuBERT的配置,而非HuBERT专属的HubertConfig。通用配置类不包含HuBERT模型特有的参数(比如feat_proj_layer_norm),导致后续模型初始化时找不到这些必填属性,触发AttributeError。

修复方案

方案一:使用Hubert专属配置类

直接替换PretrainedConfig为HubertConfig,确保加载到HuBERT的完整配置参数:

from transformers import HubertConfig, HubertForSequenceClassification

NUM_LABELS = 6
model_id = "facebook/hubert-base-ls960"

config = HubertConfig.from_pretrained(model_id, num_labels=NUM_LABELS)
hubert_model = HubertForSequenceClassification.from_pretrained(
    model_id,
    config=config,
    ignore_mismatched_sizes=True,
)

方案二:简化写法(推荐)

无需手动创建配置对象,直接在from_pretrained方法中指定num_labels参数,框架会自动加载对应模型的专属配置:

from transformers import HubertForSequenceClassification

NUM_LABELS = 6
model_id = "facebook/hubert-base-ls960"

hubert_model = HubertForSequenceClassification.from_pretrained(
    model_id,
    num_labels=NUM_LABELS,
    ignore_mismatched_sizes=True,
)

内容的提问来源于stack exchange,提问作者Nikolai Reverger

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 23:25:00