目录
安装torch
pytorch-pretrained-bert简单使用
运行Pycharm中的代码时候提示ModuleNotFoundError: No module named ‘torch’。试了很多种方法都不行,然后进入官网查了下具体的安装方法,附上网址https://pytorch.org/get-started/previous-versions/。
摘取一段放在这里供大家参考。
# CUDA 10.0
pip install torch===1.2.0 torchvision===0.4.0 -f https://download.pytorch.org/whl/torch_stable.html
# CUDA 9.2
pip install torch==1.2.0+cu92 torchvision==0.4.0+cu92 -f https://download.pytorch.org/whl/torch_stable.html
# CPU only
pip install torch==1.2.0+cpu torchvision==0.4.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
从下载模型权重开始
# 切换到你的anaconda gpu 环境
# source activate 你的conda环境名称
# 安装加载预训练模型&权重的包
pip install pytorch-pretrained-bert
接着就是下载模型权重文件了,pytorch-pretrained-bert官方下载地址太慢了…,推荐去kaggle下载L-12_H-768-A-12 uncase版本,下载地址在这里,里面有两个文件,都下载下来,并把模型参数权重的文件bert-base-uncased解压出来,然后放在你熟悉的硬盘下即可。
加载模型试试
from pytorch_pretrained_bert import BertModel, BertTokenizer
import numpy as np
import torch
# 加载bert的分词器
tokenizer = BertTokenizer.from_pretrained('E:/Projects/bert-pytorch/bert-base-uncased-vocab.txt')
# 加载bert模型,这个路径文件夹下有bert_config.json配置文件和model.bin模型权重文件
bert = BertModel.from_pretrained('E:/Projects/bert-pytorch/bert-base-uncased/')
s = "I'm not sure, this can work, lol -.-"
tokens = tokenizer.tokenize(s)
print("\\".join(tokens))
# "i\\'\\m\\not\\sure\\,\\this\\can\\work\\,\\lo\\##l\\-\\.\\-"
# 是否需要这样做?
# tokens = ["[CLS]"] + tokens + ["[SEP]"]
ids = torch.tensor([tokenizer.convert_tokens_to_ids(tokens)])
print(ids.shape)
# torch.Size([1, 15])
result = bert(ids, output_all_encoded_layers=True)
print(result)
没问题,那么bert返回给我们了什么呢?
result = (
[encoder_0_output, encoder_1_output, ..., encoder_11_output],
pool_output
)
output_all_encoded_layers=True
,12层Transformer的结果全返回了,存在第一个列表中,每个encoder_output的大小为[batch_size, sequence_length, hidden_size]
;[batch_size, hidden_size]
,pooler层的输出在论文中描述为:CLS
) to train on the Next-Sentence task (see BERT’s paper).[batch_size, sequence_length, hidden_size]
的张量,可以看作bert对于这句话的表示。用bert微调我们的模型
将bert嵌入我们的模型即可。
class CustomModel(nn.Module):
def __init__(self, bert_path, n_other_features, n_hidden):
super().__init__()
# 加载并冻结bert模型参数
self.bert = BertModel.from_pretrained(bert_path)
for param in self.bert.parameters():
param.requires_grad = False
self.output = nn.Sequential(
nn.Dropout(0.2),
nn.Linear(768 + n_other_features, n_hidden),
nn.ReLU(),
nn.Linear(n_hidden, 1)
)
def forward(self, seqs, features):
_, pooled = self.bert(seqs, output_all_encoded_layers=False)
concat = torch.cat([pooled, features], dim=1)
logits = self.output(concat)
return logits
测试:
s = "I'm not sure, this can work, lol -.-"
tokens = tokenizer.tokenize(s)
ids = torch.tensor([tokenizer.convert_tokens_to_ids(tokens)])
# print(ids)
# tensor([[1045, 1005, 1049, 2025, 2469, 1010, 2023, 2064, 2147, 1010, 8840, 2140,
# 1011, 1012, 1011]])
model = CustomModel('你的路径/bert-base-uncased/',10, 512)
outputs = model(ids, torch.rand(1, 10))
# print(outputs)
# tensor([[0.1127]], grad_fn=)