博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用bert 快速进行 词嵌入word2vec教程
阅读量:3938 次
发布时间:2019-05-23

本文共 936 字,大约阅读时间需要 3 分钟。

word2vec:

就是将 一个词或者一个句子映射到一个高维空间,得到一组向量

最近遇到一个任务,需要对特定的语句key 去 提取相应的 value:

比如从 一个身份证 ocr 结果中, 输入 姓名 得到对应 人的名字
一想到这里, 为了能够从 姓名 这个词组 box 得到 人名 box 结果, 除了从相对位置入手,词语之间的关联性也是一个入手点。

google 已经将预训练模型放出来在官方的github上面了,利用预训练模型,进行词嵌入是一件简单的事情:

  1. 下载预训练模型: 链接:https://github.com/google-research/bert#pre-trained-models
    我是用的是
    解压
  2. 安装包:
pip install tensorflow-gpu==1.15pip install -U bert-serving-server bert-serving-client
  1. 启动:
bert-serving-start -model_dir /path_to_the_model/ -num_worker=1
  1. 预测 词嵌入:
from bert-serving.client import BertClient()client = BertClient()vectors = client.encode(['dog', 'cat','man'])
  1. 计算相似度:
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/

你可能感兴趣的文章
从2D恢复出3D的数据
查看>>
glm 中 数据类型 与 原始数据(c++ 数组)之间的转换
查看>>
Derivatives of scalars, vector functions and matrices
查看>>
the jacobian matrix and the gradient matrix
查看>>
VS2010 将背景设为保护色
查看>>
ubutun里面用命令行安装软件
查看>>
ubuntu 常用命令
查看>>
SQLite Tutorial 4 : How to export SQLite file into CSV or Excel file
查看>>
how to move pivot to origin
查看>>
Optimizate objective function in matrix
查看>>
Convert polygon faces to triangles or quadrangles
查看>>
How do I divide matrix elements by column sums in MATLAB?
查看>>
read obj in matlab
查看>>
find out the neighbour matrix of a mesh
查看>>
Operators and special characters in matlab
查看>>
As-Conformal-As-Possible Surface Registration
查看>>
qmake Variable Reference
查看>>
Lesson 2 Gradient Desent
查看>>
find border vertex
查看>>
matlab sliced variable
查看>>