Em um artigo anterior, examinamos o mecanismo de atenção, um método extremamente comum em modelos modernos de aprendizado profundo que podem melhorar os indicadores de desempenho de aplicativos de tradução de máquina neural. Neste artigo, veremos o Transformer, um modelo que usa o mecanismo de atenção para aumentar a velocidade de aprendizado. Além disso, para várias tarefas, os Transformers superam o modelo de tradução automática neural do Google. No entanto, a maior vantagem dos transformadores é sua alta eficiência em condições de paralelização. Até o Google Cloud recomenda usar o Transformer como modelo ao trabalhar no Cloud TPU . Vamos tentar descobrir em que consiste o modelo e quais funções ele executa.
O modelo Transformer foi proposto pela primeira vez no artigo Atenção é tudo que você precisa . Uma implementação no TensorFlow está disponível como parte do pacote Tensor2Tensor . Além disso, um grupo de pesquisadores da PNL de Harvard criou uma anotação de guia do artigo com uma implementação no PyTorch . Neste mesmo guia, tentaremos declarar de maneira mais simples e consistente as principais idéias e conceitos que, esperamos, ajudem as pessoas que não têm um conhecimento profundo da área a entender esse modelo.
Revisão de Alto Nível
Vejamos o modelo como uma espécie de caixa preta. Em aplicativos de tradução automática, ele aceita uma frase em um idioma como entrada e exibe uma frase em outro.
![the_transformer_3](https://habrastorage.org/webt/vo/iv/w7/voivw7zwvgnsmmjzbxkn8ypdo2s.png)
, , , .
![The_transformer_encoders_decoders](https://habrastorage.org/webt/mj/3a/qq/mj3aqqrifxbnb8f5gv_r5jbdf7g.png)
– ; 6 , ( 6 , ). – , .
![The_transformer_encoder_decoder_stack](https://habrastorage.org/webt/-y/7z/tl/-y7ztlhyq5_817b89qhjjikwopk.png)
, . :
![Transformer_encoder](https://habrastorage.org/webt/2_/bq/if/2_bqifdpdhluoadxjo_ncvpgdcm.png)
, , (self-attention), . .
(feed-forward neural network). .
, , ( , seq2seq).
![Transformer_decoder](https://habrastorage.org/webt/34/f7/lm/34f7lmnxvdcchib66hhxbdx8vzg.png)
, , /, , .
NLP-, , , (word embeddings).
![casamentos](https://habrastorage.org/webt/po/lw/jl/polwjl7ap50biqcjrwegkczazy4.png)
512. .
. , , : 512 ( , – ). , , , , .
, .
![encoder_with_tensors](https://habrastorage.org/webt/a8/o9/z-/a8o9z-cte22jxysrcoqqfmbgyqu.png)
: . , , , .
, .
!
, , – , , , .
![encoder_with_tensors_2](https://habrastorage.org/webt/s7/qm/ln/s7qmln-nbaxuocg1rswbf6fjang.png)
. , .
, « » -, . , «Attention is All You Need». , .
– , :
”The animal didn't cross the street because it was too tired”
«it» ? (street) (animal)? .
«it», , «it» «animal».
( ), , .
(RNN), , RNN /, , . – , , «» .
![transformer_self-atenção_visualização](https://habrastorage.org/webt/pz/li/tj/pzlitjsvdvi0ehs5yuw0jmqfoje.png)
«it» #5 ( ), «The animal» «it».
Tensor2Tensor, , .
, , , .
– ( – ): (Query vector), (Key vector) (Value vector). , .
, , . 64, / 512. , (multi-head attention) .
![transformer_self_attention_vectors](https://habrastorage.org/webt/d1/o4/fd/d1o4fdyczdrehbvczb2m2d3whcs.png)
x1 WQ q1, «», . «», «» «» .
«», «» «»?
, . , , , .
– (score). , – «Thinking». . , .
. , #1, q1 k1, — q1 k2.
![transformer_self_attention_score](https://habrastorage.org/webt/c_/wx/af/c_wxaft2ynanknfd_osg0842_us.png)
– 8 ( , – 64; , ), (softmax). , 1.
![self-attention_softmax](https://habrastorage.org/webt/em/kp/4s/emkp4swx5jqhbqljq7tpm4ne-t0.png)
- (softmax score) , . , -, , .
– - ( ). : , , ( , , 0.001).
– . ( ).
![self-attention-output](https://habrastorage.org/webt/tp/wo/s0/tpwos0dakwx0ebolkn3hzem3wsi.png)
. , . , , . , , .
– , . X , (WQ, WK, WV).
![self-attention-matrix-calculation](https://habrastorage.org/webt/i_/ei/5g/i_ei5g0b1yt95qxkqve1jxdumrq.png)
. (512, 4 ) q/k/v (64, 3 ).
, , 2-6 .
![self-attention-matrix-calculation-2](https://habrastorage.org/webt/ms/wn/cx/mswncxi-t4jjxh9mwybx9e7qmug.png)
.
, (multi-head attention). :
- . , , z1 , . «The animal didn’t cross the street because it was too tired», , «it».
- « » (representation subspaces). , , // ( 8 «» , 8 /). . ( /) .
![transformer_attention_heads_qkv](https://habrastorage.org/webt/c-/jh/8n/c-jh8nfwmfjoiciycph9wsywqf0.png)
, WQ/WK/WV «», Q/K/V . , WQ/WK/WV Q/K/V .
, , 8 , 8 Z .
![transformer_attention_heads_z](https://habrastorage.org/webt/uc/0b/hi/uc0bhij9fzgajpjhem0sia2mna8.png)
. , 8 – ( ), Z .
? WO.
![transformer_attention_heads_weight_matrix_o](https://habrastorage.org/webt/s1/d7/md/s1d7md5obt_ps-nzg-adgfhr2ks.png)
, , . , . , .
![transformer_multi-headed_self-attention-recap](https://habrastorage.org/webt/he/j3/f1/hej3f1fkxked2nrlj_xnz9k3vmy.png)
, «» , , , «» «it» :
![transformer_self-attention_visualization_2](https://habrastorage.org/webt/t9/ax/jv/t9axjvckxd8xqxtpcygunzpf8ty.png)
«it», «» «the animal», — «tired». , «it» «animal» «tired».
«» , , .
![transformer_self-attention_visualization_3](https://habrastorage.org/webt/w1/z1/po/w1z1pou6afrqqohpzswfk6kdnos.png)
— .
. , . , Q/K/V .
![transformer_positional_encoding_vectors](https://habrastorage.org/webt/ee/bv/n7/eebvn7g_nwsete4fzv4jcuguz4c.png)
, , , .
, 4, :
![transformer_positional_encoding_example](https://habrastorage.org/webt/ts/av/pt/tsavptjlxb3tm5s-nlhmiqekspk.png)
?
: , , , — .. 512 -1 1. , .
![transformer_positional_encoding_large_example](https://habrastorage.org/webt/v9/dv/oh/v9dvohtljexbjop_vrykyyqdzbk.png)
20 () 512 (). , : ( ), – ( ). .
( 3.5). get_timing_signal_1d(). , , (, , , ).
, , , , ( , ) , (layer-normalization step).
![transformer_resideual_layer_norm](https://habrastorage.org/webt/gp/ji/3r/gpji3ragynjcuq7y6hkhqp1ws5m.png)
, , :
![transformer_resideual_layer_norm_2](https://habrastorage.org/webt/h3/hm/94/h3hm94cg9y9acyf5odv4uxt1e5q.png)
. , :
![transformer_resideual_layer_norm_3](https://habrastorage.org/webt/dm/wa/pi/dmwapi3jsz1arewhc4xg3_hgevo.png)
, , , . , .
. K V. «-» , :
![transformer_decoding_1](https://habrastorage.org/webt/l6/ry/nl/l6rynlkppwpzjaenv35ipi8vcpq.gif)
. ( – ).
, , . , , . , , , .
![transformer_decoding_2](https://habrastorage.org/webt/nm/yu/oq/nmyuoqb-9wqu9bssgxkvmhv9gmc.gif)
.
. ( –inf) .
«-» , , , , .
. ? .
– , , , , (logits vector).
10 (« » ), . , 10 000 – . .
( , 1). .
![transformer_decoder_output_softmax](https://habrastorage.org/webt/hs/i1/go/hsi1go4q7rri9w6vydeuiyyqkqi.png)
, , .
, , , , .
, . .. , .
, 6 («a», «am», «i», «thanks», «student» «<eos>» (« »).
![vocabulary](https://habrastorage.org/webt/6w/qz/6w/6wqz6wpimd6gstmtqr2scouikfs.png)
.
, (, one-hot-). , «am», :
![one-hot-vocabulary-example](https://habrastorage.org/webt/an/sf/tw/ansftwzhb-z4txzgf5zuexvyql4.png)
: one-hot- .
(loss function) – , , , .
, . – «merci» «thanks».
, , , «thanks». .. , .
![transformer_logits_output_and_label](https://habrastorage.org/webt/ev/87/xq/ev87xqtv05wyamo1jcrqlrhnxho.png)
() , /. , , , .
? . , . -.
, . . , «je suis étudiant» – «I am a student». , , , :
- (6 , – 3000 10000);
- , «i»;
- , «am»;
- .. , .
![output_target_probability_distributions](https://habrastorage.org/webt/vj/ge/o0/vjgeo0a-6nwsevrrkttu79epj3g.png)
, :
![output_trained_model_probability_distributions](https://habrastorage.org/webt/nh/ry/zd/nhryzdfhby64om8iz-vp2l9jxvu.png)
, . , , (.: ). , , , – , .
, , , , . , (greedy decoding). – , , 2 ( , «I» «a») , , : , «I», , , «a». , , . #2 #3 .. « » (beam search). (beam_size) (.. #1 #2), - (top_beams) ( ). .
, . , :
:
Autores