【问题标题】:Extracting layer output from Classification model of SimpleTransformer从 Simple Transformer 的分类模型中提取层输出
【发布时间】:2021-02-10 05:32:21
【问题描述】:

我已经为文本分类任务微调了一个 BERT 基础模型。现在,我想提取隐藏层输出,以便将此输出与其他特征结合起来训练随机森林模型。 问题在于我不知道如何提取隐藏层输出。如果有人能在这方面帮助我,那就太好了。

from simpletransformers.classification import ClassificationModel

model_xlm = ClassificationModel('bert', 'bert-base-uncased')
model_xlm.train_model(df_train)

【问题讨论】:

    标签: python text-classification bert-language-model simpletransformers


    【解决方案1】:

    在 Google 搜索中遇到了这个问题,最近通过阅读 SimpleTransformers git repo 中的源代码找到了答案。

    要获得隐藏层输出,只需将 output_hidden_​​states 传入模型:

    model_xlm = ClassificationModel('bert', 'bert-base-uncased', {"output_hidden_states": True})
    

    那么当你调用 predict 时,你会得到这个:

    preds, model_outputs, all_embedding_outputs, all_layer_hidden_states = model.predict(data)
    

    这里是源中的参考: https://github.com/ThilinaRajapakse/simpletransformers/blob/68f0faace15530fa1a738a34ea13521eec4518b1/simpletransformers/classification/classification_model.py#L2242

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-18
      • 2017-05-25
      • 2019-01-27
      • 2021-03-21
      • 2023-01-10
      • 1970-01-01
      • 1970-01-01
      • 2021-04-07
      相关资源
      最近更新 更多