文章目录

  • bert理解
  • 简单机理
  • encoder输入输出
    • 输出的结果
  • 作用
  • code(notebook)
  • 总结
  • 引用

bert理解

一句话概括, bert就是一个抽取器。输入一句话(词序列),输出抽取后的embedding序列。
再简单理解就是,它就是一个 encoder。

简单机理

我们可以用transformer在语言模型上做预训练。因为transformer是encoder-decoder结构,语言模型就只需要encoder部分就够了。BERT,利用transformer的encoder来进行预训练。

那么什么是transformer?
这是一个新的训练结构,发展历程而言就是CNN,RNN,transformer; transformer是基于attention机理发展而来。

transformer由编码器和解码器组成。编码器和解码器都是基于attention机制。如下图

什么是注意力机制,一图简单领会,后面我们单独开一篇动手实践一下

注意力机制就是,当前词的含义,必须结合结合上下文才能更好的理解。

encoder输入输出

  • 输入会加入特殊的[CLS]代表整句话的含义,可以用于分类。

  • input的词help,prince,mayuko等,一共512,这是截取的最大长度。

  • 然后经过12层的encoder

  • 最后输出的是每个token对应的embedding序列,每个token对应一个768维的向量。这个应该很好理解。

输出的结果

out = bert(xx)

 Return::obj:`tuple(torch.FloatTensor)` comprising various elements depending on the configuration (:class:`~transformers.BertConfig`) and inputs:**last_hidden_state** (:obj:`torch.FloatTensor` of shape :obj:`(batch_size, sequence_length, hidden_size)`):Sequence of hidden-states at the output of the last layer of the model.**pooler_output** (:obj:`torch.FloatTensor`: of shape :obj:`(batch_size, hidden_size)`):Last layer hidden-state of the first token of the sequence (classification token)further processed by a Linear layer and a Tanh activation function. The Linearlayer weights are trained from the next sentence prediction (classification)objective during pre-training.This output is usually *not* a good summaryof the semantic content of the input, you're often better with averaging or poolingthe sequence of hidden-states for the whole input sequence.**hidden_states** (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``config.output_hidden_states=True``):Tuple of :obj:`torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer)of shape :obj:`(batch_size, sequence_length, hidden_size)`.Hidden-states of the model at the output of each layer plus the initial embedding outputs.**attentions** (:obj:`tuple(torch.FloatTensor)`, `optional`, returned when ``config.output_attentions=True``):Tuple of :obj:`torch.FloatTensor` (one for each layer) of shape:obj:`(batch_size, num_heads, sequence_length, sequence_length)`.Attentions weights after the attention softmax, used to compute the weighted average in the self-attentionheads.

作用

有了词序列对应的embedding向量,就可以对词分类、句子向量构建,句子分类、句子相似度比较等。

code(notebook)

#%% md# bert#%%!pip install transformers#%%import torch
from transformers import BertModel, BertTokenizer#%%tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')#%%input_ids = tokenizer.encode('hello world bert!')
input_ids#%%type(input_ids)#%%ids = torch.LongTensor(input_ids)
ids#%%text = tokenizer.convert_ids_to_tokens(input_ids)
text#%%model = BertModel.from_pretrained('bert-base-uncased', output_hidden_states=True)
# Set the device to GPU (cuda) if available, otherwise stick with CPU
device = 'cuda' if torch.cuda.is_available() else 'cpu'model = model.to(device)
ids = ids.to(device)model.eval()#%%print(ids.size())
# unsqueeze IDs to get batch size of 1 as added dimension
granola_ids = ids.unsqueeze(0)
print(granola_ids.size())#%% mdIn the example below, an additional argument has been given to the model initialisation. output_hidden_states will give us more output information. By default, a BertModel will return a tuple but the contents of that tuple differ depending on the configuration of the model. When passing output_hidden_states=True, the tuple will contain (in order; shape in brackets):1. the last hidden state (batch_size, sequence_length, hidden_size)
1. the pooler_output of the classification token (batch_size, hidden_size)
1. the hidden_states of the outputs of the model at each layer and the initial embedding outputs (batch_size, sequence_length, hidden_size)#%%out = model(input_ids=granola_ids) # tuplehidden_states = out[2]
print("last hidden state:",out[0].shape) #torch.Size([1, 6, 768])
print("pooler_output of classification token:",out[1].shape)#[1,768] cls
print("all hidden_states:", len(out[2]))#%%for i, each_layer in enumerate(hidden_states):print('layer=',i, each_layer)#%%sentence_embedding = torch.mean(hidden_states[-1], dim=1).squeeze()
print(sentence_embedding)
print(sentence_embedding.size())#%%# get last four layers
last_four_layers = [hidden_states[i] for i in (-1, -2, -3, -4)]
# cast layers to a tuple and concatenate over the last dimension
cat_hidden_states = torch.cat(tuple(last_four_layers), dim=-1)
print(cat_hidden_states.size())# take the mean of the concatenated vector over the token dimension
cat_sentence_embedding = torch.mean(cat_hidden_states, dim=1).squeeze()
print(cat_sentence_embedding)
print(cat_sentence_embedding.size())

不同的emebdding组合会带来不一样的结果,参考。

利用concat的向量,最优结果。

总结

  1. 不同的层代表不同的特征含义,向量组合的实验可以证明这一点。
  2. bert就是抽取器
  3. 不同隐层输出的向量的使用是核心所在
  4. 仔细理解文中的两幅图,和样例代码。然后就是感悟了!

引用

  1. https://github.com/huggingface/transformers/issues/2986
  2. https://github.com/BramVanroy/bert-for-inference/blob/master/introduction-to-bert.ipynb
  3. https://www.cnblogs.com/gczr/p/11785930.html
  4. https://blog.csdn.net/longxinchen_ml/article/details/86533005

bert系列第一篇: bert进行embedding相关推荐

  1. Webpack系列-第一篇基础杂记

    系列文章 Webpack系列-第一篇基础杂记 Webpack系列-第二篇插件机制杂记 Webpack系列-第三篇流程杂记 前言 公司的前端项目基本都是用Webpack来做工程化的,而Webpack虽然 ...

  2. 深入理解表单脚本系列第一篇——表单对象

    前面的话 javascript最初的一个应用就是分担服务器处理表单的责任,打破处处依赖服务器的局面.尽管目前的web和javascript已经有了长足的发展,但web表单的变化并不明显.由于web表单 ...

  3. Android 系统(243)---Android进程系列第一篇---进程基础

    Android进程系列第一篇---进程基础 内容预览.png 概述: 本文主要讲解进程基础,更深入的认识有血有肉的进程,内容涉及进程控制块,信号,进程FD泄露等等.仅供参考,欢迎指正. 一.从Linu ...

  4. Base64系列第一篇 Base64介绍

    本文地址:http://blog.csdn.net/morewindows/article/details/11871429转载请标明出处,谢谢. 欢迎关注微博:http://weibo.com/Mo ...

  5. Java小白入门系列 第一篇 写在前面

    2018年8月30日  22:00:17 郑州  多云 Sue Java小白入门系列 第一篇  写在前面 写在前面: 首先声明一下,本人也是正在学Java,并不是多么专业人士,只是最近受老师的启发,所 ...

  6. 小白学习Flink系列--第一篇(知识图谱)

    小白学习Flink系列–第一篇(知识图谱) 如何学习Flink? ​ 对于一门计算机技术来说,如何快速学习上手呢?具体的逻辑是什么呢?我认为有以下几条 了解技术的应用场景 技术的基本概念,如何使用,以 ...

  7. 自己动手实现蓝牙MESH应用系列 | 第一篇:蓝牙MESH基础概念介绍

    文章目录 1. 前言 2. 概述 2.1. 蓝牙风格(Flavors) 2.2. mesh网络的动机 2.3. mesh网络中的消息传输方式 2.3.1. 以消息为中心的通信 - 发布/订阅(publ ...

  8. pytorch 指定卡1_[原创][深度][PyTorch] DDP系列第一篇:入门教程

    引言 DistributedDataParallel(DDP)是一个支持多机多卡.分布式训练的深度学习工程方法.PyTorch现已原生支持DDP,可以直接通过torch.distributed使用,超 ...

  9. 深入理解ajax系列第一篇——XHR对象

    前面的话 1999年,微软公司发布IE5,第一次引入新功能:允许javascript脚本向服务器发起HTTP请求.这个功能当时并没有引起注意,直到2004年Gmail发布和2005年Google Ma ...

最新文章

  1. 深入浅出理解锁之—— AbstractQueuedSynchronizer
  2. 误删mysql数据库密码后,如何恢复密码
  3. 页面调用系统window打印
  4. Java EE安全性API向前发展
  5. Element UI table组件源码分析
  6. linux_iptables 详解
  7. inner join 与 left join 之间的区别
  8. Xposed 插件开发(三)—— 我的 hooker 是哪里导致出错了?
  9. hdu Hike on a Graph
  10. Hybrid APP介绍
  11. Java基础案例教程_Java基础案例教程答案
  12. 绩效考核管理中的模型分析与功能设计
  13. 利用excel生成word并在其中批量插入图片和题注、文字描述等内容
  14. 网络广告中ctr是什么意思
  15. 阿里云Centos7服务器域名解析和Nginx配置
  16. ios 自架验证服务器,iOS 13-Sign In with Apple(苹果登录)APP+后端验证
  17. scaling之旅_【scaling】什么意思_英语scaling的翻译_音标_读音_用法_例句_在线翻译_有道词典...
  18. 【unity学习笔记-如何给动态的人物添加碰撞体】
  19. 图解各种数据库数据源(ODBC)配置
  20. 2020-10-16 js实现模拟双色球摇号

热门文章

  1. tail命令,实时查看日志文件
  2. 10个月接私单赚了60多万,加工资年入百万,同学:你在做灰产吗?
  3. 打开计算机 访问不到d盘,教大家电脑找不到d盘的原因及解决办法
  4. C#控件的创建、属性设置及事件注册
  5. IGMP组播加入失败原因
  6. Web前端(一)HTML超文本标记语言
  7. OTN电层的保护SNCP保护详解
  8. c语言建立可视化窗口,如何用C语言编程出可视化界面?
  9. 9+11个无版权、高清、免费图片素材网站给你!免费无版权可商用图标、图片素材,需要图片的时候可以上去看看
  10. 提升HTML5的性能体验系列之一 避免切页白屏