从头搭建问答(QA)系统

这这篇教程里,我们不是要搭建精度最高的模型,只是想去理解各种NLP概念,同时用代码实现,探索更多的解决方案。我始终认为要从最基本的模型出发来弄清楚问题的来龙去脉,也可以作为以后更先进模型的baseline。
本教程主要分为3部分:
这一部分主要介绍Facebook sentence embeddings ,在搭建QA系统时它是怎么用的。所有代码详见Github repository

SQuAD Dataset

Stanford Question Answering Dataset (SQuAD) 是当前最新的阅读理解数据集,涵盖一系列维基百科文章的问题,其中每个问题的答案是相应文章的一个片段(英文叫span)。在500+篇文章中包含100,000+个question-answer对,迄今为止,SQuAD超过以前任何一个阅读理解数据集。

Problem

在数据集上的任何一个样例,都包括context(文本),question(问题),text(注意数据集写的不是answer,为了不引起歧义,统一用答案表述行文)。如下:
在这里插入图片描述
我们的目标是在新给定的问题和相应文本情况下,寻找到答案。也就是说这并不是一个开放数据集,问题的答案是文本中的一部分。现在可以把问题分成2部分:

  1. 找到有正确答案的句子(黄色记号处)
  2. 一旦定位到句子,就从句子中找到正确答案(绿色记号处)

Introducing Infersent, Facebook Sentence Embedding

最近有很多单词嵌入的方法,word2vecdoc2vecfood2vecnode2vec
在这些嵌入 背后的基本思想就是用不同维数的向量来表征实体对象,这样在计算机更容易操作。
传统做法,就是把句子中的所有单词的向量表示求平均,我们叫做bag of words。每个句子都被切分成单词,通过Glove embeddings求得这些单词的向量表示,然后兑这些向量求平均。这种技术表现中规中矩,并不是一个非常精确的方法,因为它并没有照顾到单词的序列(order)。

先说说Infersent,这是一种sentence embeddings方法,提供了语义句子表示。在自然语言推断数据集(natural language inference data)上训练并且能很好地推广到各种不同的任务。

过程如下:

Create a vocabulary from the training data and use this vocabulary to train infersent model. Once the model is trained, provide sentence as input to the encoder function which will return a 4096-dimensional vector irrespective of the number of words in the sentence.
这些嵌入向量可以用在各种下游自然语言任务中,比如说寻到两个句子之间的相似性。这里有一个Quora-Question Pair kaggle比赛的例子
现在回来SQuAD问题,我们先解决问题的第一部分。

  • 把文本拆分成多个句子,可以使用Spacy & Textblob软件包。TextBlob切分句子更加智能。
  • 使用Infersent模型得到每一个句子以及问题的向量表示。
  • 基于余弦相似度和欧式距离,为每个sentence-question对创建类似距离的特征

Models

下面使用两个主要的方法进一步解决问题:

  • 无监督学习。没有使用target,直接从文本中返回与给定问题距离最近的句子。
  • 监督学习。在这部分,如何创建训练数据及是棘手的事情,原因是每个部分句子数量不固定,答案从一个单词到多个单词不等。

Suoervised Learning Models

首先,把每个文本的句子个数限定为10,其实大约98%的文本不超过10个句子。所以问题变成了
对于每一个句子,基于余弦距离构建一个特征。如果文本句子不够10个,就用1(最大可能的余弦距离)代替。下面举个例子:

Example
Question — “To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France?”
Context — “Architecturally, the school has a Catholic character. Atop the Main Building\’s gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend “Venite Ad Me Omnes”. Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive (and in a direct line that connects through 3 statues and the Gold Dome), is a simple, modern stone statue of Mary.”
Text — “Saint Bernadette Soubirous”

在上面的例子中,目标是5,因为正确答案的句子在文本的索引就是5th,我们有10个特征,每个特征对应文本中的10个句子。下面举个例子:
在这里插入图片描述
column_cos_7, column_cos_8, and column_cos_9 缺失值用1填充,因为后面3个句子不存在于段落中。

Dependency Parsing

在这个问题附加的一个特征是**”Dependency Parse Tree“**。这使得模型的准确率稍稍提高了5%。在这,我们使用的是Spacy tree parsing因为在解析树的navigation上API很丰富好用。
在这里插入图片描述
更多细节参考:Stanford Lecture

Relations among the words are illustrated above the sentence with directed, labeled arcs from heads to dependents. We call this a Typed Dependency structure because the labels are drawn from a fixed inventory of grammatical relations. It also includes a root node that explicitly marks the root of the tree, the head of the entire structure.

下面用Spacy tree parse对我们的数据进行可视化,还是上面用过的例子。
Question — “To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France?”
在这里插入图片描述
Sentence having the answer — “It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858”
在这里插入图片描述
下面是在这个例子的文本中,所有句子的root(二叉树中的概念)。
在这里插入图片描述
思想就是:把问题中的root,这个例子中就是动词”appear“,和所有句子中的root或者sub-root进行匹配。在一个句子中有多个动词,所以有多个root。如果问题的root包含在句子的root中,我们就说这个句子是问题的答案可能性就大。需要考虑的是,为每个句子创建一个特征,其值是0或者1。这里,1代表问题的root包含在句子的root中,0则相反。

注意:在把句子的root和问题的root进行比较时,要进行stemming。在先前的例子中,问题的root是appear而句子当中的root是appeared。如果没有把appear和appeared变成同一个单词,他们就无法匹配。

下面这个例子是训练集中的2个样本:
在这里插入图片描述

对于logistic回归来说,对所有的列标准化至关重要。

一旦训练数据创建完毕,我们就可以使用multinomial logistic regession,随机森林以及gradient boosting技术。

multinominal logistic regression在validation set上的准确率为65%。随机森林是67%,而XGBoost最高,为69%。

接下来会添加更多的特征来改善模型。与特征工程相关的思想和其他改善很受欢迎,与上面这些概念的所有代码详情见[这里]。(https://github.com/aswalin/SQuAD)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值