Concepts / LSTM Networks and Sequence Memory

LSTM Networks and Sequence Memory

Each sequence has a fixed length, often described as maxlen in the source.

  • Programming

One Character at a Time

Character-level generation asks a narrow question repeatedly: given the characters available so far, which character should come next? The model does not produce an entire word or sentence in one operation. It predicts one character, appends that character to the text, and uses the enlarged context for the next prediction.

Building Training Windows

Training begins by dividing the text into overlapping character sequences. Each sequence has a fixed length, often described as maxlen in the source. The character immediately following a sequence becomes that sequence's target. In other words, the input supplies the context and the target identifies what the model should learn to predict next.

Worked example

Aligning context and target

Suppose a toy text is divided into fixed-length windows of four characters. Show how overlapping windows are paired with the character that follows each window.

  1. 1
    First window

    The first four-character context is paired with the fifth character as its next-character target.

  2. 2
    Advance one position

    The next context begins one character later, so it overlaps most of the first context. Its target is the character immediately after this new window.

  3. 3
    Repeat

    Moving the window through the text creates many context-target pairs. Every pair preserves the same contract: the input is the context, and the target is the following character.

Result

Overlapping windows turn one text string into many supervised learning examples, each teaching the relationship between a fixed-length context and the character after it.

Context 1characters 1 through maxlenTarget 1following characterContext 2shifted one characterTarget 2following characterContext 3shifted one characterTarget 3following characterpredictspredictspredicts
How does a text string become multiple fixed-length input windows, with each window aligned to the character that follows it?

The source describes the input array x as three-dimensional. Its dimensions correspond to the number of sequences, the sequence length maxlen, and the number of unique characters. The target array y contains the one-hot-encoded character that follows each input sequence. This alignment is the central data contract for the training data.

From Context to Probabilities

The generation model combines an LSTM layer with a Dense softmax classifier. The LSTM processes the character sequence and uses sequence memory to help represent information from earlier characters while considering the current sequence. The Dense softmax classifier converts the model's result into a probability distribution over possible next characters.

Character sequencefixed-length contextLSTMsequence memoryDense softmaxvocabulary-sized outputNext-characterprobabilitiesone probability percharacterprocessespasses representationproduces
How does a fixed-length character sequence move through the LSTM and Dense softmax classifier to produce next-character probabilities?

The Dense softmax output must represent the full character vocabulary. If the vocabulary contains the unique characters used by the training text, the classifier needs one output position for each of those possible characters. The target's one-hot encoding and the classifier's output positions must therefore refer to the same character set.

Earlier charactersinformation from thesequenceLSTM memorysequence informationcarried forwardCurrent characterpart of the contextNext-characterpredictionprobability distributioncontributes informationhelps informis considered with memory
How does the LSTM use earlier characters while processing the current sequence to help predict what comes next?

The Autoregressive Loop

After training, generation starts with a seed snippet. The routine sends the current fixed-length context through the model, obtains a distribution for the next character, adjusts that distribution using a temperature, and samples one character. The sampled character is appended to the generated text. The oldest character is then removed from the fixed-length context so that the next prediction uses the newest available window.

Current contextfixed-length windowModel predictionnext-character distributionSampled characterone selected characterGenerated textnew character appendedAdvanced contextoldest character removedsendsampleappendupdate windowrepeat
What happens at each step when the current context is sent through the model, a character is sampled, the context advances, and the new character is appended?
Worked example

Tracing one generation step

A seed snippet is already available as the current context. Describe one complete generation step without assuming a particular predicted character.

  1. 1
    Read the context

    The current fixed-length character window is presented to the trained model.

  2. 2
    Predict

    The LSTM and Dense softmax classifier produce a probability distribution over the character vocabulary.

  3. 3
    Adjust and sample

    The distribution is adjusted using the selected temperature, and one character is sampled from it.

  4. 4
    Append

    The sampled character is added to the generated text.

  5. 5
    Advance

    The oldest character is removed from the fixed-length context, leaving a new context for the next iteration.

Result

One iteration both extends the generated passage and prepares the next context. Repeating the iteration creates the passage character by character.

Temperature and Variety

Temperature changes the probability distribution used for sampling. A lower temperature makes the sampling distribution more concentrated, so choices tend to favor the model's more likely characters. A higher temperature makes the distribution more varied, allowing less likely characters to receive more opportunity during sampling. Temperature changes how the same model predictions are sampled; it does not create a separate model.

Same modelpredictionshared startingdistributionLower temperaturemore concentrated choicesFocused outputless sampling varietyHigher temperaturemore varied choicesVaried outputmore sampling varietysample withsample withtends towardtends toward
How does changing temperature reshape the probability distribution over possible next characters and affect generated text?

The source generation loop compares temperatures of 0.2, 0.5, 1.0, and 1.2 while training progresses. These values are alternative sampling choices applied to predictions from the same model, making it possible to compare more concentrated and more varied generated text.

Diagnosing Generation Failures

A poor-looking passage does not identify its own cause. Debug the system in stages: data preparation, model construction, and sampling. Checking these stages separately prevents a sampling adjustment from hiding a data or model problem.

Generation problemunexpected passageData preparationcontext-target alignmentAligned pairscontinue checkingModel constructionfull vocabulary outputVocabulary-sizedoutputcontinue checkingSamplingprobability-to-characterconversionValid sampledcharactercontinue checkingcheck firstif alignedthen checkif output matches vocabularythen checkif conversion is correct
How can symptoms of failed generation be traced to data preparation, model construction, or sampling?
  • Common mistake

    Pairing a context with the wrong target character.

    What it looks like

    The input sequence is taken from one position, but the target is not the character immediately following that sequence.

    Why it fails

    The training example no longer teaches the intended next-character relationship.

    Do this instead

    Check that every input sequence is aligned with the character directly after it.

  • Common mistake

    Using an output layer that does not cover the complete character vocabulary.

    What it looks like

    The classifier has fewer output positions than the number of unique characters used by the training text.

    Why it fails

    The model cannot represent a probability for every possible target character.

    Do this instead

    Check that the Dense softmax output size matches the character vocabulary.

  • Common mistake

    Debugging only the temperature.

    What it looks like

    Changing temperature repeatedly when the sampled index is converted back to the wrong character.

    Why it fails

    Temperature changes sampling, but it does not correct a broken probability-to-character conversion.

    Do this instead

    Verify the conversion from predicted probabilities to a sampled index and then back to a character.

Practice the Trace

MEDIUM

Describe the next generation step after a model has received its current fixed-length context. Include the model prediction, temperature adjustment, character sampling, text append, and context update.

Hints
  • The model first produces a distribution over the character vocabulary.
  • The sampled character must be added to the generated text.
  • The next context removes the oldest character so the window remains fixed-length.
MEDIUM

A generation result looks wrong. Explain which stage you would inspect first if the target is not the character immediately following each input sequence, and which stage you would inspect if the sampled probability index is converted back to the wrong character.

Hints
  • The first issue concerns the relationship between input windows and targets.
  • The second issue occurs after the model has produced probabilities.

Key Takeaways

  1. 1
    Character-level generation learns a mapping from a fixed-length character context to the character that follows it.
  2. 2
    Overlapping input windows and aligned one-hot next-character targets form the central training data contract.
  3. 3
    The LSTM processes sequence information, while the Dense softmax classifier produces probabilities across the character vocabulary.
  4. 4
    Generation is autoregressive: each sampled character is appended and used as part of the next context.
  5. 5
    Temperature changes sampling variety, while data alignment, vocabulary coverage, and probability-to-character conversion are separate debugging concerns.

At a glance

  • Character-level generation predicts one next character from a fixed-length context.
  • Training uses overlapping contexts paired with the character immediately following each context.
  • An LSTM supplies sequence processing and memory, and a Dense softmax classifier supplies vocabulary-wide next-character probabilities.
  • The generation loop samples, appends, advances the context, and repeats.
  • Temperature controls how concentrated or varied sampling is, but it cannot repair errors in data preparation or sampling logic.

Related concepts

More concepts starting with L