Model Design
Encoder Challenge
Given the input text W={w1,…,wn} where wi is the i'th token in W, a contextualized encoder (e.g., BERT) takes W and generates an embedding ei∈R1×d for every token wi∈Wusing wi as well as its context. The challenge is that this encoder can take only up the m-number of tokens such that it cannot handle any input where n>m.
What are the ways to handle arbitrarily large input using a contextualized encoder?
Baseline
One popular method is called the "Sliding Window", which splits the input into multiple blocks of text, generates embeddings for each block separately, and merges them at the end.
Let where Wh={w(h−1)m+1,…,whm} if hm<n; otherwise, Wh={w(h−1)m+1,…,wn} such that km≤n. Then, the encoder takes each Wh and generates Eh={e(h−1)m+1,…,ehm} for every token in Wh. Finally, the embedding matrix E∈Rn×d is created by sequentially stacking all embeddings in W∀h.
What are the potential issues with this baseline method?
The baseline method does not have enough context to generate high-quality embeddings for tokens on the edge of each block.
Advanced (Exercise)
Modify the baseline method such that a block has overlapped tokens with its surrounding blocks (both front and back). Once all blocks are encoded, each overlapped token should have two embeddings. Create an average embedding of those two embeddings and make it the final embedding for the overlapped token.
Decoder Challenge
In a sequence-to-sequence model (aka, an encoder-decoder model), a decoder takes an embedding matrix E∈Rm×d and predicts what token should come next. It is often the case that this embedding matrix is also bounded by a certain size, which becomes an issue when the size of the matrix becomes larger than m (for the case above, E∈Rn×d where n>m). One common method to handle this issue is to use an attention matrix for dimensionality reduction as follows:
The embedding matrix E∈Rn×d is first transposed to ET∈Rd×n then multiplied by an attention matrix A∈Rn×m such that ET⋅A→D∈Rd×m. Finally, the transpose of D, that is DT∈Rm×d gets fed into the decoder.
Would the following method be equivalent to the above method?
An attention matrix A∈Rm×nis multiplied by the embedding matrix E∈Rn×d such that A⋅E→D∈Rm×d. Finally, D gets fed into the decoder.
Last updated