1. 环境

  1. conda create -n bertuse python=3.7.6
  2. conda activate bertuse

只有把Python版本降下来,才能安装对应的Tensorflow1.X版本。

2. PIP 安装

版本一定要保持一致。

  1. pip install protobuf==3.20.0 -i https://pypi.mirrors.ustc.edu.cn/simple/
  2. pip install tensorflow==1.14.0 -i https://pypi.mirrors.ustc.edu.cn/simple/
  3. pip install bert-serving-server
  4. pip install bert-serving-client

3. SHELL 开启 Bert-SERVER

进入到Google or 哈工大的Library,以Google为例:chinese_L-12_H-768_A-12
image.png

  1. bert-serving-start -model_dir D:/chinese_L-12_H-768_A-12/ -num_worker=4 -max_seq_len=50
  2. # -model_dir 预训练Lib的位置;
  3. # -num_worker 线程数,表示同时可以处理多少个并发请求
  4. # -pooling_strategy 默认是句向量;-pooling_strategy NONE是词向量
  5. # -max_seq_len 句子最大长度

开启成功后,Bert在Shell中开启监听模式:
image.png

4. Python 调用

  1. > from bert_serving.client import BertClient
  2. > bc = BertClient(ip='localhost')
  3. # Bert—Embedding
  4. > test=bc.encode(['你好','bert'])
  5. OUTPUT:array([[ 0.28940165, -0.13572706, 0.07591117, ..., -0.1409121 ,0.54629964, -0.30118048],
  6. [-0.06168841, 0.15839505, -0.5834469 , ..., 0.18099746,-0.12812363, -0.28579813]],
  7. dtype=float32)


5. Python 直接开Server

需要在bertuse中安装JUPYTER:

  1. activate bertuse
  2. pip install ipykernel ipython
  3. ipython kernel install --user --name bertuse

开启Server:

  1. from bert_serving.server.helper import get_args_parser
  2. from bert_serving.server import BertServer
  3. args = get_args_parser().parse_args(['-model_dir', 'D:/chinese_L-12_H-768_A-12/',
  4. '-pooling_strategy', 'NONE',
  5. '-max_seq_len','60'])
  6. server = BertServer(args)
  7. server.start()