The classic RNN model and how to use the Attention structure

1. Starting from a single-layer network

Before learning RNN, we must first understand the most basic single-layer network, its structure is shown in the figure:

The input is x, and the output y is obtained by transforming Wx+b and the activation function f. I believe you are already very familiar with this.

Second, the classic RNN structure (N vs N)

In practical applications, we will also encounter a lot of serial data:



Such as:

  • Natural language processing problems. x1 can be regarded as the first word, x2 can be regarded as the second word, and so on.
  • speech processing. At this time, x1, x2, x3... are the sound signals of each frame.
  • time series problem. such as daily stock prices, etc.

Sequence-shaped data is not so easy to handle with raw neural networks. In order to model sequence problems, RNN introduces the concept of hidden state h (hidden state), which can extract features from sequence-shaped data and then convert it into output. Let's start with the calculation of h1:


The meanings of the symbols in the illustration are:

  • Circles or squares represent vectors.
  • An arrow represents a transformation of the vector. As shown in the figure above, h0 and x1 are respectively connected by an arrow, which means that each of h0 and x1 is transformed once.

Similar notations also appear in many papers. It is easy to mess up when you are a beginner, but as long as you grasp the above two points, you can easily understand the meaning behind the diagrams.

The calculation of h2 is similar to that of h1. It should be noted that during the calculation, the parameters U, W, and b used in each step are the same, that is to say, the parameters of each step are shared, which is an important feature of RNN and must be kept in mind.


Calculate the rest in turn (using the same parameters U, W, b):


For the sake of convenience, we only draw the case where the sequence length is 4. In fact, this calculation process can continue indefinitely.

Our current RNN has no output, and the way to get the output value is to calculate directly through h:

As mentioned before, an arrow means to perform a transformation similar to f(Wx+b) on the corresponding vector, and the arrow here means to perform a transformation on h1 to obtain the output y1.

The rest of the output proceeds similarly (using the same parameters V and c as y1):

OK!大功告成!这就是最经典的RNN结构,我们像搭积木一样把它搭好了。它的输入是x1, x2, .....xn,输出为y1, y2, ...yn,也就是说,输入和输出序列必须要是等长的

由于这个限制的存在,经典RNN的适用范围比较小,但也有一些问题适合用经典的RNN结构建模,如:

  • 计算视频中每一帧的分类标签。因为要对每一帧进行计算,因此输入和输出序列等长。
  • 输入为字符,输出为下一个字符的概率。这就是著名的Char RNN(详细介绍请参考:The Unreasonable Effectiveness of Recurrent Neural Networks,Char RNN可以用来生成文章,诗歌,甚至是代码,非常有意思)。

三、N VS 1

有的时候,我们要处理的问题输入是一个序列,输出是一个单独的值而不是序列,应该怎样建模呢?实际上,我们只在最后一个h上进行输出变换就可以了:


这种结构通常用来处理序列分类问题。如输入一段文字判别它所属的类别,输入一个句子判断其情感倾向,输入一段视频并判断它的类别等等。

四、1 VS N

输入不是序列而输出为序列的情况怎么处理?我们可以只在序列开始进行输入计算:


还有一种结构是把输入信息X作为每个阶段的输入:


下图省略了一些X的圆圈,是一个等价表示:

这种1 VS N的结构可以处理的问题有:

  • 从图像生成文字(image caption),此时输入的X就是图像的特征,而输出的y序列就是一段句子
  • 从类别生成语音或音乐等

五、N vs M

下面我们来介绍RNN最重要的一个变种:N vs M。这种结构又叫Encoder-Decoder模型,也可以称之为Seq2Seq模型。

原始的N vs N RNN要求序列等长,然而我们遇到的大部分问题序列都是不等长的,如机器翻译中,源语言和目标语言的句子往往并没有相同的长度。

为此,Encoder-Decoder结构先将输入数据编码成一个上下文向量c:


得到c有多种方式,最简单的方法就是把Encoder的最后一个隐状态赋值给c,还可以对最后的隐状态做一个变换得到c,也可以对所有的隐状态做变换。

拿到c之后,就用另一个RNN网络对其进行解码,这部分RNN网络被称为Decoder。具体做法就是将c当做之前的初始状态h0输入到Decoder中:


还有一种做法是将c当做每一步的输入:


由于这种Encoder-Decoder结构不限制输入和输出的序列长度,因此应用的范围非常广泛,比如:

  • 机器翻译。Encoder-Decoder的最经典应用,事实上这一结构就是在机器翻译领域最先提出的
  • 文本摘要。输入是一段文本序列,输出是这段文本序列的摘要序列。
  • 阅读理解。将输入的文章和问题分别编码,再对其进行解码得到问题的答案。
  • 语音识别。输入是语音信号序列,输出是文字序列。
  • …………

六、Attention机制

在Encoder-Decoder结构中,Encoder把所有的输入序列都编码成一个统一的语义特征c再解码,因此, c中必须包含原始序列中的所有信息,它的长度就成了限制模型性能的瓶颈。如机器翻译问题,当要翻译的句子较长时,一个c可能存不下那么多信息,就会造成翻译精度的下降。

Attention机制通过在每个时间输入不同的c来解决这个问题,下图是带有Attention机制的Decoder:


每一个c会自动去选取与当前所要输出的y最合适的上下文信息。具体来说,我们用 a_{ij} 衡量Encoder中第j阶段的hj和解码时第i阶段的相关性,最终Decoder中第i阶段的输入的上下文信息 c_i 就来自于所有 h_ja_{ij} 的加权和。

以机器翻译为例(将中文翻译成英文):



输入的序列是“我爱中国”,因此,Encoder中的h1、h2、h3、h4就可以分别看做是“我”、“爱”、“中”、“国”所代表的信息。在翻译成英语时,第一个上下文c1应该和“我”这个字最相关,因此对应的 a_{11} 就比较大,而相应的 a_{12}a_{13}a_{14} 就比较小。c2应该和“爱”最相关,因此对应的 a_{22} 就比较大。最后的c3和h3、h4最相关,因此 a_{33}a_{34} 的值就比较大。

至此,关于Attention模型,我们就只剩最后一个问题了,那就是:这些权重 a_{ij} 是怎么来的?

事实上, a_{ij} 同样是从模型中学出的,它实际和Decoder的第i-1阶段的隐状态、Encoder第j个阶段的隐状态有关。

同样还是拿上面的机器翻译举例, a_{1j} 的计算(此时箭头就表示对h'和 h_j 同时做变换):



a_{2j} 的计算:


a_{3j} 的计算:

以上就是带有Attention的Encoder-Decoder模型计算的全过程。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325867287&siteId=291194637