Introduction to Sequence-to-Sequence (Seq2Seq)

Recommended Read:
– https://micmi.co/long-short-term-memory-lstm/
– https://micmi.co/writing-lstm-from-scratch-in-c/

In our recent blog post, we built a Many-to-One architecture sequence model: it reads an input sequence of length $T_{\text{in}}$ (e.g. 3 steps) and outputs a single prediction at the very end. In a Many-to-Many synced architecture, every input step $x_t$ produces one output step $y_t$, meaning $T_{\text{in}}$ = $T_{\text{out}}$.

But what if the input sequence and output sequence have different lengths ($T_{\text{in}} \neq T_{\text{out}}$), and the mapping isn’t 1-to-1? For example translating from English to French.

EnglishFrench
Thank you very muchMerci beaucoup
4 words2 words

Because the lengths don’t match, we cannot emit an output at every input step. We have to read the entire input first, compress its meaning into a fixed-sized memory state, and then generate the output one token at a time until the model decides it is finished.

Encoder-Decoder Architecture

In problems where inputs and outputs have varying lengths that are unaligned, we design an encoder-decoder architecture.

This might look intimidating, but it simply is two LSTM Sequences linked together.

Encoder

The encode reads the input sequence $x_1, x_2, …, x_{T_\text{in}}$. We throw away all of its intermediate outputs. All we need from the Encoder is the final memory state at the end of the sequence:

$$ (H^{\text{enc}}, C^{\text{enc}}) $$

Decoder

For our decoder, instead of initializing its memory states to zeros like how we did in the LSTMSequence::forward() method we created in our last post. We initialized the Decoder’s starting states directly with the Encoder’s final states:

$$ H_0^\text{dec} = H_{\text{enc}}, C_0^\text{dec} = C_{\text{enc}} $$

Also just to make things clear before we move forward, you might have noticed that in the Decoder’s LSTM unit, we used $t’$ (t prime), instead of just $t$. It’s just a way for us to differentiate between the encoding vs decoding’s time steps. We’ll also use $x’$ (x prime) for the decoder’s inputs vs just $x$ for the encoder’s.

Forward Pass

Now that we have a bit of an overview of what the Encoder and Decoder is, let’s learn how to do a forward pass in it.

Since we already implemented LSTM from scratch, there’s not much surprise in the forward pass in the Encoder, it’s literally what we implemented without computing for the prediction $\hat{y}$.

We will put more attention in handling the input and output of the Decoder.

Autoregressive loop

The Decoder works in an autoregressive loop. An autoregressive model is a system that predicts the next element (regressive) in a sequence, such as the next word in a sentence, by using its own outputs as inputs (auto).

In simpler terms, the output generated by our decoder at step $t – 1$ becomes the input to step $t$.

There is a critical difference between how we run this loop during inference vs training.

A. Inference

During inference, we don’t know the true answer $y$:

  • Initialize $H_0^\text{dec} = H_{\text{enc}}, C_0^\text{dec} = C_{\text{enc}}$
  • Feed $x_0^\text{dec} = \text{Embed<SOS>}$ in the Decoder.
  • Then we compute for a prediction.
$$ \hat{y}_1 = \text{softmax}(W_y \cdot H_1^\text{dec} + b_y) $$
  • We use output at this Decoder time step as the input to the next step $x_2^{\text{dec}} = \text{Embed}(\hat{y}_1)$.
  • Then we repeat this loop until the prediction is <EOS> or we hit the max length limit.

B. Training (Teacher Forcing)

During early training, the Decoder’s weights are random. If it predicts the wrong first word, then we feed it into step 2 like how we did during inference, then the training will be derailed and our model won’t properly learn.

If you look at the image above, the Decoder’s first prediction $\hat{y}_0$ is wrong. If we follow the inference steps and used this wrong $\hat{y}_0$ as the input to the next time step $x’_1$, then we are effectively derailing the model.

To fix this, we use Teacher Forcing. During training, we ignore the Decoder’s own prediction $\hat{y}{t-1}$ and use the ground-truth $y{t-1}$ instead from our dataset at every step.

  • Initialize $H_0^\text{dec} = H_{\text{enc}}, C_0^\text{dec} = C_{\text{enc}}$
  • Feed $x_0^\text{dec} = \text{Embed<SOS>}$ in the Decoder.
  • Then we compute for a prediction.
$$ \hat{y}_1 = \text{softmax}(W_y \cdot H_1^\text{dec} + b_y) $$
  • Instead of using $\text{Embed}(\hat{y_1})$ as the input for $x_2^\text{dec}$, we use the ground-truth. $x_1^\text{dec} = y_1$.
  • Then we repeat this loop until the ground-truth is <EOS>.

Backpropagation Through Time

Decoder

By now, you should have noticed that the Decoder is a Many-to-Many architecture. Others might not agree, and i’ll discuss it more later but just by looking at the forward pass, we can say that it’s Many-to-Many. So unlike how we perform backpropogation in our last post where we only computed for the final state $\partial{H_\text{final}}$ and set $\partial{H_t} = 0$ on the other LSTM units. We just have to compute for $\partial{H_t}$ for each unit as we trace back through time.

Encoder

And if you have noticed Just like in the Forward Pass, there’s nothing new in how we do backpropagation in the Encoder since it’s basically what we did in our last blog post.