赞
踩
使用了肖涵博士的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
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
(下载好的中文模型路径,num_worker的数量可以自行选择)
最后,在pycharm编写如下代码:
from bert_serving.client import BertClient
bc = BertClient()
print(bc.encode(['北京'],['上海']))
这样就可以获取到词向量了!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。