近期想用Ernie获取字向量,但用PaddlePaddle没整出来,然后发现这个好工具。
https://github.com/nghuyong/ERNIE-Pytorch
用这个工具可以将Ernie转换为Pytorch能够使用的版本,像Bert一样使用Ernie。
其关键代码如下:
extract_and_convert('./Ernie1.0/model/', './convert')
tokenizer = BertTokenizer.from_pretrained('./convert')
model = BertModel.from_pretrained('./convert')
input_ids = torch.tensor([tokenizer.encode("hello", add_special_tokens=True)])
with torch.no_grad():
pooled_output = model(input_ids)[1]
print(pooled_output.numpy())