Knowledge Mapping 2- CRF layer of the most user-friendly BiLSTM-CRF model introduced

Ref: https://zhuanlan.zhihu.com/p/44042528    article excerpt over the contents of the original author and do some comments

This translation from the original article on GitHub blog, there is the end of the original link. The article does not obscure mathematical formula, but step by step implementation process CRF explain by way of example, is very, very appropriate CRF entry information.

Outline

The articles in this series include the following:

  • Concepts - based layer BiLSTM-CRF CRF model named entity recognition task explained
  • Detailed example - an example of a toy with a detailed explanation of how CRF works
  • Chainer achieved - the code used to achieve CRF layer is based on packet Chainer

I. Introduction

In the field named entity recognition, neural network implementation is very popular and commonly used. For example, this paper describes the terms and words embedded embedding BiLSTM-CRF model is one of them. I will be the model as an example to explain the working principle of CRF layer.

If you do not know BiLSTM and implementation details of CRF, they only need to remember is the entity recognition model in two different layers name.

Two .BiLSTM-CRF model

We have two types of provisions in the dataset entity, name and organization name. So, in fact, in our dataset There are five types of tags:

B-Person (name beginning portion)

I- Person (name of the middle portion)

B-Organization (the beginning of the organization)

I-Organization (intermediate portion organization)

O (non-solid information)

In addition, x is a word comprising five words (w0, w1, w2, w3, w4). Also, in the sentence x in [w0, w1] is the name, [w3] is the name of the organization, others are "O". Next, a brief introduction about the model, as shown below:

First, each word in the sentence is a word that contains embedded and embedded word vector word, the word is usually embedded in pre-trained, the embedded word is randomly initialized. All embedded will be adjusted as the iterative process of training.

Secondly, BiLSTM-CRF input word is embedded vector prediction is output corresponding to each word label.

Despite not need to know the implementation details BiLSTM, but in order to better understand the CRF level, we still need to know what output BiLSTM in the end is what it means.

As shown above, the input BiLSTM dense layer is a k-dimensional vector w0 after a single word by onehot encoding, the embedded word output BiLSTM layer indicates that this word score corresponding to each category, such as W0, the output node is BiLSTM 1.5 ( B-Person), 0.9 (I -Person), 0.1 (B-Organization), 0.08 (I-Organization) and 0.05 (O), these scores would be input layer CRF .

All scores by BiLSTM layer output will be used as input layer of CRF, the highest score in the category sequence category is the final result of our forecast.

2.1 If you do not look like CRF layer

As you found, even if there is no layer CRF, we still can train a model for the named entity recognition based on BiLSTM, as shown in FIG.

Because the model is the result of BiLSTM word score corresponding to each category, we can choose the category with the highest score as a predictor of outcome. Such as W0, "B-Person" of the highest (1.5) points, then we can select the "B-Person" as a prediction result. The same, w1 is the "I-Person", w2 is "O", w3 is the "B-Organization", w4 is "O". Although we got the right result in this example, but the reality is not always the case. Consider the following example.

Obviously, this classification is not accurate.

2.2 CRF layer may be learned constraint sentences

CRF layer may add some constraints to ensure that the final results are valid prediction. These constraints can be obtained when the automatic learning layer CRF training data .

Possible constraints as follows, with these useful constraints wrong prediction series will be greatly reduced.

  • Beginning of the sentence should be "B-" or "O", rather than "I-".
  • “B-label1 I-label2 I-label3…”,在该模式中,类别1,2,3应该是同一种实体类别。比如,“B-Person I-Person” 是正确的,而“B-Person I-Organization”则是错误的。
  • “O I-label”是错误的,命名实体的开头应该是“B-”而不是“I-”。

2.3 CRF损失函数

CRF损失函数两部分组成真实路径的分数所有路径的总分数。真实路径的分数应该是所有路径中分数最高的。其中每条路径上的损失函数都包括两种类型的分数(状态分数+转移分数)。

CRF层中的损失函数包括两种类型的分数(状态分数+转移分数),而理解这两类分数的计算是理解CRF的关键。

(1)状态分数(Emission score)

第一个类型的分数是发射分数(状态分数)。这些状态分数来自BiLSTM层的输出。如下图所示,w0被预测为B-Person的分数是1.5.

为方便起见,我们给每个类别一个索引yi={0,1,2,3,4},如下表所示:

Xi yj 代表状态分数,i是单词的位置索引,yj是类别的索引。根据上表,

表示单词w1被预测为B−Organization的分数是0.1。

(2)转移分数

我们用tyiyj来表示转移分数。例如,tB−Person,I−Person=0.9表示从类别B−Person→I−Person的分数是0.9。因此,我们有一个所有类别间的转移分数矩阵。

为了使转移分数矩阵更具鲁棒性,我们加上START 和 END两类标签。START代表一个句子的开始(不是句子的第一个单词),END代表一个句子的结束。

下表是加上START和END标签的转移分数矩阵。

如上表格所示,转移矩阵已经学习到一些有用的约束条件:

  • 句子的第一个单词应该是“B-” 或 “O”,而不是“I”。(从“START”->“I-Person 或 I-Organization”的转移分数很低)
  • “B-label1 I-label2 I-label3…”,在该模式中,类别1,2,3应该是同一种实体类别。比如,“B-Person I-Person” 是正确的,而“B-Person I-Organization”则是错误的。(“B-Organization” -> “I-Person”的分数很低)
  • “O I-label”是错误的,命名实体的开头应该是“B-”而不是“I-”。

要怎样得到这个转移矩阵呢?

实际上,转移矩阵是BiLSTM-CRF模型的一个参数。在训练模型之前,你可以随机初始化转移矩阵的分数。这些分数将随着训练的迭代过程被更新,换句话说,CRF层可以自己学到这些约束条件

CRF损失函数两部分组成真实路径的分数所有路径的总分数。真实路径的分数应该是所有路径中分数最高的。

例如,我们的数据集中有如下几种类别:

一个包含5个单词的句子,可能的类别序列如下:

  • 1. START B-Person B-Person B-Person B-Person B-Person END
  • 2. START B-Person I-Person B-Person B-Person B-Person END
  • …..
  • 10. START B-Person I-Person O B-Organization O END
  • N. O O O O O O O

每种可能的路径的分数为Pi,共有N条路径,则路径的总分是(e是常数e)

如果第十条路径是真实路径,也就是说第十条是正确预测结果,那么第十条路径的分数应该是所有可能路径里得分最高的。

根据如下损失函数,在训练过程中,BiLSTM-CRF模型的参数值将随着训练过程的迭代不断更新,使得真实路径所占的比值越来越大。

现在的问题是:

  1. 怎么定义路径的分数?
  2. 怎么计算所有路径的总分?
  3. 当计算所有路径总分时,是否需要列举出所有可能的路径?(答案是不需要)

(3)真实路径分数

计算真实路径分数,eSi,是非常容易的。我们先集中注意力来计算Si

“START B-Person I-Person O B-Organization O END”这条真实路径来说:

句子中有5个单词,w1,w2,w3,w4,w5,加上START和END 在句子的开始位置和结束位置,记为,w0,w6

Si = EmissionScore + TransitionScore

这些分数来自BiLSTM层的输出,至于x_{0,start}x_{6,end},则设为0。

这些分数来自于CRF层,将这两类分数加和即可得到Si 和 路径分数eSi

(4)所有路径的总分

如何计算所有路径的总分呢?我们将以一个玩具的例子详细讲解。

这部分是最重要的并且也是比较难的,但不用担心,我将用玩具的例子尽可能简单的讲清楚里面的细节。

Step 1: 我们定义的损失函数如下:

现在我们把它变成对数损失函数:

由于我们的训练目标通常是最小化损失函数,所以我们加上负号:

前面我们已经很清楚如何计算真实路径得分,现在我们需要找到一个方法去计算

Step 2:回忆一下状态分数 和 转移分数

为了简化问题,我们假定我们的句子只有3个单词组成:

X = [w0, w1 ,w2]

另外,我们只有两个类别:

LabelSet = {l1, l2}

状态分数如下:

转移矩阵如下:

Step 3:开始奋斗!(纸和笔准备好!)

记住:我们的目标是:

整个过程是一个分数的积聚过程。它的实现思想有点像动态规划。首先,w0所有路径的总分先被计算出来,然后,我们计算w0 -> w1的所有路径的得分,最后计算w0 -> w1 -> w2的所有路径的得分,也就是我们需要的结果。接下来,你会看到两个变量:obs和 previousPrevious存储了之前步骤的结果,obs代表当前单词所带的信息。 x01表示第0个单词属于第1类的概率

如果我们的句子只有一个单词,我们就没有之前步骤的结果,所以Previous 是空。我们只能观测到状态分数 obs =[x01,x02]

W0 的所有路径总分就是:

 

你可能疑惑为啥要扩展previous 和 obs 矩阵呢?因为这样操作可以是接下来的计算相当高效,你很快就能意会到这点。

(t12表示上一时刻的状态类别是1,下一时刻状态类别是2的概率)

实际上,第二次迭代过程也就完成了。下面是第一个单词w0,第二个单词是w1 的所有路径的总分。Si 表示第i种路径的概率或得分,将w1状态为1的放在一列,w2状态为2的放在一列。

发现了吗,这其实就是我们的目标,Si 表示第i种路径的概率或得分。

读到这边,差不多就大功告成了。这一步,我们再重复一次之前的步骤。

跟上一步骤一样。我们用新的previous计算总分:

完结,撒花!

我们最终得到了我们的目标,我们的句子中共有3个单词和两个类别,所以共有8(2^3)条路径。

2.4 对句子的单词词性做预测

在之前章节我们学习了BiLSTM-CRF模型的基本结构和CRF的损失函数。现在你可以用各种开源框架搭建你自己的BiLSTM-CRF模型(Keras, Chainer, TensorFlow等)。用这些框架最爽的事情就是你不用自己实现反向传播这个过程,并且有的框架已经实现CRF层,这样只需要添加一行代码就能在你的模型中实现CRF过程。

本章我们会探索如何用我们训练好的模型去预测一个句子每个单词的词性。

Step 1:BiLSTM-CRF模型得到的发射分数和转移分数

假定我们的句子共3个单词组成:

并且,我们已经从我们的模型中得到了发射分数和转移分数,如下:

转移矩阵:

Step 2:开始预测

如果你熟悉Viterbi算法,理解这一步的知识点将会非常容易。当然,如果你不熟悉也无所谓,整个预测过程和之前求所有路径总分的过程非常类似。我将逐步解释清楚,我们先从左到右的顺序来运行预测算法。

你将会看到两类变量:obs 和 previous。Previous存储了上一个步骤的最终结果,obs代表当前单词包含的信息(发射分数)。

Alpha0 是历史最佳的分数 ,alpha1 是最佳分数所对应的类别索引。这两类变量的详细信息待会会做说明。先来看下面的图片:你可以把这两类变量当做狗狗去森林里玩耍时在路上做的标记,这些标记可以帮助狗狗找到回家的路。

 

 

现在,我们来观测第一个单词W0,很显然,W0所对应的最佳预测类别是非常容易知道的。比如如果

显然,最佳预测结果是l2。

看到这里,你可能好奇这跟之前求所有路径分数的算法没什么区别,别急,你马上就会看到不同之处啦!

在下一次迭代前更改previous的值:

举个例子,如果我们的得分如下:

 

那么我们的previous应该是:

这是什么意思呢?其实也就是说previous存储的是当前单词对应各类别的最佳路径得分。W1被预测为L1类别的最高分是0.5,路径是L2->L1,W1被预测为L2类别的最高分是0.4,路径是L2->L2。

这边,我们有两个变量来储存历史信息,alpha0 和 alpha1.

在本次迭代中,我们将最佳分数存储到alpha0 :

同时,最佳分数所对应的类别索引存储到alpha1:

类别L1的索引是0,L2的索引是1,所以(1,1)=(L2,L2)表示第1个单词的类别是L2时,第二个单词属于类别L1/L2的概率才会最大。表示当前最佳分数0.5对应的路径是L2->L1,最佳分数0.4对应的路径是L2->L2。(1,1)可以理解为前一单词分别对应的类别索引。

上面scores有错误,应该是

更改previous的值:

假如我们的得分是:

现在我们的previous是:

现在,我们选取previous[0] 和previous[1]中最大的分数作为最佳路径。也就是0.9对应的路径是我们的预测结果。

由w1-->w2得出:0.8对应的最佳路径是L2-->L1;0.9对应的最佳路径是L1-->L2;

同时,每个类别对应的最大得分添加到alpha0 和 alpha1中:

(1,1),(0.5,0.4):表示w0属于类别L2的时,w1属于L1的概率(0.5)和属于L2的概率(0.4) 取得最大值。

(1,0),(0.8,0.9):表示w1属于类别L2的时,w2属于L1的概率取得最大值;最终L2-->L2-->L1的概率为0.8

                            表示w1属于类别L1的时,w2属于L2的概率取得最大值,最终L2-->L1-->L2的概率为0.9

Step 3:根据最大得分找到最佳路径

这是最后一步,alpha0 和 alpha1将被用来找到最佳路径。

先看alpha0,alpha0中最后一个单词对应的类别得分分别是0.8 和 0.9,那么0.9对应的类别L2就是最佳预测结果。再看alpha1,L2对应的索引是0, “0”表示之前一个单词对应的类别是L1,所以W1-W2的最佳路径是: L1->L2

接着往前推,alpha1=(1,1),我们已经知道W1的预测结果是L1,对应的索引是0,(1,1)[0] = 1,所以W0对应的类别是L2。

所以我们预测的最佳路径是 L2-> L1 -> L2 。

 

 

 

 

发布了118 篇原创文章 · 获赞 132 · 访问量 60万+

Guess you like

Origin blog.csdn.net/qfikh/article/details/103588744