4.2. Model Design

Encoder Challenge

Given the input text W={w1,,wn}W = \{w_1, \ldots, w_n\} where wiw_i is the ii'th token in ​WW, a contextualized encoder (e.g., BERT) takes WW and generates an embedding eiR1×de_i \in \mathbb{R}^{1 \times d} for every token wiWw_i \in Wusing wiw_i as well as its context. The challenge is that this encoder can take only up the mm-number of tokens such that it cannot handle any input where n>mn > 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 W = W_1 \cup \cdots \cup W_k \where Wh={w(h1)m+1,,whm}W_h = \{w_{(h-1)m+1}, \ldots, w_{hm}\} if hm<nhm < n; otherwise, Wh={w(h1)m+1,,wn}W_h = \{w_{(h-1)m+1}, \ldots, w_{n}\} such that kmnkm \leq n. Then, the encoder takes each WhW_h​ and generates Eh={e(h1)m+1,,ehm}E_h = \{e_{(h-1)m+1}, \ldots, e_{hm}\} for every token in WhW_h. Finally, the embedding matrix ERn×dE \in \mathbb{R}^{n \times d} is created by sequentially stacking all embeddings in WhW_{\forall 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 ERm×dE \in \mathbb{R}^{m \times 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 mm (for the case above, ERn×dE \in \mathbb{R}^{n \times d} where n>mn > m). One common method to handle this issue is to use an attention matrix for dimensionality reduction as follows:

The embedding matrix ERn×dE \in \mathbb{R}^{n \times d} is first transposed to ETRd×nE^T \in \mathbb{R}^{d \times n} then multiplied by an attention matrix ARn×mA \in \mathbb{R}^{n \times m} such that ETADRd×mE^T \cdot A \rightarrow D \in \mathbb{R}^{d \times m}. Finally, the transpose of DD, that is DTRm×dD^T \in \mathbb{R}^{m \times d} gets fed into the decoder.

Would the following method be equivalent to the above method?

An attention matrix ARm×nA \in \mathbb{R}^{m \times n}is multiplied by the embedding matrix ERn×dE \in \mathbb{R}^{n \times d} such that AEDRm×dA \cdot E \rightarrow D \in \mathbb{R}^{m \times d}. Finally, DD gets fed into the decoder.

Last updated