肖涵博士,bert-as-service 作者。现为腾讯 AI Lab 高级科学家、德中人工智能协会主席。
启动server:
bert-serving-start -model_dir uncased_L-12_H-768_A-12 -num_worker=4
/home/zhongling/tensorflow1.4/lib/python3.5/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
usage: /home/zhongling/tensorflow1.4/bin/bert-serving-start -model_dir uncased_L-12_H-768_A-12 -num_worker=4
ARG VALUE
__________________________________________________
ckpt_name = bert_model.ckpt
config_name = bert_config.json
cors = *
cpu = False
device_map = []
fp16 = False
gpu_memory_fraction = 0.5
graph_tmp_dir = None
http_max_connect = 10
http_port = None
mask_cls_sep = False
max_batch_size = 256
max_seq_len = 25
model_dir = uncased_L-12_H-768_A-12
num_worker = 4
pooling_layer = [-2]
pooling_strategy = REDUCE_MEAN
port = 5555
port_out = 5556
prefetch_size = 10
priority_batch_size = 16
tuned_model_dir = None
verbose = False
xla = False
I:VENTILATOR:[__i:__i: 63]:freeze, optimize and export graph, could take a while...
I:GRAPHOPT:[gra:opt: 52]:model config: uncased_L-12_H-768_A-12/bert_config.json
I:GRAPHOPT:[gra:opt: 55]:checkpoint: uncased_L-12_H-768_A-12/bert_model.ckpt
I:GRAPHOPT:[gra:opt: 59]:build graph...
I:GRAPHOPT:[gra:opt:128]:load parameters from checkpoint...
I:GRAPHOPT:[gra:opt:132]:optimize...
启动客户端:
>>> bc = BertClient("192.168.2.12")
>>> arr = ["women bag","phone case","Spring and summer lace bra wrapped chest anti cross chest sexy vest backing sling underwear (without PAD)","Factory Direct Fawn Decorations Bucket Bag","Fashion Women's Crossbody Bag Shoulder Bag With Cute Decoration","Flower Stud Earrings","Luxury Phone Case For iPhone"]
>>> for i in range(len(arr)):
... print(i,arr[i])
...
0 women bag
1 phone case
2 Spring and summer lace bra wrapped chest anti cross chest sexy vest backing sling underwear (without PAD)
3 Factory Direct Fawn Decorations Bucket Bag
4 Fashion Women's Crossbody Bag Shoulder Bag With Cute Decoration
5 Flower Stud Earrings
6 Luxury Phone Case For iPhone
>>> vecs = bc.encode(arr)
>>> 1 - spatial.distance.cosine(vecs[1],vecs[2])
0.6363017559051514
>>> 1 - spatial.distance.cosine(vecs[1],vecs[3])
0.703334391117096
>>> 1 - spatial.distance.cosine(vecs[1],vecs[4])
0.6734864711761475
>>> 1 - spatial.distance.cosine(vecs[1],vecs[5])
0.7531731724739075
>>> 1 - spatial.distance.cosine(vecs[1],vecs[6])
0.7267242670059204