【问题标题】:Extracting Features from BertForSequenceClassification从 BertForSequenceClassification 中提取特征
【发布时间】:2021-06-23 13:14:43
【问题描述】:

大家好,目前我正在尝试开发一种用于矛盾检测的模型。使用和微调 BERT 模型我已经得到了相当可观的结果,但我认为使用其他一些功能可以获得更好的准确性。我把自己定位在这个Tutorial 上。经过微调,我的模型如下所示:

==== Embedding Layer ====

bert.embeddings.word_embeddings.weight                  (30000, 768)
bert.embeddings.position_embeddings.weight                (512, 768)
bert.embeddings.token_type_embeddings.weight                (2, 768)
bert.embeddings.LayerNorm.weight                              (768,)
bert.embeddings.LayerNorm.bias                                (768,)

==== First Transformer ====

bert.encoder.layer.0.attention.self.query.weight          (768, 768)
bert.encoder.layer.0.attention.self.query.bias                (768,)
bert.encoder.layer.0.attention.self.key.weight            (768, 768)
bert.encoder.layer.0.attention.self.key.bias                  (768,)
bert.encoder.layer.0.attention.self.value.weight          (768, 768)
bert.encoder.layer.0.attention.self.value.bias                (768,)
bert.encoder.layer.0.attention.output.dense.weight        (768, 768)
bert.encoder.layer.0.attention.output.dense.bias              (768,)
bert.encoder.layer.0.attention.output.LayerNorm.weight        (768,)
bert.encoder.layer.0.attention.output.LayerNorm.bias          (768,)
bert.encoder.layer.0.intermediate.dense.weight           (3072, 768)
bert.encoder.layer.0.intermediate.dense.bias                 (3072,)
bert.encoder.layer.0.output.dense.weight                 (768, 3072)
bert.encoder.layer.0.output.dense.bias                        (768,)
bert.encoder.layer.0.output.LayerNorm.weight                  (768,)
bert.encoder.layer.0.output.LayerNorm.bias                    (768,)

==== Output Layer ====

bert.pooler.dense.weight                                  (768, 768)
bert.pooler.dense.bias                                        (768,)
classifier.weight                                           (2, 768)
classifier.bias                                                 (2,)

我的下一步是从该模型中获取 [CLS] 令牌,将其与一些手工制作的特征结合起来,并将它们输入到不同的模型 (MLP) 中进行分类。任何提示如何做到这一点?

【问题讨论】:

    标签: python nlp bert-language-model huggingface-transformers


    【解决方案1】:

    您可以使用 bert 模型的池化输出(馈入池化层的 [CLS] 令牌的上下文嵌入):

    from transformers import BertModel, BertTokenizer
    
    #replace bert-base-uncased with the path to your saved model
    t = BertTokenizer.from_pretrained('bert-base-uncased')
    m = BertModel.from_pretrained('bert-base-uncased')
    
    
    i = t.batch_encode_plus(['this is a sample', 'different sample'], padding=True,return_tensors='pt')
    o = m(**i)
    
    print(o.keys())
    #shape [batch_size, 768]
    print(o.pooler_output.shape)
    useMe = o.pooler_output
    

    【讨论】:

    • 这失败了:'SequenceClassifierOutput' 对象没有属性 'pooler_output' 我认为这仅适用于基本 Bert 模型,不适用于 BertForSequenceClassification
    • @Klaas 这是正确的,因为SequenceClassifierOutput 不会导出 pooler_output。您可以使用 BertModel 类加载您的模型,也可以分叉 BertForSequenceClassification。
    • 保存我的预训练模型并将其加载为普通BertModel 似乎得到了所需的输出,谢谢!您能否详细说明分叉 BertForSequenceClassification 的含义?
    • 您只需从它继承并定义您自己的输出 pooled_output 的 forward 方法。
    猜你喜欢
    • 1970-01-01
    • 2016-12-13
    • 1970-01-01
    • 2016-10-11
    • 2020-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多