赞
踩
花了很多时间在版本对照上面,不然总是报这样或者那样的错误,版本总结如下:
Windows:Win10
Anaconda:直接官网下载最新版本
conda 23.7.4
Python:3.7.12
protobuf:3.19.0
TensorFlow:1.13.1
版本对照文章参考:
Tensorflow与Python、CUDA、cuDNN的版本对应表
如果某些命令执行失败,提示如“由于目标计算机积极拒绝,无法连接。”等信息,那就多试几次,我也是折腾了一天才弄好的,耐心点,相信自己你可以的。每个人情况可能不一样,我将我遇到的问题提前规避出来,避免大家少踩坑。
另外提醒,文件夹不要以中文名称命名,防止编码错误!
直接官网下载最新版本,此处不做过多介绍
首先添加channels,这个是很多文章没有写的,导致国内去下载经常超时报错或如下错误
The following packages are not availablm current channels
查看当前conda配置
conda config --show
添加频道
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
以下步骤参照其他博文操作,原文地址为windows+Anaconda环境下安装BERT成功安装方法及问题汇总,感谢这位老哥,主要操作均是从这篇文章总结的
创建一个bert的虚拟环境,且python版本选择3.7.12,如果失败多试几次
conda create --name bert python=3.7.12
切换到创建的虚拟环境
conda activate bert
安装依赖包
pip install bert-serving-server -i https://pypi.tuna.tsinghua.edu.cn/simple #服务端
pip install bert-serving-client -i https://pypi.tuna.tsinghua.edu.cn/simple # 客户端
卸载当前高版本protobuf,不然之后运行bert会报错
pip uninstall protobuf
安装低版本
pip install protobuf==3.19.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
安装低版本TensorFlow,不然启动bert也会报错,降低到TensorFlow==1.*版本
下载TensorFlow1. 的.whl安装文件:tensorflow · PyPI
安装刚下载的TensorFlow,如果失败多试几次
pip install tensorflow-1.13.1-cp37-cp37m-win_amd64.whl
下载并解压预训练模型文件BERT-Base, Chinese
进入解压的文件目录下,启动服务
bert-serving-start -model_dir ./chinese_L-12_H-768_A-12/ -num_worker=2
等待一会,日志显示如下,启动成功:
I:[33mWORKER-0[0m:use device cpu, load graph from C:\Users\ZESHEN~1.ZHO\AppData\Local\Temp\tmp8cprkdy_
I:[33mWORKER-1[0m:use device cpu, load graph from C:\Users\ZESHEN~1.ZHO\AppData\Local\Temp\tmp8cprkdy_
I:[33mWORKER-1[0m:ready and listening!
I:[33mWORKER-0[0m:ready and listening!
I:[35mVENTILATOR[0m:all set, ready to serve request!
新建文件BertClient.py,并将其放在模型文件所在的目录:
from bert_serving.client import BertClient
bc = BertClient()
value = bc.encode(['古时候', '以前的时候'])
print(value)
刚刚启动的是服务端,不要动,再启动另一个Anaconda prompt,切换到刚刚创建的bert虚拟环境,运行:
python BertClient.py
服务端显示如下:
I:[35mVENTILATOR[0m:new config request req id: 1 client: b'62ea6795-9573-406f-91bc-ea7d601e98c6'
I:[32mSINK[0m:send config client b'62ea6795-9573-406f-91bc-ea7d601e98c6'
I:[35mVENTILATOR[0m:new encode request req id: 2 size: 2 client: b'62ea6795-9573-406f-91bc-ea7d601e98c6'
I:[32mSINK[0m:job register size: 2 job id: b'62ea6795-9573-406f-91bc-ea7d601e98c6#2'
I:[33mWORKER-0[0m:new job socket: 0 size: 2 client: b'62ea6795-9573-406f-91bc-ea7d601e98c6#2'
I:[33mWORKER-0[0m:job done size: (2, 768) client: b'62ea6795-9573-406f-91bc-ea7d601e98c6#2'
I:[32mSINK[0m:collect b'EMBEDDINGS' b'62ea6795-9573-406f-91bc-ea7d601e98c6#2' (E:2/T:0/A:2)
I:[32mSINK[0m:send back size: 2 job id: b'62ea6795-9573-406f-91bc-ea7d601e98c6#2'
客户端显示如下:
(bert) E:\python\chinese_L-12_H-768_A-12>python BertClient.py
[[-0.51603067 0.35779792 -0.11559596 ... -0.32379794 0.2381701
-0.07825265]
[-0.4484791 1.3458204 0.2654073 ... -0.09895833 0.34069267
-0.7328518 ]]
至此,打完收工!感谢您的浏览!
如果您对技术有兴趣,友好交流,可以加v进技术群一起沟通,v:zzs1067632338,备注csdn即可
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。