gpu版
pip install torch===1.5.0 torchvision===0.6.0 -f https://download.pytorch.org/whl/torch_stable.html
cpu版
pip install torch==1.5.0+cpu torchvision==0.6.0+cpu -f https://download.pytorch.org/whl/torch_stable.html
离线安装
或者将whl文件放到localpath目录下 运行 pip install --no-index --find-links=/localpath torch torchvision
清华镜像下载(解压,将对应文件复制到python安装目录下的Lib/site-packages 和Scripts,再安装 python依赖包:pyyaml mkl cmake cffi 否则会报错)
1.4版本加载模型出错segmentation fault,官方的1.5版本不支持cuda10.0,公司的环境只能用cuda10.0所以自己编译了一遍
注意:一定要完全按照git文档的命令行去操作,不能跳过,否则会报错。
bert wwm,Chinese pytorch版本 讯飞云下载链接
提取密码:hteX
ps: 可换成 bert-wwm-ext 或其他模型
下载的模型解压到D:/XXX/chinese-bert_chinese_wwm_pytorch
data目录下文件改名:
bert_config.json 改名为 config.json
chinese_wwm_pytorch.bin 改名为 pytorch_model.bin
使用
from transformers import BertTokenizer,BertModel
# tokenizer = BertTokenizer.from_pretrained("hfl/chinese-bert-wwm-ext")
# model = BertModel.from_pretrained("hfl/chinese-bert-wwm-ext")
tokenizer = BertTokenizer.from_pretrained("D:/XXX/chinese-bert_chinese_wwm_pytorch/data")
model = BertModel.from_pretrained("D:/XXX/chinese-bert_chinese_wwm_pytorch/data")
def use_bert_wwm():
text = "[CLS] 李明是做什么的?[SEP] 李明是个老师。[SEP]"
tokenized_text = tokenizer.tokenize(text)
print(tokenized_text)
input_ids = torch.tensor([tokenizer.encode("一壶浊酒喜相逢", add_special_tokens=True)]) # Add special tokens takes care of adding [CLS], [SEP], <s>... tokens in the right way for each model.
with torch.no_grad():
last_hidden_states = model(input_ids)[0] # Models outputs are now tuples
print(last_hidden_states)