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

微调后的ConvBERT模型与原模型行为是否一致?调用transformers-interpret报错

问题

我正在对模型ConvBertModel.from_pretrained("sarnikowski/convbert-medium-small-da-cased")进行多标签情感分类任务微调。训练完成后,通过以下代码加载微调后的模型:

# 加载验证损失最优的模型版本
trained_model = Tagger.load_from_checkpoint(
  trainer.checkpoint_callback.best_model_path,
  n_classes=3
)
trained_model.eval()
trained_model.freeze()

我原本认为trained_model仅权重更新,架构与微调前的模型一致,但用它执行其他任务时持续报错。比如使用transformers-interpret库获取预测SHAP值时:

from transformers_interpret import MultiLabelClassificationExplainer

model = trained_model # 我的微调后模型
tokenizer = ConvBertTokenizer.from_pretrained("sarnikowski/convbert-medium-small-da-cased")

cls_explainer = MultiLabelClassificationExplainer(model, tokenizer)

word_attributions = cls_explainer("There were many aspects of the film I liked, but it was frightening and gross in parts. My parents hated it.")

报错信息如下:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-40-0000c765e477> in <module>
      2 cls_explainer = MultiLabelClassificationExplainer(
      3     trained_model,
----> 4     tokenizer
      5     )
      6 

3 frames
/usr/local/lib/python3.7/dist-packages/transformers_interpret/explainers/multilabel_classification.py in __init__(self, model, tokenizer, attribution_type, custom_labels)
     35         custom_labels: Optional[List[str]] = None,
     36     ):
---&gt; 37         super().__init__(model, tokenizer, attribution_type, custom_labels)
     38         self.labels = []
     39 

/usr/local/lib/python3.7/dist-packages/transformers_interpret/explainers/sequence_classification.py in __init__(self, model, tokenizer, attribution_type, custom_labels)
     51             AttributionTypeNotSupportedError:
     52         """
---&gt; 53         super().__init__(model, tokenizer)
     54         if attribution_type not in SUPPORTED_ATTRIBUTION_TYPES:
     55             raise AttributionTypeNotSupportedError(

/usr/local/lib/python3.7/dist-packages/transformers_interpret/explainer.py in __init__(self, model, tokenizer)
     17         self.tokenizer = tokenizer
     18 
---&gt; 19         if self.model.config.model_type == "gpt2":
     20             self.ref_token_id = self.tokenizer.eos_token_id
     21         else:

/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in __getattr__(self, name)
   1206                 return modules[name]
   1207         raise AttributeError("'{}' object has no attribute '{}'".format(
-&gt; 1208             type(self).__name__, name))
   1209 
   1210     def __setattr__(self, name: str, value: Union[Tensor, 'Module']) -&gt; None:

AttributeError: 'Tagger' object has no attribute 'config'

我的理解是否错误?微调后的模型是否与原模型行为不一致?

解答

你的理解存在偏差,问题核心并非微调后的模型行为不一致,而是你加载的trained_model并非原生的ConvBertModel,而是自定义的Tagger类实例——这个类应该是基于PyTorch Lightning封装的模型,它内部包含ConvBertModel作为子模块,但本身没有直接暴露config属性,而transformers-interpret库要求模型直接具备config属性来识别模型类型。

解决方法如下:

  • 提取Tagger内部的预训练模型核心模块,传给MultiLabelClassificationExplainer即可。假设你的Tagger类中将ConvBertModel命名为model(这是此类封装的常见命名方式),代码可修改为:
cls_explainer = MultiLabelClassificationExplainer(trained_model.model, tokenizer)
  • 若不确定内部子模块的名称,可通过print(trained_model.named_children())查看模型结构,找到对应的预训练模型模块。

微调后的ConvBertModel权重已正确更新,只是封装后的Tagger类不符合transformers-interpret的接口要求,并非模型本身行为出现异常。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 16:20:59