Hidden States in Neural Networks
The hidden state is the internal memory the network keeps while it works.
Why Translation Needs Two Cooperating Parts
Many language translation systems are built from two cooperating pieces rather than a single component. Before transformer models became dominant, a particularly popular way to build such a system was an encoder-decoder RNN. In this design, the work of translation is split between two halves: one half reads the input, and the other half writes the output.
This article follows one sentence through that design. The goal is to see where each piece of information goes next, from the words you feed in to the words that come out, and to understand why the hidden state is the thread that connects the two halves.
What do you think happens?
In a translation system with two halves, which half should be allowed to look at the original sentence?
Reveal answer
Only the encoder reads the original sentence
The decoder never reads the original sentence directly. It depends entirely on what the encoder has already processed, and the encoder never writes the translation. The handoff between the two is the central event in the system.
Two Submodules and Their Jobs
An encoder-decoder architecture divides translation between an encoder that reads the whole input and a decoder that writes the output.
The encoder is the submodule that reads. It receives the input text and processes it sequentially, one token at a time. Its job is to build up a representation of what it has read so far. The encoder never writes the translation.
The decoder is the submodule that writes. It begins from the encoder's result and generates the translated sentence one word at a time. The decoder never reads the original sentence directly. Everything it knows about the input has to arrive through the encoder.
The division of labor is the key idea. Neither submodule does the whole task alone. Reading and writing are separated, and the connection between them carries all the information about the input. Because of this, the handoff between the encoder and the decoder is the single most important event in the system.
How the Encoder Builds Its Hidden State
The hidden state is the internal memory the network keeps while it works. It refers to the internal values held in the hidden layers of the network. In the encoder, its purpose is to build up a representation of what has been read so far.
The encoder's hidden state is revised each time a new token arrives. Read in order, the state after the first token reflects only that token. When the next token arrives, the state is updated to reflect both tokens. The aim is for the state left after the last token to capture the meaning of the entire input sentence.
Notice what the diagram is saying. Each state is not a separate copy of a word. It is a revised version of the same memory, updated by the newest token. After the last token, that memory is the only result of reading the input that is passed onward.
Handoff to the Decoder
The encoder's final hidden state is the only thing the decoder receives from the encoder. This makes it a compact summary of everything the encoder read. The decoder begins by taking this final state as its starting point.
From that starting point, the decoder generates the translated sentence one word at a time. At each generation step, it also updates its own hidden state. The decoder's hidden state is meant to carry the context needed to predict the next word, so each new output is chosen with awareness of what has already been produced.
Tracing the German-to-English Example
Translating du, Kannst, mir into you, Can, help
Trace how the German input tokens du, Kannst, and mir become the English output tokens you, Can, and help, one step at a time.
- 1Read the first input token
The encoder receives du and updates its hidden state. The state now reflects only this first token.
- 2Read the second input token
The encoder receives Kannst and revises its hidden state so that it reflects du and Kannst together.
- 3Read the last input token
The encoder receives mir and revises its hidden state once more. The resulting state is meant to capture the meaning of the whole input sentence.
- 4Hand off the final state
The encoder's final hidden state passes to the decoder. This is the only information the decoder gets from the input.
- 5Generate the first output word
The decoder starts from the handed-off state and produces you. It updates its own hidden state.
- 6Generate the next output words
The decoder produces Can, then help, each time choosing the word with awareness of what it has already produced.
The English sentence you, Can, help is produced one word at a time by the decoder, starting from the encoder's final hidden state.
The arrows between the output tokens matter. They show that the decoder produces you, Can, and help in sequence, not all at once. Each output word is a separate step, and the decoder's hidden state is updated at every one of those steps.
Mistakes Beginners Make with the Two Halves
Common mistake Thinking the decoder reads the original sentence directly
What it looks likeAssuming that when the decoder writes Can, it looks back at the German word Kannst.
Why it failsThe decoder never reads the original sentence directly. It depends entirely on what the encoder has already processed.
Do this insteadRemember that the only information the decoder receives from the input is the encoder's final hidden state.
Common mistake Thinking the encoder writes the translation
What it looks likeBelieving the encoder outputs English words as it reads German words.
Why it failsThe encoder never writes the translation. Its job is to build a representation of what it has read.
Do this insteadAssign reading to the encoder and writing to the decoder.
Common mistake Treating the final hidden state as just the last word
What it looks likeAssuming the handoff contains only the token mir.
Why it failsThe hidden state is revised at every step, and the state after the last token is meant to capture the entire input sentence.
Do this insteadThink of the final state as a summary built from all the tokens, not a copy of the last one.
Common mistake Imagining all output words are produced at once
What it looks likePicturing you, Can, and help appearing in a single step.
Why it failsThe decoder produces output one word at a time, and it updates its hidden state at each step.
Do this insteadTrace the output as a sequence of separate decoder steps.
Checking Your Understanding
Without looking back, describe in your own words what the encoder's hidden state contains after the last input token, and explain why the decoder needs it before it can write the first English word.
Hints
- Think about what the encoder is trying to capture as it reads each token.
- Think about which piece of information the decoder receives from the encoder and nothing else.
A friend says the decoder translates by looking at the German input again at every output step. Explain why this is incorrect, using the terms encoder, decoder, and hidden state.
Hints
- Which submodule is the only one that reads the original input?
- What is the decoder's own hidden state responsible for at each step?
Key Takeaways
- An encoder-decoder architecture splits translation between an encoder that reads the whole input and a decoder that writes the output.
- The encoder processes input one token at a time, revising its hidden state with each token, with the aim that the final state captures the meaning of the entire input.
- The encoder's final hidden state is the only information the decoder receives from the encoder, and the decoder uses it as a starting point.
- The decoder generates output one word at a time and updates its own hidden state at each step, so each new word is chosen with awareness of what it has already produced.
- The handoff between encoder and decoder is the central event of the system, since neither submodule can do the whole task alone.