【huggingface bertmodel类使用理解】

关于在BertModel.from_pretrained 之后如何取bert的输出的理解,参考了huggingface的文档:

先给一个手写图:
请添加图片描述

需要注意的是:pooler_output 和 last_hidden_state [0] 不相等。前者是后者经过一个MLP层得来,这个MLP层是在预训练时经过NSP任务的微调。

所以如果使用单文本分类任务,使用 last_hidden_state [0] 再自己加一个 MLP 层也许更为合适。

引用huggingface文档:

pooler_output (torch.FloatTensor of shape (batch_size, hidden_size)) — Last layer hidden-state of the first token of the sequence (classification token) after further processing through the layers used for the auxiliary pretraining task. E.g. for BERT-family of models, this returns the classification token after processing through a linear layer and a tanh activation function. The linear layer weights are trained from the next sentence prediction (classification) objective during pretraining

参考SimCSE代码的写法:

请添加图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值