本地安裝bert服務,並查詢詞向量

參考https://github.com/hanxiao/bert-as-service

 

一.第一步,安裝python包

pip install bert-serving-server # server 

pip install bert-serving-client # client, independent of `bert-serving-server`

二.下載BERT預訓練模型

訪問https://github.com/google-research/bert,來到

這裏推薦下載的是BERT-Base,Uncased和BERT-Base,Chinese。因爲我要做中文詞向量的查詢,所以下載了Chinese這版的。

下載完後解壓,拷貝一下文件絕對路徑(我的存放位置是F:/MyDownloads/chinese_L-12_H-768_A-12)

三.打開cmd,運行命令行

bert-serving-start  -model_dir F:/MyDownloads/chinese_L-12_H-768_A-12 -num_worker=1

這裏的model_dir是BERT預訓練模型所在路徑,num_worker爲同時訪問限制,一般自己本地用的話1就可以了。

四.詞向量查詢

from bert_serving.client

import BertClient

bc = BertClient()

print(bc.encode(['中國', '美國', '日本']))

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章