将tensorflow版本的预训练模型转换成pytorch版本的

该博客介绍了如何在PyTorch中使用预训练的BERT模型,包括安装步骤、模型转换、tokenizer应用及输出词向量和句向量的过程。重点在于从tensorflow版本模型转换到PyTorch,并演示了如何利用BERT进行基本的自然语言处理任务。
摘要由CSDN通过智能技术生成

参考:pytorch_transformers使用之获取bert词向量_yecp1的博客-CSDN博客

1、安装pytorch 1.1+

pip install torch

2、安装pytorch-transformers

pip install transformers

3、下载预训练好的模型tensorflow版本,例如bert-base-chinese

哈工大中文bert-wwm:GitHub - ymcui/Chinese-BERT-wwm: Pre-Training with Whole Word Masking for Chinese BERT(中文BERT-wwm系列模型)

gooogle-bert:GitHub - google-research/bert: TensorFlow code and pre-trained models for BERT

4、转换

5、改名

【转换后的结果】

 【修改目录名称、修改文件名称、可以删除ckpt文件】

 这样就得到了转换好的模型

6、使用

import torch
from pytorch_transformers import BertTokenizer,BertModel

tokenizer = BertTokenizer.from_pretrained('transformer-bert/bert-base-chinese')
model = BertModel.from_pretrained('transformer-bert/bert-base-chinese')
input_ids = torch.tensor(tokenizer.encode("自然语言处理")).unsqueeze(0)  # Batch size 1
outputs = model(input_ids)
# last_hidden_states = outputs[0]  # The last hidden-state is the first element of the output tuple
sequence_output = outputs[0]
pooled_output = outputs[1]

print(sequence_output)
print(sequence_output.shape)    ## 字向量

print(pooled_output)      
print(pooled_output.shape)      ## 句向量

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值