liveBook · Manning

Sequence model

As explained in Deep Learning with Python, Third Edition

A model designed to ingest an ordered token sequence and learn dependencies across positions in that sequence.

A sequence model ingests tokens in order and learns dependencies across positions. Unlike set or bag-of-words models, it preserves sequence information; RNNs process tokens step by step, while Transformers can inspect multiple positions using positional information. section 14.3 section 14.5

Use sequence models when the arrangement of tokens carries useful information:

  • Convert raw text into token IDs before modeling; character, word, and subword tokenization are available. section 14.6
  • Pad variable-length sequences to a rectangular batch, or truncate them to a manageable maximum length. section 14.5
  • Prevent padding tokens from influencing predictions, and recognize that effective sequence models generally require substantial data. section 14.5 section 14.6

IMDb sentiment classification

For raw IMDb reviews, tokenize each review, represent tokens as integer IDs, and produce fixed-length sequences of 600 positions. Shorter reviews receive [PAD] tokens; longer reviews are truncated. A sequence model can then learn sentiment-related dependencies directly from the ordered review text instead of relying on manually engineered local-order features. section 14.5

Listing 14.19 Padding IMDb reviews to a fixed sequence length
max_length = 600
max_tokens = 30_000
text_vectorization = layers.TextVectorization(
    max_tokens=max_tokens,
    split="whitespace",                                         #1
    output_mode="int",                                          #2
    output_sequence_length=max_length,                          #3
)
text_vectorization.adapt(train_ds_no_labels)

sequence_train_ds = train_ds.map(
    lambda x, y: (text_vectorization(x), y), num_parallel_calls=8
)
sequence_val_ds = val_ds.map(
    lambda x, y: (text_vectorization(x), y), num_parallel_calls=8
)
sequence_test_ds = test_ds.map(
    lambda x, y: (text_vectorization(x), y), num_parallel_calls=8
)

Shows a TensorFlow TextVectorization pipeline that creates 600-token integer sequences for training, validation, and test datasets. section 14.5

How Deep Learning with Python, Third Edition covers it

How deep it goes
Defines sequence modeling, compares RNNs, 1D ConvNets, and Transformers, and details tokenization, padding, and truncation.
Examples
Uses IMDb reviews, integer token sequences, [PAD] tokens, and a 600-position TextVectorization pipeline.
What it assumes
Assumes basic text classification and tokenization knowledge while teaching ordered inputs, padding, truncation, and positional dependencies.
What you can do after
Build it from scratch by preprocessing text into padded token-ID sequences and selecting an RNN, 1D ConvNet, or Transformer.
How it connects
Connects tokenization, embeddings, pretraining, RNNs, 1D ConvNets, Transformers, attention, and set models.

Also explained in

Compare how 2 books explain sequence model →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.