BERT 服務化 bert-as-service
阿新 • • 發佈:2021-03-03
bert-as-service 用 BERT 作為句子編碼器, 並通過 ZeroMQ 服務託管, 只需兩行程式碼就可以將句子對映成固定長度的向量表示;
## 準備
windows10 + python3.5 + tensorflow1.2.1
## 安裝流程
1. 安裝 tensorflow, [參考](https://tensorflow.google.cn/install/pip#windows_1)
2. 安裝 bert-as-service
bert-as-service, 依賴於 **python≥3.5** AND **tensorflow≥1.10**;
```
pip install bert-serving-server
pip instlal bert-serving-client
```
3. 下載中文 bert 預訓練的模型
| [BERT-Base, Uncased](https://storage.googleapis.com/bert_models/2018_10_18/uncased_L-12_H-768_A-12.zip) | 12-layer, 768-hidden, 12-heads, 110M parameters |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| [BERT-Large, Uncased](https://storage.googleapis.com/bert_models/2018_10_18/uncased_L-24_H-1024_A-16.zip) | 24-layer, 1024-hidden, 16-heads, 340M parameters |
| [BERT-Base, Cased](https://storage.googleapis.com/bert_models/2018_10_18/cased_L-12_H-768_A-12.zip) | 12-layer, 768-hidden, 12-heads , 110M parameters |
| [BERT-Large, Cased](https://storage.googleapis.com/bert_models/2018_10_18/cased_L-24_H-1024_A-16.zip) | 24-layer, 1024-hidden, 16-heads, 340M parameters |
| [BERT-Base, Multilingual Cased (New)](https://storage.googleapis.com/bert_models/2018_11_23/multi_cased_L-12_H-768_A-12.zip) | 104 languages, 12-layer, 768-hidden, 12-heads, 110M parameters |
| [BERT-Base, Multilingual Cased (Old)](https://storage.googleapis.com/bert_models/2018_11_03/multilingual_L-12_H-768_A-12.zip) | 102 languages, 12-layer, 768-hidden, 12-heads, 110M parameters |
| [BERT-Base, Chinese](https://storage.googleapis.com/bert_models/2018_11_03/chinese_L-12_H-768_A-12.zip) | Chinese Simplified and Traditional, 12-layer, 768-hidden, 12-heads, 110M parameters |
4. 啟動 bert-as-serving 服務
```shell
bert-serving-start -model_dir /tmp/english_L-12_H-768_A-12/ -num_worker=2 //模型路徑自改
usage: xxxx\Anaconda3\envs\py35\Scripts\bert-serving-start -model_dir D:\env\bert\chinese_L-12_H-768_A-12 -num_worker=2
ARG VALUE
__________________________________________________
ckpt_name = bert_model.ckpt
config_name = bert_config.json
cors = *
cpu = False
device_map = []
do_lower_case = True
fixed_embed_length = False
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 = D:\env\bert\chinese_L-12_H-768_A-12
no_position_embeddings = False
no_special_token = False
num_worker = 2
pooling_layer = [-2]
pooling_strategy = REDUCE_MEAN
port = 5555
port_out = 5556
prefetch_size = 10
priority_batch_size = 16
show_tokens_to_client = False
tuned_model_dir = None
verbose = False
xla = False
I:[35mVENTILATOR[0m:freeze, optimize and export graph, could take a while...
I:[36mGRAPHOPT[0m:model config: D:\env\bert\chinese_L-12_H-768_A-12\bert_config.json
I:[36mGRAPHOPT[0m:checkpoint: D:\env\bert\chinese_L-12_H-768_A-12\bert_model.ckpt
I:[36mGRAPHOPT[0m:build graph...
I:[36mGRAPHOPT[0m:load parameters from checkpoint...
I:[36mGRAPHOPT[0m:optimize...
I:[36mGRAPHOPT[0m:freeze...
I:[36mGRAPHOPT[0m:write graph to a tmp file: C:\Users\Memento\AppData\Local\Temp\tmpo07002um
I:[35mVENTILATOR[0m:bind all sockets
I:[35mVENTILATOR[0m:open 8 ventilator-worker sockets
I:[35mVENTILATOR[0m:start the sink
I:[32mSINK[0m:ready
I:[35mVENTILATOR[0m:get devices
W:[35mVENTILATOR[0m:no GPU available, fall back to CPU
I:[35mVENTILATOR[0m:device map:
worker 0 -> cpu
worker 1 -> cpu
I:[33mWORKER-0[0m:use device cpu, load graph from C:\Users\Memento\AppData\Local\Temp\tmpo07002um
I:[33mWORKER-1[0m:use device cpu, load graph from C:\Users\Memento\AppData\Local\Temp\tmpo07002um
I:[33mWORKER-0[0m:ready and listening!
I:[33mWORKER-1[0m:ready and listening!
I:[35mVENTILATOR[0m:all set, ready to serve request!
```
5. 用 python 模擬呼叫 bert-as-service 服務
````python
bc = BertClient(ip="localhost", check_version=False, check_length=False)
vec = bc.encode(['你好', '你好呀', '我很好'])
print(vec)
````
輸出結果:
```
[[ 0.2894022 -0.13572647 0.07591158 ... -0.14091237 0.54630077
-0.30118054]
[ 0.4535432 -0.03180456 0.3459639 ... -0.3121457 0.42606848
-0.50814617]
[ 0.6313594 -0.22302179 0.16799903 ... -0.1614125 0.23098437
-0.5840646 ]]
```
## [亮點](https://github.com/hanxiao/bert-as-service#highlights)
-