本文共 936 字,大约阅读时间需要 3 分钟。
word2vec:
就是将 一个词或者一个句子映射到一个高维空间,得到一组向量
最近遇到一个任务,需要对特定的语句key 去 提取相应的 value:
比如从 一个身份证 ocr 结果中, 输入 姓名 得到对应 人的名字 一想到这里, 为了能够从 姓名 这个词组 box 得到 人名 box 结果, 除了从相对位置入手,词语之间的关联性也是一个入手点。google 已经将预训练模型放出来在官方的github上面了,利用预训练模型,进行词嵌入是一件简单的事情:
pip install tensorflow-gpu==1.15pip install -U bert-serving-server bert-serving-client
bert-serving-start -model_dir /path_to_the_model/ -num_worker=1
from bert-serving.client import BertClient()client = BertClient()vectors = client.encode(['dog', 'cat','man'])
from service.client import BertClientimport numpy as npbc = BertClient()def cosine(a,b): return a.dot(b)/(np.linalg.norm(a)*np.linalg.norm(b))vectors=np.array(bc.encode(['First do it', 'then do it right']))print(['First do it', 'then do it right'],":",cosine(vectors[0],vectors[1]))
结果:
['First do it', 'then do it right'] : 0.92645866
转载地址:http://rnywi.baihongyu.com/