当前位置:   article > 正文

文本向量—使用bert预训练模型获取中文的词向量_anaconda python 中文向量 类预训练语言模型

anaconda python 中文向量 类预训练语言模型

使用了肖涵博士的bert-as-service,详情请参考https://github.com/hanxiao/bert-as-service

1、使用的环境python=3.6,tensorflow=1.13.2,tensorflow版本不能太高,2.x不支持bert-as-service。
安装需要的包:

pip install bert-serving-server
pip install bert-serving-client
  • 1
  • 2

2、同时还需要下载训练好的Bert中文模型:https://storage.googleapis.com/bert_models/2018_11_03/chinese_L-12_H-768_A-12.zip
3、然后在终端命令进入bert-serving-start.exe所在的文件夹,这个一般在python安装路径下的Scripts文件夹下,因为我使用的是anaconda的python,所以我的在/home/usrsvc/anaconda3/envs/bertVector/bin这个文件夹下。
4、命令行启动bert-as-service:
在这里插入图片描述

bert-serving-start -model_dir /data/bert_chinese_pretrainmodel/chinese_L-12_H-768_A-12 -num_worker=2 > /data/userhome/leijin/log/bert-log/bert.log
  • 1

(下载好的中文模型路径,num_worker的数量可以自行选择)
最后,在pycharm编写如下代码:

from bert_serving.client import BertClient
bc = BertClient()
print(bc.encode(['北京'],['上海']))
  • 1
  • 2
  • 3

这样就可以获取到词向量了!

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/774138?site
推荐阅读
相关标签
  

闽ICP备14008679号