Source-linked AI summary

Attention Is All You Need

Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Lukasz Kaiser, Illia Polosukhin

arXiv:1706.03762v7cs.CLcs.LG

TL;DR

Sequence transduction model은 순차적인 recurrent 또는 convolutional 연산에 의존하므로 학습 중 병렬화가 제한된다. 이 논문은 attention만 사용하는 Transformer를 제안하며, 두 WMT 2014 과제에서 state-of-the-art 번역 품질을 달성하는 동시에 훨씬 빠르게 학습된다.

  • 문제

    Recurrent sequence model의 순차적 연산은 학습 예제 내부의 병렬화를 제한하며, 특히 더 긴 sequence에서 두드러진다.

  • 방법

    Transformer는 recurrent layer를 multi-headed self-attention으로 대체해 sequence representation을 계산하고 전역 dependency를 포착한다.

  • 결과

    WMT 2014 English-to-German에서 28.4 BLEU를 기록해 새로운 state of the art를 수립했으며, English-to-French에서도 state-of-the-art 성능을 달성했다.

  • 핵심 시사점 및 한계

    번역에서 Transformer는 recurrent 또는 convolutional architecture보다 훨씬 빠르게 학습되면서도 state-of-the-art 품질을 달성할 수 있다.

Abstract

from arXiv · show

The dominant sequence transduction models are based on complex recurrent or convolutional neural networks in an encoder-decoder configuration. The best performing models also connect the encoder and decoder through an attention mechanism. We propose a new simple network architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two machine translation tasks show these models to be superior in quality while being more parallelizable and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task, improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training costs of the best models from the literature. We show that the Transformer generalizes well to other tasks by applying it successfully to English constituency parsing both with large and limited training data.

1 서론

Transformer는 recurrent sequence modeling을 attention만으로 대체해 recurrent computation의 제한적인 병렬화를 해결하고 전역 input-output dependency를 포착한다. 8개의 P100 GPU에서 불과 12시간 만에 더 높은 병렬화 수준과 새로운 translation state-of-the-art 성능을 달성한다.

  • 1 서론: Recurrent 및 gated architecture는 language modeling과 machine translation을 비롯한 sequence modeling 및 transduction의 state-of-the-art 접근법으로 자리 잡았다 [7] [2] [5].후속 연구에서는 recurrent language model과 encoder-decoder architecture를 계속 개선했다 [38] [24] [15].
  • 1 서론: Recurrent model은 sequence position에 걸쳐 computation을 인수분해하므로 각 hidden state가 이전 state에 의존하며 training example 내부의 병렬화를 막는다.memory constraint가 example 간 batching을 제한하기 때문에 sequence가 길수록 이 한계는 더욱 중요해진다.
  • 1 서론: Transformer는 recurrence를 배제하고 input과 output 사이의 전역 dependency를 포착하는 데 전적으로 attention에 의존한다.Attention mechanism은 input 또는 output sequence에서 거리에 관계없이 dependency를 model한다 [2, 19].
  • 1 서론: Transformer는 훨씬 더 높은 병렬화를 가능하게 하며 8개의 P100 GPU에서 불과 12시간 만에 새로운 translation state-of-the-art 성능에 도달한다.이 설계는 attention을 recurrent network와 결합하는 대신 recurrent component를 대체한다.

2 배경

기존 접근법은 convolutional 또는 recurrent attention mechanism으로 순차 계산을 줄였으며, self-attention은 이미 여러 언어 과제에서 성공을 거두고 있었다. Transformer는 sequence-aligned recurrence나 convolution 없이 self-attention만을 사용하는 최초의 transduction model로 제시된다.

  • Convolutional model: Extended Neural GPU, ByteNet [18], ConvS2S [9]는 순차 계산을 줄이면서 모든 입력 및 출력 hidden representation을 병렬로 계산하기 위해 convolution을 사용한다.임의의 위치를 연결하려면 ConvS2S에서는 거리에 따라 선형적으로, ByteNet에서는 로그적으로 증가하는 연산이 필요하다.
  • Self-attention: Self-attention은 하나의 sequence 내 위치들을 연결해 그 representation을 계산하며, 독해, 요약, 텍스트 함의, 문장 representation 학습에서 성공을 거두었다 [4] [28] [22].
  • 관련 attention model: End-to-end memory network는 sequence-aligned recurrence 대신 recurrent attention을 사용하며, 단순 언어 질의응답과 language modeling에서 좋은 성능을 보인다.
  • 새로운 점: Transformer는 sequence-aligned RNNs나 convolution 없이 전적으로 self-attention에 의존하는 최초의 transduction model로 설명된다.이 논문은 self-attention과 [17] [18], [9]와 같은 model에 대한 그 장점을 제시한다.

3 모델 아키텍처 · 3.1 Encoder와 Decoder 스택 · 3.2 Attention

Transformer는 Encoder와 Decoder 스택에서 recurrent 또는 convolutional sequence transduction을 stacked self-attention과 point-wise fully connected layer로 대체한다. Attention mechanism은 autoregressive decoding을 지원하기 위해 scaled dot product, 여러 병렬 head, masking을 사용한다.

  • 3 모델 아키텍처: Transformer는 encoder-decoder architecture [5] [2]를 따라 Encoder와 Decoder 모두에 stacked self-attention과 point-wise fully connected layer를 사용한다.Encoder는 입력 symbol을 연속 표현으로 매핑하고, autoregressive Decoder는 출력 symbol을 순차적으로 생성한다 [10].
  • 3.1 Encoder와 Decoder 스택: Encoder는 N = 6개의 동일한 layer로 구성되며, 각 layer는 multi-head self-attention과 position-wise feed-forward network, residual connection, layer normalization을 결합한다 [11] [1].각 sub-layer는 LayerNorm(x + Sublayer(x))를 사용한다.
  • 3.1 Encoder와 Decoder 스택: Decoder는 N = 6개의 layer로 구성되며, encoder-decoder attention, residual connection, layer normalization, 그리고 이후 position에 접근하지 못하게 하는 masked self-attention을 포함한다.Masking은 허용되지 않는 미래 position 연결을 차단하여 autoregressive generation을 보존한다.
  • 3.2 Attention: Scaled Dot-Product Attention은 query-key dot product를 계산하고, 이를 √dk로 나눈 뒤 softmax weight를 적용하여 value의 weighted sum을 구성한다.Scaling은 큰 dk에서 큰 dot product가 softmax를 gradient가 극도로 작은 영역으로 밀어 넣는 현상을 완화한다.
  • 3.2.2 Multi-Head Attention: Multi-head attention은 query, key, value를 여러 개의 학습된 저차원 subspace로 projection하고, 이를 병렬로 처리한 뒤 출력을 concatenate하여 다시 projection한다.이를 통해 모델은 서로 다른 representation subspace와 position에 공동으로 attend할 수 있으며, 단일 head의 averaging 한계를 피한다.
  • 3.2.2 Multi-Head Attention: 모델은 h = 8개의 attention head를 사용하며 dk = dv = dmodel/h = 64로 설정하여, full-dimensional single-head attention과 전체 계산 비용을 유사하게 유지한다.각 head의 감소된 차원이 head를 병렬로 실행하는 비용을 상쇄한다.
  • 3.2.3 우리 모델에서의 Attention 적용: Attention은 모든 입력 position에 대한 encoder-decoder attention, 이전 layer에 대한 encoder self-attention, 현재 position까지의 position에 대한 decoder self-attention으로 적용된다.Decoder self-attention은 softmax 입력을 −∞로 설정하여 허용되지 않는 연결을 masking하고, left-to-right 정보 흐름을 보존한다.

3.3 위치별 피드포워드 네트워크

각 encoder와 decoder layer는 attention과 함께 위치별 피드포워드 네트워크를 추가한다. 이 네트워크는 중간에 ReLU를 두고 두 번의 선형 변환을 적용하며, 위치 간에는 파라미터를 공유하고 layer 간에는 서로 다른 파라미터를 사용한다.

  • 3.3 Position-wise Feed-Forward Networks: 각 encoder와 decoder layer에는 모든 position에 독립적이고 동일하게 적용되는 position-wise feed-forward network가 포함된다.이 network는 두 개의 linear transformation 사이에 ReLU activation을 둔 구조다.
  • 3.3 위치별 피드포워드 네트워크: 피드포워드 네트워크는 위치 간에는 동일한 선형 변환 파라미터를 사용하지만 layer마다 서로 다른 파라미터를 사용한다.이는 kernel size가 1인 두 개의 convolution으로도 설명할 수 있다.
  • 3.3 위치별 피드포워드 네트워크: 모델은 입력 및 출력 표현에 d_model = 512를 사용하고, 피드포워드 네트워크의 내부 layer에는 d_ff = 2048을 사용한다.

3.4 임베딩과 Softmax

Transformer는 학습된 token embedding과 Softmax를 적용한 학습된 선형 변환을 사용해 다음 token의 확률을 산출한다. 두 embedding layer와 pre-softmax 변환에 하나의 weight matrix를 공유하며, embedding weight에 √dmodel을 곱해 스케일링한다.

  • 3.4 임베딩과 Softmax: 학습된 embedding은 입력 및 출력 token을 dmodel 차원의 벡터로 매핑하고, 학습된 선형 변환과 Softmax는 예측된 다음 token의 확률을 산출한다.이 구성요소들은 표준 sequence transduction 설정을 따른다.
  • 3.4 임베딩과 Softmax: 모델은 [30]을 따라 두 embedding layer와 pre-softmax 선형 변환 전체에 하나의 weight matrix를 공유한다.
  • 3.4 임베딩과 Softmax: Embedding-layer weight는 사용하기 전에 √dmodel을 곱한다.

3.5 위치 인코딩

Transformer는 recurrence와 convolution이 없기 때문에 토큰 순서를 표현하기 위해 encoder와 decoder의 입력 임베딩에 위치 인코딩을 추가한다. 이 논문은 사인파 인코딩을 사용하며, 그 상대 위치 구조가 attention과 학습 길이를 넘어선 extrapolation을 지원할 수 있다고 본다.

  • 3.5 위치 인코딩: 사인파 위치 인코딩은 encoder와 decoder의 입력 임베딩에 순서 정보를 추가하며, 임베딩과 같은 차원을 사용하므로 두 값을 더할 수 있다.모델에 recurrence와 convolution이 모두 없기 때문에 이 인코딩이 필요하다.
  • 3.5 위치 인코딩: 각 인코딩 차원은 파장이 2π에서 10000 · 2π까지 기하급수적으로 증가하는 사인파다.고정된 k에 대해 PEpos+k가 PEpos로부터 선형적으로 표현될 수 있으므로, 이 설계가 모델의 상대 위치 기반 attention 학습을 돕는다고 가정했다.
  • 3.5 위치 인코딩: 학습된 위치 임베딩 [9]과 사인파 인코딩은 거의 동일한 결과를 냈지만, 길이 extrapolation 가능성 때문에 사인파 버전을 선택했다.비교 결과는 Table 3 row (E)에 보고되어 있다.

4 Self-Attention의 장점

이 절은 recurrent 및 convolutional layer와 비교해 계산 복잡도, 병렬화, 장거리 의존성을 위한 경로 길이를 분석함으로써 self-attention의 동기를 제시한다. Self-attention은 일정한 sequential depth, 일반적인 문장 길이에서 유리한 복잡도, 그리고 해석 가능한 attention pattern을 제공할 가능성이 있다.

  • 비교에서는 layer를 layer당 계산 복잡도, 병렬화 가능한 계산, 그리고 장거리 의존성을 학습하는 데 필요한 경로 길이 로 평가한다.경로가 짧을수록 장거리 의존성을 학습하기 쉽다.
  • Self-attention은 일정한 수의 sequential operation으로 모든 input-output position을 연결하는 반면, recurrent layer는 O(n)의 sequential operation을 필요로 한다.
  • Self-attention은 sequence length n이 representation dimensionality d보다 작을 때 recurrent layer보다 빠르며, 이는 word-piece 및 byte-pair sentence representation에서 일반적으로 나타난다.매우 긴 sequence에서는 attention을 크기 r인 neighborhood로 제한하면 계산 성능을 높일 수 있지만 maximum path length가 증가한다.
  • Convolutional layer는 모든 position을 연결하려면 O(n/k)의 contiguous-kernel layer 또는 O(logk(n))의 dilated-convolution layer가 필요하며, 일반적으로 recurrent layer보다 k배 더 많은 비용이 든다.Separable convolution은 convolutional complexity를 낮춘다.
  • Attention distribution은 개별 head가 서로 다른 task를 학습하고 흔히 syntactic 또는 semantic behavior를 보이기 때문에 해석 가능성을 높일 수 있다.이 절은 이러한 pattern이 appendix에서 예시와 함께 설명되고 논의된다고 언급한다.

5 학습

Transformer는 Adam, warmup 및 decay learning-rate schedule, regularization을 사용해 WMT 2014 translation dataset으로 학습했다. 학습에는 NVIDIA P100 GPU 8개가 사용됐으며, configuration에 따라 12시간에서 3.5일까지 걸렸고, 더 낮은 학습 비용으로 기존 state-of-the-art model보다 높은 BLEU score를 달성했다.

  • 학습에는 약 4.5 million개의 English-German sentence pair와, shared 37,000-token byte-pair vocabulary를 사용했으며, 32,000-token word-piece vocabulary를 사용한 36 million개의 English-French sentence도 사용했다.Sentence pair는 대략적인 sequence length별로 batch를 구성했다.
  • NVIDIA P100 GPU 8개가 장착된 한 machine에서 base model은 100,000 step을 12시간 동안 학습했고, big model은 300,000 step을 3.5일에 걸쳐 학습했다.Base-model step에는 약 0.4초, big-model step에는 1.0초가 걸렸다.
  • Optimization에는 β1 = 0.9, β2 = 0.98, ϵ = 10^-9인 Adam을 사용했으며, 4,000 warmup step 동안 learning rate를 높인 뒤 inverse-square-root decay를 적용했다.
  • Transformer는 WMT 2014 English-to-German 및 English-to-French test에서 기존 state-of-the-art model보다 더 높은 BLEU score를 학습 비용의 일부만으로 달성했다.
  • Regularization에는 base model에서 Pdrop = 0.1인 residual 및 embedding-position dropout과, ϵls = 0.1인 label smoothing이 포함됐다. 이 방법은 perplexity를 악화시켰지만 accuracy와 BLEU를 개선했다.

6 결과

Transformer는 훨씬 낮은 학습 비용으로 번역 분야의 state-of-the-art 결과를 달성하며, 구성 요소 연구를 통해 중요한 아키텍처 및 정규화 선택을 규명한다. 또한 English constituency parsing에도 잘 일반화되어 Recurrent Neural Network Grammar를 제외한 기존 시스템을 능가한다.

  • 번역 결과: WMT 2014 English-to-German에서 28.4 BLEU를 기록해 앙상블을 포함한 기존 최고 보고 모델을 2.0 BLEU 이상 능가한다.8개의 P100 GPU에서 3.5일 동안 학습했다.
  • 번역 결과: WMT 2014 English-to-French에서 41.0 BLEU를 기록해, 이전 최고 수준의 학습 비용의 사분의 일 미만으로 기존에 발표된 모든 단일 모델을 능가한다.English-to-French big model은 dropout rate Pdrop = 0.1을 사용했다.
  • 절제 연구: English-to-German 개발 데이터에서는 attention head가 너무 적거나 많으면 품질이 저하되고, 더 작은 key dimension은 성능을 해치며, 더 큰 모델과 dropout은 성능을 향상한다.Single-head attention은 최적 설정보다 0.9 BLEU 낮으며, 학습된 positional embedding은 sinusoidal encoding [9]과 거의 동일한 성능을 보인다.
  • English constituency parsing: English constituency parsing에서 Transformer는 Recurrent Neural Network Grammar [8]를 제외한 기존에 보고된 모든 모델을 능가한다.또한 40K-sentence WSJ training set만 사용해 학습했을 때 Berkeley-Parser 도 능가한다.

7 결론

Transformer는 multi-headed self-attention만을 기반으로 하는 sequence transduction model로, recurrent encoder-decoder layer를 대체한다. recurrent 또는 convolutional architecture보다 빠르게 학습되며 WMT 2014의 두 translation task 모두에서 state-of-the-art 성능을 달성한다.

  • 이 model은 전적으로 attention에 기반한 최초의 sequence transduction architecture로, recurrent layer를 multi-headed self-attention으로 대체한다.
  • Transformer는 WMT 2014 English-to-German 및 English-to-French에서 state-of-the-art 성능을 달성하며, English-to-German에서는 기존에 보고된 모든 ensemble을 능가한다.또한 recurrent 또는 convolutional layer에 기반한 architecture보다 훨씬 빠르게 학습할 수 있다.
  • 향후 연구에서는 attention 기반 model을 text 외의 영역으로 확장하고, 대규모 input과 output을 위한 restricted attention을 개발하며, generation을 덜 sequential하게 만들 예정이다.제안되는 modality에는 image, audio, video가 포함된다.

Attention 시각화

Attention 시각화는 encoder self-attention head가 장거리 의존성, 대용사 선행사 해결, 문장 구조와 관련된 서로 다른 동작을 학습한다는 점을 보여준다.

  • Attention 시각화: 5번째 layer의 encoder self-attention head는 장거리 의존성을 따라가며, 많은 head가 ‘making’과 ‘more difficult’를 연결하는 의존성에 attention을 둔다.시각화는 ‘making’이라는 단어에 대한 attention을 보여주며, 서로 다른 색은 서로 다른 head를 나타낸다.
  • Attention 시각화: 두 개의 5번째 layer attention head는 대용사 선행사 해결에 관여하는 것으로 보이며, ‘its’라는 단어에 특히 날카로운 attention을 생성한다.그림은 head 5의 전체 attention과 head 5 및 6에서 ‘its’로부터의 분리된 attention을 제시한다.
  • Attention 시각화: 많은 attention head가 문장 구조와 관련된 동작을 보이며, 서로 다른 head가 서로 다른 task를 학습한다는 점이 분명하다.예시는 6개 중 5번째 layer에 있는 두 encoder self-attention head에서 가져왔다.
Loading 1706.03762v7…