Pytorch版本的BERT使用學習筆記(新版)

前言:

coder們最常用的Pytorch版本的BERT應該就是這一份了吧 https://github.com/huggingface/pytorch-pretrained-BERT 

這份是剛出BERT的時候出的,暫且叫它舊版

這是博主在學習使用舊版的時候粗略記過的一些筆記:https://blog.csdn.net/ccbrid/article/details/88732857

隨着BERT的出現,更多的預訓練模型(BERT, GPT-2, RoBERTa, XLM, DistilBert, XLNet, CTRL...)也如雨後春筍般涌出,Hugging Face的這款BERT工具也在不管的更新迭代

本文是針對於新版的學習👇

0、BERT相關網址

BERT論文:https://arxiv.org/abs/1810.04805

Google_BERT代碼(tensorflow):https://github.com/google-research/bert 

Pytorch版本的BERT:https://github.com/huggingface/transformers(本文記錄該工具的使用)

該工具使用文檔:https://huggingface.co/transformers/

BERT部分使用文檔https://huggingface.co/transformers/model_doc/bert.html#bertmodel

快速瀏覽版:https://github.com/huggingface/transformers#quick-tour

1、安裝步驟

1. 要求Python 3.5+, PyTorch 1.0.0+ 或 TensorFlow 2.0.0-rc1

2. 推薦使用虛擬環境 例如:

conda create -n transformers python=3.6

source activate transformers

(conda env list 查看已有虛擬環境)

3. 首先 you need to install one of, or both, TensorFlow 2.0 and PyTorch. (本文PyTorch)

安裝PyTorch可訪問網址:https://pytorch.org/get-started/locally/#start-locally

在該網址上選擇對應的安裝環境,可以直接得到如下命令:

conda install pytorch torchvision cudatoolkit=10.1 -c pytorch

由於博主本機cuda不支持1.4的torch,所以重新安裝

pip install torch==1.2.0 torchvision==0.4.0 -f https://download.pytorch.org/whl/torch_stable.html

4. pip install transformers

5. 下到本地方便查看源碼(此步可省略)git clone https://github.com/huggingface/transformers.git

2、使用方法

因爲我們知道,

cls是一個,sep是大於等於一個。

'[CLS]'必須出現在樣本段落的開頭,一個段落可以有一句話也可以有多句話,每句話的結尾必須是'[SEP]'。

例如:

['[CLS]', 'this', 'is', 'blue', '[SEP]', 'that', 'is', 'red', '[SEP]']

 

 

 

 

 

 

我的實驗:

1. BERT

2. BERT+CVAE

我的實驗數據:

1. AMI

2. xunfei_dia2W

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章