1. python<=3.6(3.7不行),1.10<=tensorflow<2.0
2. 将目前环境换成3.6版本,conda activate python36(这是我的3.6环境名字)
3. pip安装Bert服务器和客户端
pip install bert-serving-server # 服务端 pip install bert-serving-client # 客户端,与服务端互相独立
3. 启动bert服务
bert-serving-start -model_dir /lab/skill/uncased_L-12_H-768_A-12
4. 重新打开一个命令行窗口,写入python代码
from bert_serving.client import BertClient bc = BertClient() doc_vecs = bc.encode(['First do it', 'then do it right', 'then do it better']) bc.encode(['First do it ||| then do it right']) bc = BertClient() vec = bc.encode(['hey you', 'whats up?']) '''vec # [2, 25, 768] vec[0] # [1, 25, 768], sentence embeddings for `hey you` vec[0][0] # [1, 1, 768], word embedding for `[CLS]` vec[0][1] # [1, 1, 768], word embedding for `hey` vec[0][2] # [1, 1, 768], word embedding for `you` vec[0][3] # [1, 1, 768], word embedding for `[SEP]` vec[0][4] # [1, 1, 768], word embedding for padding symbol vec[0][25] # error, out of index! '''
参考
https://blog.csdn.net/qq_34832393/article/details/90414293