Commit f2d8a07d by zengxin

chapter6

parent 1621d4cf
......@@ -246,7 +246,7 @@ NMT & $ 21.7^{\ast}$ & $18.7^{\ast}$ & -1
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-encoder-decoder-process}
\caption{ encoder-decoder过程 }
\caption{ Encoder-Decoder过程 }
\label{fig:6-5}
\end{figure}
%----------------------------------------------
......@@ -268,7 +268,7 @@ NMT & $ 21.7^{\ast}$ & $18.7^{\ast}$ & -1
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Presentation-space}
\input{./Chapter6/Figures/figure-presentation-space}
\caption{统计机器翻译和神经机器翻译的表示空间}
\label{fig:6-6}
\end{figure}
......@@ -298,7 +298,7 @@ NMT & $ 21.7^{\ast}$ & $18.7^{\ast}$ & -1
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-A-working-example-of-neural-machine-translation}
\input{./Chapter6/Figures/figure-a-working-example-of-neural-machine-translation}
\caption{神经机器翻译的运行实例}
\label{fig:6-7}
\end{figure}
......@@ -398,18 +398,18 @@ NMT & $ 21.7^{\ast}$ & $18.7^{\ast}$ & -1
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Structure-of-a-recurrent-network-model}
\caption{循环网络模型的结构}
\input{./Chapter6/Figures/figure-structure-of-a-recurrent-network-model}
\caption{循环神经网络处理序列的实例}
\label{fig:6-9}
\end{figure}
%----------------------------------------------
\parinterval 在神经机器翻译里使用循环神经网络也很简单。我们只需要把源语言句子和目标语言句子分别看作两个序列,之后使用两个循环神经网络分别对其进行建模。这种网络结构如图\ref{fig:6-10}所示。图中,下半部分是编码器,上半部分是解码器。编码器利用循环神经网络对源语言序列逐词进行编码处理,同时利用循环单元的记忆能力,不断累积序列信息,遇到终止符<eos>后便得到了包含源语言句子全部信息的表示结果。解码器利用编码器的输出和起始符<sos>开始逐词的进行解码,即逐词翻译,每得到一个译文单词,便将其作为当前时刻解码端循环单元的输入,这也是一个典型的神经语言模型的序列生成过程。解码器通过循环神经网络不断的累积已经得到的译文的信息,并继续生成下一个单词,直到遇到结束符<eos>,便得到了最终完整的译文。
\parinterval 在神经机器翻译里使用循环神经网络也很简单。我们只需要把源语言句子和目标语言句子分别看作两个序列,之后使用两个循环神经网络分别对其进行建模。这个过程如图\ref{fig:6-10}所示。图中,下半部分是编码器,上半部分是解码器。编码器利用循环神经网络对源语言序列逐词进行编码处理,同时利用循环单元的记忆能力,不断累积序列信息,遇到终止符<eos>后便得到了包含源语言句子全部信息的表示结果。解码器利用编码器的输出和起始符<sos>开始逐词的进行解码,即逐词翻译,每得到一个译文单词,便将其作为当前时刻解码端循环单元的输入,这也是一个典型的神经语言模型的序列生成过程。解码器通过循环神经网络不断的累积已经得到的译文的信息,并继续生成下一个单词,直到遇到结束符<eos>,便得到了最终完整的译文。
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Model-structure-based-on-recurrent-neural-network-translation}
\input{./Chapter6/Figures/figure-model-structure-based-on-recurrent-neural-network-translation}
\caption{基于循环神经网络翻译的模型结构}
\label{fig:6-10}
\end{figure}
......@@ -493,20 +493,20 @@ $\textrm{P}({y_j | \mathbf{s}_{j-1} ,y_{j-1},\mathbf{C}})$由Softmax实现,Sof
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Word-embedding-structure}
\caption{词嵌入层结构}
\caption{词嵌入的生成过程}
\label{fig:6-12}
\end{figure}
%----------------------------------------------
\parinterval 需要注意的是,在上面这个过程中One-hot表示和词嵌入矩阵并不必须调用矩阵乘法才得到词嵌入结果。只需要获得One-hot向量中1对应的索引,从词嵌入矩阵中取出对应的行即可。这种利用索引``取''结果的方式避免了计算代价较高的矩阵乘法,因此在实际系统中很常用。
\parinterval 在解码端,需要在每个位置预测输出的单词。在循环神经网络中,每一时刻循环单元的输出向量为$\mathbf{s}_j$,它可以被看作这个时刻的目标语单词的一种表示,但是我们无法根据这个向量得出要生成的目标语单词的概率。而输出层的目的便是通过向量$\mathbf{s}_j$计算词表中每个单词的生成概率,进而选取概率最高的单词作为当前时刻的输出。图\ref{fig:6-13}展示了一个输出层的运行实例。
\parinterval 在解码端,需要在每个位置预测输出的单词。在循环神经网络中,每一时刻循环单元的输出向量为$\mathbf{s}_j$,它可以被看作这个时刻的目标语单词的一种表示,但是我们无法根据这个向量得出要生成的目标语单词的概率。而输出层的目的便是通过向量$\mathbf{s}_j$计算词表中每个单词的生成概率,进而选取概率最高的单词作为当前时刻的输出。图\ref{fig:6-13}展示了一个输出层进行单词预测的实例。
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Output-layer-structur}
\caption{输出层结构}
\input{./Chapter6/Figures/figure-output-layer-structur}
\caption{输出层的预测过程}
\label{fig:6-13}
\end{figure}
%----------------------------------------------
......@@ -534,7 +534,7 @@ $\textrm{P}({y_j | \mathbf{s}_{j-1} ,y_{j-1},\mathbf{C}})$由Softmax实现,Sof
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Softmax}
\input{./Chapter6/Figures/figure-softmax}
\caption{ Softmax函数(一维)所对应的曲线}
\label{fig:6-14}
\end{figure}
......@@ -707,7 +707,7 @@ $\textrm{P}({y_j | \mathbf{s}_{j-1} ,y_{j-1},\mathbf{C}})$由Softmax实现,Sof
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-bi-RNN}
\caption{双向循环神经网络}
\caption{基于双向循环神经网络的机器翻译模型结构}
\label{fig:6-18}
\end{figure}
%----------------------------------------------
......@@ -727,8 +727,8 @@ $\textrm{P}({y_j | \mathbf{s}_{j-1} ,y_{j-1},\mathbf{C}})$由Softmax实现,Sof
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Double-layer-RNN} \hspace{10em}
\caption{双层循环神经网络}
\input{./Chapter6/Figures/figure-double-layer-RNN} \hspace{10em}
\caption{基于双层循环神经网络的机器翻译模型结构}
\label{fig:6-19}
\end{figure}
%----------------------------------------------
......@@ -782,7 +782,7 @@ $\textrm{P}({y_j | \mathbf{s}_{j-1} ,y_{j-1},\mathbf{C}})$由Softmax实现,Sof
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Attention-of-source-and-target-words}
\input{./Chapter6/Figures/figure-attention-of-source-and-target-words}
\caption{源语词和目标语词的关注度}
\label{fig:6-21}
\end{figure}
......@@ -795,7 +795,7 @@ $\textrm{P}({y_j | \mathbf{s}_{j-1} ,y_{j-1},\mathbf{C}})$由Softmax实现,Sof
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-encoder-decoder-with-Attention}
\input{./Chapter6/Figures/figure-encoder-decoder-with-attention}
\caption{(a)不使用和(b)使用注意力机制的翻译模型对比}
\label{fig:6-22}
\end{figure}
......@@ -820,7 +820,7 @@ $\textrm{P}({y_j | \mathbf{s}_{j-1} ,y_{j-1},\mathbf{C}})$由Softmax实现,Sof
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Calculation-process-of-context-vector-C}
\input{./Chapter6/Figures/figure-calculation-process-of-context-vector-C}
\caption{上下文向量$\mathbf{C}_j$的计算过程}
\label{fig:6-23}
\end{figure}
......@@ -861,7 +861,7 @@ a (\mathbf{s},\mathbf{h}) = \left\{ \begin{array}{ll}
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Matrix-Representation-of-Attention-Weights-Between-Chinese-English-Sentence-Pairs}
\input{./Chapter6/Figures/figure-matrix-representation-of-attention-weights-between-chinese-english-sentence-pairs}
\caption{一个汉英句对之间的注意力权重{$\alpha_{i,j}$}的矩阵表示}
\label{fig:6-24}
\end{figure}
......@@ -870,12 +870,12 @@ a (\mathbf{s},\mathbf{h}) = \left\{ \begin{array}{ll}
\vspace{0.5em}
\end{itemize}
\parinterval\ref{fig:6-25}展示了一个上下文向量的计算过程。首先,计算目标语第一个单词``Have''与源语中的所有单词的相关性,即注意力权重,对应图中第一列$\alpha_{i,1}$,则当前时刻所使用的上下文向量$\mathbf{C}_1 = \sum_{i=1}^8 \alpha_{i,1} \mathbf{h}_i$;然后,计算第二个单词``you''的注意力权重对应第二列$\alpha_{i,2}$,其上下文向量$\mathbf{C}_2 = \sum_{i=1}^8 \alpha_{i,2} \mathbf{h}_i$,以此类推,可以得到任意目标语位置$j$的上下文向量$\mathbf{C}_j$。很容易看出,不同目标语单词的上下文向量对应的源语言词的权重$\alpha_{i,j}$是不同的,不同的注意力权重为不同位置赋予了不同重要性,对应了注意力机制的思想。
\parinterval\ref{fig:6-25}展示了一个上下文向量的计算过程实例。首先,计算目标语第一个单词``Have''与源语中的所有单词的相关性,即注意力权重,对应图中第一列$\alpha_{i,1}$,则当前时刻所使用的上下文向量$\mathbf{C}_1 = \sum_{i=1}^8 \alpha_{i,1} \mathbf{h}_i$;然后,计算第二个单词``you''的注意力权重对应第二列$\alpha_{i,2}$,其上下文向量$\mathbf{C}_2 = \sum_{i=1}^8 \alpha_{i,2} \mathbf{h}_i$,以此类推,可以得到任意目标语位置$j$的上下文向量$\mathbf{C}_j$。很容易看出,不同目标语单词的上下文向量对应的源语言词的权重$\alpha_{i,j}$是不同的,不同的注意力权重为不同位置赋予了不同重要性,对应了注意力机制的思想。
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Example-of-context-vector-calculation-process}
\input{./Chapter6/Figures/figure-example-of-context-vector-calculation-process}
\caption{上下文向量计算过程实例}
\label{fig:6-25}
\end{figure}
......@@ -913,13 +913,13 @@ a (\mathbf{s},\mathbf{h}) = \left\{ \begin{array}{ll}
\parinterval 那么,如何理解这个过程?注意力机制的本质又是什么呢?换一个角度来看,实际上,目标语位置$j$本质上是一个查询,我们希望从源语言端找到与之最匹配的源语言位置,并返回相应的表示结果。为了描述这个问题,可以建立一个查询系统。假设有一个库,里面包含若干个$\mathrm{key}$-$\mathrm{value}$单元,其中$\mathrm{key}$代表这个单元的索引关键字,$\mathrm{value}$代表这个单元的值。比如,对于学生信息系统,$\mathrm{key}$可以是学号,$\mathrm{value}$可以是学生的身高。当输入一个查询$\mathrm{query}$,我们希望这个系统返回与之最匹配的结果。也就是,希望找到匹配的$\mathrm{key}$,并输出其对应的$\mathrm{value}$。比如,当查询某个学生的身高信息时,可以输入学生的学号,之后在库中查询与这个学号相匹配的记录,并把这个记录中的$\mathrm{value}$(即身高)作为结果返回。
\parinterval\ref{fig:6-26}(a)展示了一个这样的查询系统。里面包含四个$\mathrm{key}$-$\mathrm{value}$单元,当输入查询$\mathrm{query}$,就把$\mathrm{query}$与这四个$\mathrm{key}$逐个进行匹配,如果完全匹配就返回相应的$\mathrm{value}$。在图中的例子中,$\mathrm{query}$$\mathrm{key}_3$是完全匹配的(因为都是横纹),因此系统返回第三个单元的值,即$\mathrm{value}_3$。当然,如果库中没有与$\mathrm{query}$匹配的$\mathrm{key}$,则返回一个空结果。
\parinterval\ref{fig:6-26}展示了一个这样的查询系统。里面包含四个$\mathrm{key}$-$\mathrm{value}$单元,当输入查询$\mathrm{query}$,就把$\mathrm{query}$与这四个$\mathrm{key}$逐个进行匹配,如果完全匹配就返回相应的$\mathrm{value}$。在图中的例子中,$\mathrm{query}$$\mathrm{key}_3$是完全匹配的(因为都是横纹),因此系统返回第三个单元的值,即$\mathrm{value}_3$。当然,如果库中没有与$\mathrm{query}$匹配的$\mathrm{key}$,则返回一个空结果。
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Query-model-corresponding-to-traditional-query-model-vs-attention-mechanism}
\caption{传统查询模型(a)和注意力机制所对应的查询模型(b)}
\input{./Chapter6/Figures/figure-query-model-corresponding-to-traditional-query-model-vs-attention-mechanism}
\caption{传统查询模型}
\label{fig:6-26}
\end{figure}
%----------------------------------------------
......@@ -937,7 +937,7 @@ a (\mathbf{s},\mathbf{h}) = \left\{ \begin{array}{ll}
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Query-model-corresponding-to-attention-mechanism}
\input{./Chapter6/Figures/figure-query-model-corresponding-to-attention-mechanism}
\caption{注意力机制所对应的查询模型}
\label{fig:6-27}
\end{figure}
......@@ -1059,7 +1059,7 @@ L(\mathbf{Y},\widehat{\mathbf{Y}}) = \sum_{j=1}^n L_{\textrm{ce}}(\mathbf{y}_j,\
\subsubsection{学习率策略}
\parinterval 在公式\ref{eq:6-30}中, $\alpha$决定了每次参数更新时更新的步幅大小,称之为{\small\bfnew{学习率}}\index{学习率}(Learning Rate)\index{Learning Rate}。学习率作为基于梯度方法中的重要超参数,它决定目标函数能否收敛到较好的局部最优点以及收敛的速度。合理的学习率能够使模型快速、稳定地达到较好的状态。但是,如果学习率太小,收敛过程会很慢;而学习率太大,则模型的状态可能会出现震荡,很难达到稳定,甚至使模型无法收敛。图\ref{fig:6-28} 对比了不同学习率对损失函数的影响。
\parinterval 在公式\ref{eq:6-30}中, $\alpha$决定了每次参数更新时更新的步幅大小,称之为{\small\bfnew{学习率}}\index{学习率}(Learning Rate)\index{Learning Rate}。学习率作为基于梯度方法中的重要超参数,它决定目标函数能否收敛到较好的局部最优点以及收敛的速度。合理的学习率能够使模型快速、稳定地达到较好的状态。但是,如果学习率太小,收敛过程会很慢;而学习率太大,则模型的状态可能会出现震荡,很难达到稳定,甚至使模型无法收敛。图\ref{fig:6-28} 对比了不同学习率对优化过程的影响。
%----------------------------------------------
\begin{figure}[htp]
......@@ -1075,7 +1075,7 @@ L(\mathbf{Y},\widehat{\mathbf{Y}}) = \sum_{j=1}^n L_{\textrm{ce}}(\mathbf{y}_j,\
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Relationship-between-learning-rate-and-number-of-updates}
\input{./Chapter6/Figures/figure-relationship-between-learning-rate-and-number-of-updates}
\caption{学习率与更新次数的变化关系}
\label{fig:6-29}
\end{figure}
......@@ -1088,7 +1088,7 @@ L(\mathbf{Y},\widehat{\mathbf{Y}}) = \sum_{j=1}^n L_{\textrm{ce}}(\mathbf{y}_j,\
\end{eqnarray}
%-------
\noindent 另一方面,当模型训练逐渐接近收敛的时候,使用太大学习率会很容易让模型在局部最优解附近震荡,从而错过局部极小,因此需要通过减小学习率来调整更新的步长,以此来不断的逼近局部最优,这一阶段也称为学习率的衰减阶段。学习率衰减的方法有很多,比如指数衰减,余弦衰减等,图\ref{fig:6-29}展示的是{\small\bfnew{分段常数衰减}}\index{分段常数衰减}(Piecewise Constant Decay)\index{Piecewise Constant Decay},即每经过$m$次更新,学习率衰减为原来的$\beta_m$$\beta_m<1$)倍,其中$m$$\beta_m$为经验设置的超参。
\noindent 另一方面,当模型训练逐渐接近收敛的时候,使用太大学习率会很容易让模型在局部最优解附近震荡,从而错过局部极小,因此需要通过减小学习率来调整更新的步长,以此来不断的逼近局部最优,这一阶段也称为学习率的衰减阶段。学习率衰减的方法有很多,比如指数衰减,余弦衰减等,图\ref{fig:6-29}右侧展示的是{\small\bfnew{分段常数衰减}}\index{分段常数衰减}(Piecewise Constant Decay)\index{Piecewise Constant Decay},即每经过$m$次更新,学习率衰减为原来的$\beta_m$$\beta_m<1$)倍,其中$m$$\beta_m$为经验设置的超参。
%----------------------------------------------------------------------------------------
% NEW SUBSUB-SECTION
......@@ -1120,7 +1120,7 @@ L(\mathbf{Y},\widehat{\mathbf{Y}}) = \sum_{j=1}^n L_{\textrm{ce}}(\mathbf{y}_j,\
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Data-parallel-process}
\input{./Chapter6/Figures/figure-data-parallel-process}
\caption{数据并行过程}
\label{fig:6-30}
\end{figure}
......@@ -1185,7 +1185,7 @@ L(\mathbf{Y},\widehat{\mathbf{Y}}) = \sum_{j=1}^n L_{\textrm{ce}}(\mathbf{y}_j,\
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Decoding-process-based-on-greedy-method}
\input{./Chapter6/Figures/figure-decoding-process-based-on-greedy-method}
\caption{基于贪婪方法的解码过程}
\label{fig:6-32}
\end{figure}
......@@ -1196,7 +1196,7 @@ L(\mathbf{Y},\widehat{\mathbf{Y}}) = \sum_{j=1}^n L_{\textrm{ce}}(\mathbf{y}_j,\
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Decode-the-word-probability-distribution-at-the-first-position}
\input{./Chapter6/Figures/figure-decode-the-word-probability-distribution-at-the-first-position}
\caption{解码第一个位置输出的单词概率分布(``Have''的概率最高)}
\label{fig:6-33}
\end{figure}
......@@ -1222,7 +1222,7 @@ L(\mathbf{Y},\widehat{\mathbf{Y}}) = \sum_{j=1}^n L_{\textrm{ce}}(\mathbf{y}_j,\
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Beam-search-process}
\input{./Chapter6/Figures/figure-beam-search-process}
\caption{束搜索过程}
\label{fig:6-34}
\end{figure}
......@@ -1365,7 +1365,7 @@ L(\mathbf{Y},\widehat{\mathbf{Y}}) = \sum_{j=1}^n L_{\textrm{ce}}(\mathbf{y}_j,\
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Dependencies-between-words-in-a-recurrent-neural-network}
\input{./Chapter6/Figures/figure-dependencies-between-words-in-a-recurrent-neural-network}
\caption{循环神经网络中单词之间的依赖关系}
\label{fig:6-36}
\end{figure}
......@@ -1376,7 +1376,7 @@ L(\mathbf{Y},\widehat{\mathbf{Y}}) = \sum_{j=1}^n L_{\textrm{ce}}(\mathbf{y}_j,\
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Dependencies-between-words-of-Attention}
\input{./Chapter6/Figures/figure-dependencies-between-words-of-attention}
\caption{自注意力机制中单词之间的依赖关系}
\label{fig:6-37}
\end{figure}
......@@ -1387,7 +1387,7 @@ L(\mathbf{Y},\widehat{\mathbf{Y}}) = \sum_{j=1}^n L_{\textrm{ce}}(\mathbf{y}_j,\
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Example-of-self-attention-mechanism-calculation}
\input{./Chapter6/Figures/figure-example-of-self-attention-mechanism-calculation}
\caption{自注意力计算实例}
\label{fig:6-38}
\end{figure}
......@@ -1461,13 +1461,13 @@ L(\mathbf{Y},\widehat{\mathbf{Y}}) = \sum_{j=1}^n L_{\textrm{ce}}(\mathbf{y}_j,\
\subsection{位置编码}
\parinterval 在使用循环神经网络进行序列的信息提取时,每个时刻的运算都要依赖前一个时刻的输出,具有一定的时序性,这也与语言具有顺序的特点相契合。而采用自注意力机制对源语言和目标语言序列进行处理时,直接对当前位置和序列中的任意位置进行建模,忽略了词之间的顺序关系,例如图\ref{fig:6-41}中两个语义不同的句子,通过自注意力得到的表示$\mathbf{C}$(``机票'')却是相同的。
\parinterval 在使用循环神经网络进行序列的信息提取时,每个时刻的运算都要依赖前一个时刻的输出,具有一定的时序性,这也与语言具有顺序的特点相契合。而采用自注意力机制对源语言和目标语言序列进行处理时,直接对当前位置和序列中的任意位置进行建模,忽略了词之间的顺序关系,例如图\ref{fig:6-41}中两个语义不同的句子,通过自注意力得到的表示$\tilde{\mathbf{h}}$(``机票'')却是相同的。
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Calculation-of-context-vector-C}
\caption{上下文向量$\mathbf{C}$的计算}
\input{./Chapter6/Figures/figure-calculation-of-context-vector-C}
\caption{机票的更进一步抽象表示$\tilde{\mathbf{h}}$的计算}
\label{fig:6-41}
\end{figure}
%----------------------------------------------
......@@ -1477,7 +1477,7 @@ L(\mathbf{Y},\widehat{\mathbf{Y}}) = \sum_{j=1}^n L_{\textrm{ce}}(\mathbf{y}_j,\
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Transformer-input-and-position-encoding}
\input{./Chapter6/Figures/figure-transformer-input-and-position-encoding}
\caption{Transformer输入与位置编码}
\label{fig:6-42}
\end{figure}
......@@ -1498,7 +1498,7 @@ L(\mathbf{Y},\widehat{\mathbf{Y}}) = \sum_{j=1}^n L_{\textrm{ce}}(\mathbf{y}_j,\
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-A-combination-of-position-encoding-and-word-encoding}
\input{./Chapter6/Figures/figure-a-combination-of-position-encoding-and-word-encoding}
\caption{位置编码与词编码的组合}
\label{fig:6-43}
\end{figure}
......@@ -1531,7 +1531,7 @@ L(\mathbf{Y},\widehat{\mathbf{Y}}) = \sum_{j=1}^n L_{\textrm{ce}}(\mathbf{y}_j,\
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Position-of-self-attention-mechanism-in-the-model}
\input{./Chapter6/Figures/figure-position-of-self-attention-mechanism-in-the-model}
\caption{自注意力机制在模型中的位置}
\label{fig:6-44}
\end{figure}
......@@ -1560,7 +1560,7 @@ L(\mathbf{Y},\widehat{\mathbf{Y}}) = \sum_{j=1}^n L_{\textrm{ce}}(\mathbf{y}_j,\
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Point-product-attention-model}
\input{./Chapter6/Figures/figure-point-product-attention-model}
\caption{点乘注意力力模型 }
\label{fig:6-45}
\end{figure}
......@@ -1595,7 +1595,7 @@ L(\mathbf{Y},\widehat{\mathbf{Y}}) = \sum_{j=1}^n L_{\textrm{ce}}(\mathbf{y}_j,\
% 图3.10
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Mask-instance-for-future-positions-in-Transformer}
\input{./Chapter6/Figures/figure-mask-instance-for-future-positions-in-transformer}
\caption{Transformer中对于未来位置进行的屏蔽的Mask实例}
\label{fig:6-47}
\end{figure}
......@@ -1627,7 +1627,7 @@ L(\mathbf{Y},\widehat{\mathbf{Y}}) = \sum_{j=1}^n L_{\textrm{ce}}(\mathbf{y}_j,\
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Multi-Head-Attention-Model}
\input{./Chapter6/Figures/figure-multi-head-attention-model}
\caption{多头注意力模型}
\label{fig:6-48}
\end{figure}
......@@ -1655,7 +1655,7 @@ L(\mathbf{Y},\widehat{\mathbf{Y}}) = \sum_{j=1}^n L_{\textrm{ce}}(\mathbf{y}_j,\
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Residual-network-structure}
\input{./Chapter6/Figures/figure-residual-network-structure}
\caption{残差网络结构}
\label{fig:6-49}
\end{figure}
......@@ -1672,7 +1672,7 @@ x_{l+1} = x_l + \digamma (x_l)
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Position-of-difference-and-layer-regularization-in-the-model}
\input{./Chapter6/Figures/figure-position-of-difference-and-layer-regularization-in-the-model}
\caption{残差和层正则化在模型中的位置}
\label{fig:6-50}
\end{figure}
......@@ -1686,12 +1686,12 @@ x_{l+1} = x_l + \digamma (x_l)
\noindent 该公式使用均值$\mu$和方差$\sigma$对样本进行平移缩放,将数据规范化为均值为0,方差为1的标准分布。$g$$b$是可学习的参数。
\parinterval 在Transformer中经常使用的层正则化操作有两种结构,分别是{\small\bfnew{后正则化}}\index{后正则化}(Post-norm)\index{Post-norm}{\small\bfnew{前正则化}}\index{前正则化}(Pre-norm)\index{Pre-norm}。后正则化中先进行残差连接再进行层正则化,而前正则化则是在子层输入之前进行层正则化操作。在很多实践中已经发现,前正则化的方式更有利于信息传递,因此适合训练深层的Transformer模型\upcite{WangLearning}
\parinterval 在Transformer中经常使用的层正则化操作有两种结构,分别是{\small\bfnew{后正则化}}\index{后正则化}(Post-norm)\index{Post-norm}{\small\bfnew{前正则化}}\index{前正则化}(Pre-norm)\index{Pre-norm},结构如图\ref{fig:6-51}所示。后正则化中先进行残差连接再进行层正则化,而前正则化则是在子层输入之前进行层正则化操作。在很多实践中已经发现,前正则化的方式更有利于信息传递,因此适合训练深层的Transformer模型\upcite{WangLearning}
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Different-regularization-methods}
\input{./Chapter6/Figures/figure-different-regularization-methods}
\caption{不同正则化方式 }
\label{fig:6-51}
\end{figure}
......@@ -1708,7 +1708,7 @@ x_{l+1} = x_l + \digamma (x_l)
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Position-of-feedforward-neural-network-in-the-model}
\input{./Chapter6/Figures/figure-position-of-feedforward-neural-network-in-the-model}
\caption{前馈神经网络在模型中的位置}
\label{fig:6-52}
\end{figure}
......@@ -1728,15 +1728,15 @@ x_{l+1} = x_l + \digamma (x_l)
\subsection{训练}
\parinterval 与前面介绍的神经机器翻译模型的训练一样,Transformer的训练流程为:首先对模型进行初始化,然后在编码器输入包含结束符的源语言单词序列。前面已经介绍过,解码端每个位置单词的预测都要依赖已经生成的序列。在解码端输入包含起始符号的目标语序列,通过起始符号预测目标语的第一个单词,用真实的目标语第一个单词去预测第二个单词,以此类推,然后用真实的目标语序列和预测的结果比较,计算它的损失。损失越小说明模型的预测越接近真实输出。然后利用反向传播来调整模型中的参数。Transformer使用了{\small\bfnew{交叉熵损失}}\index{交叉熵损失}(Cross Entropy Loss)\index{Cross Entropy Loss}函数,如图\ref{fig:6-53}。由于Transformer 将任意时刻输入信息之间的距离拉近为1,摒弃了RNN中每一个时刻的计算都要基于前一时刻的计算这种具有时序性的训练方式,因此Transformer中训练的不同位置可以并行化训练,大大提高了训练效率。
\parinterval 与前面介绍的神经机器翻译模型的训练一样,Transformer的训练流程为:首先对模型进行初始化,然后在编码器输入包含结束符的源语言单词序列。前面已经介绍过,解码端每个位置单词的预测都要依赖已经生成的序列。在解码端输入包含起始符号的目标语序列,通过起始符号预测目标语的第一个单词,用真实的目标语第一个单词去预测第二个单词,以此类推,然后用真实的目标语序列和预测的结果比较,计算它的损失。Transformer使用了{\small\bfnew{交叉熵损失}}\index{交叉熵损失}(Cross Entropy Loss)\index{Cross Entropy Loss}函数,损失越小说明模型的预测越接近真实输出。然后利用反向传播来调整模型中的参数。由于Transformer 将任意时刻输入信息之间的距离拉近为1,摒弃了RNN中每一个时刻的计算都要基于前一时刻的计算这种具有时序性的训练方式,因此Transformer中训练的不同位置可以并行化训练,大大提高了训练效率。
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Structure-of-the-network-during-Transformer-training}
\caption{Transformer训练时网络的结构}
\label{fig:6-53}
\end{figure}
%\begin{figure}[htp]
%\centering
%\input{./Chapter6/Figures/figure-structure-of-the-network-during-transformer-training}
%\caption{Transformer训练时网络的结构}
%\label{fig:6-53}
%\end{figure}
%----------------------------------------------
\parinterval 需要注意的时候,Transformer也包含很多工程方面的技巧。首先,在训练优化器方面,需要注意以下几点:
......@@ -1774,7 +1774,7 @@ lrate = d_{model}^{-0.5} \cdot \textrm{min} (step^{-0.5} , step \cdot warmup\_st
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Comparison-of-the-number-of-padding-in-batch}
\input{./Chapter6/Figures/figure-comparison-of-the-number-of-padding-in-batch}
\caption{batch中padding数量对比(白色部分为padding)}
\label{fig:6-55}
\end{figure}
......@@ -1864,7 +1864,7 @@ Transformer Deep(48层) & 30.2 & 43.1 & 194$\times 10^{6}$
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Generate-summary}
\input{./Chapter6/Figures/figure-generate-summary}
\caption{文本自动摘要实例}
\label{fig:6-57}
\end{figure}
......@@ -1876,12 +1876,12 @@ Transformer Deep(48层) & 30.2 & 43.1 & 194$\times 10^{6}$
\subsection{文言文翻译}
\parinterval 文言文翻译,即能够根据输入的文言文,输出相应的现代汉语翻译。中国几千年的文化都是用文言文记载的,不同时代的古文之间存在巨大差异,普通人在阅读古籍时会面临很大困难。为此,有研究者致力于将古籍翻译成现代汉语。实际上,文言文翻译也是机器翻译的一个典型应用。想要训练一个文言文翻译系统并不难,只需要将文言文看作源语言,将现代文看作目标语言,并送入机器翻译模型,就可以获得一个古文翻译系统。不过,由于古文短,现代文长,过翻译或欠翻译等问题会在古文翻译中表现得更为突出。在此,如果想要获得一个性能优异的文言文翻译系统,就需要考虑如何对长度进行更精准的建模。另外,不同时代语言差异性大,因此还需要能够进行自动适应和风格迁移。下图展示了使用神经机器翻译模型得到的文言文翻译系统的实例。输入古文,系统就能生成其现代文翻译。当然,也可以用类似的方法训练现代文-古文的翻译系统。
\parinterval 文言文翻译,即能够根据输入的文言文,输出相应的现代汉语翻译。中国几千年的文化都是用文言文记载的,不同时代的古文之间存在巨大差异,普通人在阅读古籍时会面临很大困难。为此,有研究者致力于将古籍翻译成现代汉语。实际上,文言文翻译也是机器翻译的一个典型应用。想要训练一个文言文翻译系统并不难,只需要将文言文看作源语言,将现代文看作目标语言,并送入机器翻译模型,就可以获得一个古文翻译系统。不过,由于古文短,现代文长,过翻译或欠翻译等问题会在古文翻译中表现得更为突出。在此,如果想要获得一个性能优异的文言文翻译系统,就需要考虑如何对长度进行更精准的建模。另外,不同时代语言差异性大,因此还需要能够进行自动适应和风格迁移。\ref{fig:6-58}展示了使用神经机器翻译模型得到的文言文翻译系统的实例。输入古文,系统就能生成其现代文翻译。当然,也可以用类似的方法训练现代文-古文的翻译系统。
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Example-of-automatic-translation-of-classical-Chinese}
\input{./Chapter6/Figures/figure-example-of-automatic-translation-of-classical-Chinese}
\caption{文言文自动翻译实例}
\label{fig:6-58}
\end{figure}
......@@ -1898,7 +1898,7 @@ Transformer Deep(48层) & 30.2 & 43.1 & 194$\times 10^{6}$
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Automatically-generate-instances-of-couplets}
\input{./Chapter6/Figures/figure-automatically-generate-instances-of-couplets}
\caption{对联自动生成实例(人工给定上联)}
\label{fig:6-59}
\end{figure}
......@@ -1915,7 +1915,7 @@ Transformer Deep(48层) & 30.2 & 43.1 & 194$\times 10^{6}$
%----------------------------------------------
\begin{figure}[htp]
\centering
\input{./Chapter6/Figures/figure-Automatic-generation-of-ancient-poems-based-on-encoder-decoder-framework}
\input{./Chapter6/Figures/figure-automatic-generation-of-ancient-poems-based-on-encoder-decoder-framework}
\caption{基于编码器-解码器框架的古诗自动生成}
\label{fig:6-60}
\end{figure}
......
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{tikzpicture}
\setlength{\base}{0.9cm}
\setlength{\base}{1cm}
\tikzstyle{rnnnode} = [rounded corners=1pt,minimum size=0.5\base,draw,inner sep=0pt,outer sep=0pt]
\tikzstyle{wordnode} = [font=\scriptsize]
\tikzstyle{wordnode} = [font=\footnotesize]
% RNN translation model
\begin{scope}[local bounding box=RNNMT]
......@@ -20,7 +14,7 @@
\node[rnnnode,minimum height=0.5\base,fill=green!30!white,anchor=west] (eemb\x) at ([xshift=0.4\base]eemb\y.east) {};
\foreach \x in {1,2,...,4}
\node[rnnnode,fill=blue!30!white,anchor=south] (enc\x) at ([yshift=0.5\base]eemb\x.north) {};
\node[wordnode,left=0.4\base of enc1,font=\scriptsize] (init) {0};
\node[wordnode,left=0.4\base of enc1,font=\footnotesize] (init) {0};
\node[wordnode,anchor=east] (init2) at ([xshift=-3.0em]init.west){};
{
\node[rnnnode,fill=purple] (repr) at (enc4) {};
......@@ -32,50 +26,50 @@
\draw[->,dashed,thick] (label.south) -- (enc4.north);
}
\node[wordnode,below=0pt of eemb1,font=\scriptsize] (encwordin1) {};
\node[wordnode,below=0pt of eemb2,font=\scriptsize] (encwordin2) {};
\node[wordnode,below=0pt of eemb3,font=\scriptsize] (encwordin3) {};
\node[wordnode,below=0pt of eemb4,font=\scriptsize] (encwordin4) {$\langle$eos$\rangle$};
\node[wordnode,below=0pt of eemb1,font=\footnotesize] (encwordin1) {};
\node[wordnode,below=0pt of eemb2,font=\footnotesize] (encwordin2) {};
\node[wordnode,below=0pt of eemb3,font=\footnotesize] (encwordin3) {};
\node[wordnode,below=0pt of eemb4,font=\footnotesize] (encwordin4) {$\langle$eos$\rangle$};
%大括号
\draw[decorate,thick,decoration={mirror,brace}]([xshift=0.0em,yshift=-1.5em]eemb1.south west) --([xshift=0.0em,yshift=-1.5em]eemb4.south east) node [font=\scriptsize,xshift=-3.8em,yshift=-1.0em,align=center](label2) {编码器};
\draw[decorate,thick,decoration={mirror,brace}]([xshift=0.0em,yshift=-1.5em]eemb1.south west) --([xshift=0.0em,yshift=-1.5em]eemb4.south east) node [font=\footnotesize,xshift=-3.8em,yshift=-1.0em,align=center](label2) {编码器};
% RNN Decoder
\foreach \x in {1,2,...,4}
\node[rnnnode,minimum height=0.5\base,fill=green!30!white,anchor=south] (demb\x) at ([xshift=9.0em,yshift=-3.5em]enc\x.north) {};
\node[rnnnode,minimum height=0.5\base,fill=green!30!white,anchor=south] (demb\x) at ([xshift=9.5em,yshift=-3.9em]enc\x.north) {};
\foreach \x in {1,2,...,4}
\node[rnnnode,fill=blue!30!white,anchor=south] (dec\x) at ([yshift=0.5\base]demb\x.north) {};
\foreach \x in {1,2,...,4}
\node[rnnnode,minimum height=0.5\base,fill=red!30!white,anchor=south] (softmax\x) at ([yshift=0.5\base]dec\x.north) {};
% Decoder input words
\node[wordnode,below=0pt of demb1,font=\scriptsize] (decwordin) {$\langle$sos$\rangle$};
\node[wordnode,below=0pt of demb1,font=\footnotesize] (decwordin) {$\langle$sos$\rangle$};
\ExtractX{$(demb2.south)$}
\ExtractY{$(decwordin.base)$}
\node[wordnode,anchor=base,font=\scriptsize] () at (\XCoord,\YCoord) {I};
\node[wordnode,anchor=base,font=\footnotesize] () at (\XCoord,\YCoord) {I};
\ExtractX{$(demb3.south)$}
\ExtractY{$(decwordin.base)$}
\node[wordnode,anchor=base,font=\scriptsize] () at (\XCoord,\YCoord) {am};
\node[wordnode,anchor=base,font=\footnotesize] () at (\XCoord,\YCoord) {am};
\ExtractX{$(demb4.south)$}
\ExtractY{$(decwordin.base)$}
\node[wordnode,anchor=base,font=\scriptsize] () at (\XCoord,\YCoord) {fine};
\node[wordnode,anchor=base,font=\footnotesize] () at (\XCoord,\YCoord) {fine};
% Decoder output words
\node[wordnode,above=0pt of softmax1,font=\scriptsize] (decwordout1) {I};
\node[wordnode,above=0pt of softmax1,font=\footnotesize] (decwordout1) {I};
\ExtractX{$(softmax2.north)$}
\ExtractY{$(decwordout1.base)$}
\node[wordnode,anchor=base,font=\scriptsize] (decwordout2) at (\XCoord,\YCoord) {am};
\node[wordnode,anchor=base,font=\footnotesize] (decwordout2) at (\XCoord,\YCoord) {am};
\ExtractX{$(softmax3.north)$}
\ExtractY{$(decwordout1.base)$}
\node[wordnode,anchor=base,font=\scriptsize] (decwordout3) at (\XCoord,\YCoord) {fine};
\node[wordnode,anchor=base,font=\footnotesize] (decwordout3) at (\XCoord,\YCoord) {fine};
\ExtractX{$(softmax4.north)$}
\ExtractY{$(decwordout1.base)$}
\node[wordnode,anchor=base,font=\scriptsize] (decwordout4) at (\XCoord,\YCoord) {$\langle$eos$\rangle$};
\node[wordnode,anchor=base,font=\footnotesize] (decwordout4) at (\XCoord,\YCoord) {$\langle$eos$\rangle$};
%大括号
\draw[decorate,thick,decoration={brace}]([xshift=0.0em,yshift=1.3em]softmax1.north west) --([xshift=0.0em,yshift=1.3em]softmax4.north east) node [font=\scriptsize,xshift=-3.8em,yshift=1.0em,align=center](label1) {解码器};
\draw[decorate,thick,decoration={brace}]([xshift=0.0em,yshift=1.3em]softmax1.north west) --([xshift=0.0em,yshift=1.3em]softmax4.north east) node [font=\footnotesize,xshift=-3.8em,yshift=1.0em,align=center](label1) {解码器};
% Connections
\draw[-latex'] (init.east) to (enc1.west);
......@@ -93,40 +87,7 @@
\coordinate (bridge) at ([yshift=-1.15\base]demb2);
\draw[-latex'] (enc4.east) -- (dec1.west);
\node[] at ([xshift=1.5cm]dec4.east){};
\end{scope}
\end{tikzpicture}
\ No newline at end of file
......@@ -62,8 +62,8 @@
\node [rectangle,inner sep=0.7em,rounded corners=1pt,very thick,dotted,draw=ugreen!70] [fit = (sa1) (res1) (ffn1) (res2)] (box0) {};
\node [rectangle,inner sep=0.7em,rounded corners=1pt,very thick,dotted,draw=red!60] [fit = (sa2) (res3) (res5)] (box1) {};
\node [ugreen] (count) at ([xshift=-1.7em,yshift=-1em]encoder.south) {$6\times$};
\node [red] (count) at ([xshift=11em,yshift=0em]decoder.south) {$\times 6$};
\node [ugreen,font=\scriptsize] (count) at ([xshift=-1.5em,yshift=-1em]encoder.south) {$6\times$};
\node [red,font=\scriptsize] (count) at ([xshift=10.8em,yshift=0em]decoder.south) {$\times 6$};
\end{scope}
\end{tikzpicture}
\ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论