python3.7.3版本下安装tensorflow默认2.0.0,不支持tensorflow1.10.0,而暂时bert-serving-server不支持tensorflow2.0.0,建议tensorflow1.10.0版本,python3.5。
不然会报错:
TypeError: ‘NoneType’ object is not iterable #377
fail to optimize the graph!
附github链接
启动出错其他问题可查询链接
创建虚拟环境(建议PYTHON3.5版本)
conda create --name py3.5 python=3.5
conda install --channel https://conda.anaconda.org/anaconda tensorflow=1.10.0
1、使用 PIP 命令进行安装
pip install bert-serving-server # 服务端
pip install bert-serving-client # 客户端
2、下载bert预训练模型
模型下载链接
3、启动bert服务
bert-serving-start -model_dir /tmp/english_L-12_H-768_A-12/ -num_worker=2 //模型路径自改
或
建一个服务端的代码
# -*- coding: utf-8 -*-
from bert_serving.server import BertServer
from bert_serving.server.helper import get_args_parser
def main():
args = get_args_parser().parse_args(['-model_dir', r'C:\project\bert-as-service\chinese_L-12_H-768_A-12',
'-port', '86500',
'-port_out', '86501',
'-max_seq_len', '512',
'-mask_cls_sep',
'-cpu'])
bs = BertServer(args)
bs.start()
if __name__ == "__main__":
客户端代码
# 导入bert客户端
from bert_serving.client import BertClient
import numpy as np
class SimilarModel:
def __init__(self):
# ip默认为本地模式,如果bert服务部署在其他服务器上,修改为对应ip
self.bert_client = BertClient(ip='192.168.x.x')
def close_bert(self):
self.bert_client .close()
def get_sentence_vec(self,sentence):
'''
根据bert获取句子向量
:param sentence:
:return:
'''
return self.bert_client .encode([sentence])[0]
def cos_similar(self,sen_a_vec, sen_b_vec):
'''
计算两个句子的余弦相似度
:param sen_a_vec:
:param sen_b_vec:
:return:
'''
vector_a = np.mat(sen_a_vec)
vector_b = np.mat(sen_b_vec)
num = float(vector_a * vector_b.T)
denom = np.linalg.norm(vector_a) * np.linalg.norm(vector_b)
cos = num / denom
return cos
if __name__=='__main__':
# 从候选集condinates 中选出与sentence_a 最相近的句子
condinates = ['machine ','computer science','dicision tree']
sentence_a = 'machine learning'
bert_client = SimilarModel()
max_cos_similar = 0
most_similar_sentence = ''
for sentence_b in condinates:
sentence_a_vec = bert_client .get_sentence_vec(sentence_a)
sentence_b_vec = bert_client .get_sentence_vec(sentence_b)
cos_similar = bert_client .cos_similar(sentence_a_vec,sentence_b_vec)
if cos_similar > max_cos_similar:
max_cos_similar = cos_similar
most_similar_sentence = sentence_b
print('最相似的句子:',most_similar_sentence)
bert_client .close_bert()