# Attention is all you need ## Transformer Model Architecture  Transformer主要分為Encoder和Decoder兩大塊。 Encoder主要將input序列(x1,x2...xn)產生成(z1,z2,...zn),z主要是一個向量(vector)的表示。Decoder會根據z再生成output (y1,y2,...,yn),且生成方式是每次產生一個element,這種方式叫做auto-regrressive,當前的輸出會將過去的輸出當作input。 transformer主要是encoder-encoder架構,具體說是將一些self-attention,point-wise FC layer堆疊在一起。 - Encoder: 採用N=6,每一個N代表一個block,每個block裡有兩個sub-layer: self-attention、positionwise FC feed-foward network。每個sub-layer會有一個residual connection(殘差連接),再接一個layer normalization。 每個sub-layer的output可以寫成 LayerNorm(x+Sublayer(x)) 並且統一將每個sub-layer包embeded layer,output維度都設為512。 其中LayerNorm代表意義為對每個feature進行normalization,LayerNorm代表為對每個樣本數(Batch)進行normalization,之所以LayerNorm比較常見是因為每個batch的樣本的長度(n)可能不一樣長。 下圖為LayerNorm的示意圖: batch: 樣本數  - Decoder: N = 6 與encoder相同用到了一樣的兩個sub-layers,另外再加一個sub-layer,Masked Multi-Head Attention。 由於在decoder的輸出採用的是auto-regressive,在t時刻會將t時刻之前的輸出當作input來做運算。 之所以會用Masked Multi-Head Attention是為了避免在t時刻輸出時看到了t時刻以後的輸出,因此使用了masking的注意力機制來遮蓋。 ### Attention 一個attention function將query, key-value pairs 映射成output。 query, keys, values都是vectors。 output是value的權重和,全重是key和query的相似度算出來的。 #### 本篇論文使用 Scaled Dot-Product Attention  將各個query和key做內積,再除以根號d~k~,最後經過softmax Formula:  兩種常見attention function: - Additive Attention (可處理query和key不等長的情況) - Dot-product attention(本論文使用的方法,只差在本篇論文多使用除以根號dk) 本論文的dot-product attention之所以要做scaling,是因為當dk太大時,內積出來的結果值會很大,會使得值跟值之間差距擴大,這樣會使得梯度變很小 #### Multi-Head Attention  Formula:  ## Reference - Attention is all you need論文
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up