使用通用句子编码器USE(Universal Sentence Encoder)进行语义查询

1 引言

随着新的深度学习架构的出现,自然语言处理有了很大的进步。随着新架构的出现,大量的新应用涌现出来,其中,通用句子编码器USE(Universal Sentence Encoder)是一个基于Transformer的NLP模型,广泛用于嵌入句子或单词。此外,该嵌入可用于文本聚类、分类等。

在试验过程中,发现直接使用pip install top2vec[sentence_encoders]命令安装Top2Vec的USE(universal-sentence-encoder)和USEM(universal-sentence-encoder-multilingual)不成功,可能的原因是Tensorflow没有安装好。与Pytorch一样,Tensorflow有CPU和GPU两个版本,GPU版本的安装过程非常复杂,首先需要配置CUDA,于是决定先使用CPU版本。使用下面的命令进行安装:

conda install -c anaconda tensorflow

本意是安装V2.5版本,但不知什么原因,最后竟然安装的是V2.3.0版本。在测试安装成功后,又安装了tensorflow_hub和seaborn两个关键库。至此Tensorflow的基本安装工作完成。


2 下载USE

通用句子编码器USE将文本编码为高维向量,可用于文本分类、语义相似性、聚类和其他自然语言任务。该模型用于句子、短语或短段落的训练和优化,它在各种数据源和各种任务上进行训练,目的是动态地适应各种自然语言理解任务。输入是可变长度的英语文本,输出是一个512维的向量。USE模型是用深度平均网络DAN编码器训练的。USE与基于单词的嵌入模型不同,USE在自然语言预测任务上进行训练得出的,这些任务需要对单词序列的意义进行建模,而不仅仅是单个单词。USE来自于Google AI 试验室Daniel Cer等人(2018)的论文《Universal Sentence Encoder》。

在下载用句法编码器USE(Universal Sentence Encoder)时,不能使用hub.load(module_url) 进行下载于是手动下载universal-sentence-encoder_4.tar (937.86M), 解压后放在工作目录下,加载成功


3 USE试验

随机选择了如下12个句子:

"Characterization of step-path failure mechanisms",

"A combined field based-numerical modeling study",

"The importance of step-path failure geometries in the stability of rock slopes",

"use of digital imaging techniques including laser scanning in the characterization of both step-paths",

"A combined finite element-discrete element code is used to illustrate the factors influencing step-path failure",

"Based on the results of the laboratory simulations step-path failure models of large open pit slopes are presented",

"the influence of intact rock bridge length, step-path overlap and fracture spacing discussed",

"The importance of considering step-path geometries for various translational failure mechanisms",

"Evaluation of rock bridges through dfn models to improve pit slope design in the absence of joint persistence data",

"Pit slope design relies on appropriate selection of design strength parameters",

"step-path failures may develop through combination of planar shear along joints and failure through intact rock",

"The presence of rock bridges between discontinuities increases shear strength along potential failure surfaces"]

然后测试一个句子与上述句子的相似度。

(1) what is rock bridge failure mechanisms?

最相似的结果为"Characterization of step-path failure mechanisms".

(2) what is DFN model?

最相似的结果为"A combined field based-numerical modeling study".

(3) open pit slope stability

最相似的结果为"The importance of step-path failure geometries in the stability of rock slopes"


4 Seaborn热力图

在Matplotlib的基础上,Seaborn库以热力图的形式来表示数据。热力图是以二维形式表示数据的一种方式,用颜色表示数据值。热力图的目的是提供一个彩色的视觉信息总结。Seaborn可以画分布图,矩阵图,回归图,时间序列图和分类图。

(1) 句子相互作用矩阵图

使用通用句子编码器USE(Universal Sentence Encoder)进行语义查询的图1

(2) 查询句子的相似性热力图

使用通用句子编码器USE(Universal Sentence Encoder)进行语义查询的图2

5 结束语

有些人认为USE的性能优于BERT,这个论断不一定正确。模型的性能取决于数据集的质量和应用场景, 例如在我们的一些相似性试验中, Doc2Vec模型的性能有时甚至超越BERT模型。事实上,在这项近一个月的NLP研究中,我们已经比较了目前世界上最先进的模型和方法。结果发现,对于相似性查询和语义查询,最好的方法其实还是最简单的方法,即我一直使用的C(n,3)组合查询方法(一种快速在GeotechSet数据集内查询相似段落的方法)。 这是这项研究即将结束时得出的一个非常悲观的结论。

默认 最新
当前暂无评论,小编等你评论哦!
点赞 评论 收藏
关注