当前位置:网站首页>【bert】:在训练bert 语义相似的任务时,last ave state 的计算
【bert】:在训练bert 语义相似的任务时,last ave state 的计算
2022-06-11 09:49:00 【Jack_Kuo】
问题
在bert模型训练以及预测的时候,我们会使用到最后一层隐藏层的平均语义向量作为句向量,发现部分的计算方式如下:
if self.pooling == 'last-avg':
last = out.last_hidden_state.transpose(1, 2) # [batch, 768, seqlen]
return torch.avg_pool1d(last, kernel_size=last.shape[-1]).squeeze(-1) # [batch, 768]
但是这里有一个问题,我们一般输入的文本是小于或者等于seqlen的,如果等于的话,这样计算没有问题。但是如果是小于的话,这里平均到的应该是按照具体整个batch中的每行的长度来计算的,所以这里的计算方式不是最准确的。
解决
if self.pooling == 'last-avg':
token_embeddings = out.last_hidden_state
attention_mask = torch.unsqueeze(attention_mask, dim=-1)
token_embeddings = token_embeddings * attention_mask
seqlen = torch.sum(attention_mask, dim=1)
embeddings = torch.sum(token_embeddings, dim=1) / seqlen
return embeddings
边栏推荐
- ESP8266_ Get request weather forecast and JSON parsing
- Opencv image basic operation (IV) -- image feature extraction (corner detection)
- [image denoising] image denoising based on mean + median + Gauss low pass + various improved wavelet transform, including Matlab source code
- Opencv image basic operation (III) -- image feature extraction (corner detection)
- js中关键字this的理解
- ES6新增特性--箭头函数
- mysql基础学习笔记03
- js中的事件
- 【音视频】SEI简介
- 锐意进取,砥砺前行,JASMINER持续深耕品牌实力
猜你喜欢

B站到底能不能赚到钱?

VK2C22A/B抗干扰系列电表,水表段码LCD液晶驱动芯片资料(可定制DICE/COG)

Rebuilding Oracle XdB components

BeanFactory 与FactoryBean的区别

Standard dual airbags, Changan Lumin listed, starting at 48900 yuan

Understanding of the keyword this in JS

Tenthousand words thoroughly learn heap and binary tree

oracle 11g rac 磁盘组有空间无法增加数据文件?

Simulation of map and set

你对软件兼容性测试知道多少?如何选择软件兼容性测试机构?
随机推荐
基于SSM+Vue+OSS的“依伴汉服”商城设计与开发(含源码+论文+ppt+数据库)
ESP8266_接入百度物联网核心套件、使用MQTT协议通信
Interview questions: REM layout, flex layout, etc
2022 must have Chrome extension - browser plug-in to double your Internet efficiency
Can station B make money?
Monotone stack
Opencv image basic operation (III) -- image feature extraction (corner detection)
Leetcode brushing questions - hand tearing binary tree
Simulation of map and set
js基础--Date对象
What is WSGI?
Flask (IV) -- URL construction
B站到底能不能赚到钱?
Standard dual airbags, Changan Lumin listed, starting at 48900 yuan
ESP8266_ Mqtt protocol
JS foundation -- about DOM
Drink at night, 50 classic SQL questions, really fragrant~
【Objective-C】动态创建控件
「INS-30131」 安装程序验证所需的初始设置失败
转载:LinearLayout+Fragment实现下导航栏效果