liveBook · Manning

Sequence model

As explained in Deep Learning with R, Third Edition

A sequence model is a model that processes an ordered sequence of tokens or other values while learning dependencies among their positions.

A sequence model processes tokens in order and learns positional dependencies instead of treating the input as an unordered set. Recurrent neural networks and Transformers are sequence models because they use word-order information, although they obtain it differently. section 14.3 section 14.5

IMDb sentiment classification

A practical workflow uses raw positive and negative movie reviews. Tokenization assigns integer IDs, while padding or truncation makes reviews rectangular for batch computation. Reviews can be limited to 600 words and a vocabulary of 30,000 tokens before being passed to a sequence model. section 14.3 section 14.5

Listing 14.18 Padding IMDb reviews to a fixed sequence length
max_length <- 600
max_tokens <- 30000
text_vectorization <- layer_text_vectorization(
  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 |>
  dataset_map(\(x, y) tuple(text_vectorization(x), y),
              num_parallel_calls = 8)
sequence_val_ds <- val_ds |>
  dataset_map(\(x, y) tuple(text_vectorization(x), y),
              num_parallel_calls = 8)
sequence_test_ds <- test_ds |>
  dataset_map(\(x, y) tuple(text_vectorization(x), y),
              num_parallel_calls = 8)

Shows how Keras text vectorization converts reviews into fixed-length integer sequences, including vocabulary limits, padding, and dataset mapping. section 14.5

Sequence models can use several architectural strategies:

  • RNNs process tokens sequentially and use recurrent computation to remain order-aware. section 14.3 section 15.3.4
  • One-dimensional convolutional networks learn local patterns across the sequence. section 14.5
  • Transformers process relationships between sequence elements with attention, but require position embeddings because attention alone is order-agnostic. section 14.3 section 15.3.4
  • Compared with set models, sequence models generally require more data; pretraining on unlabeled text can reduce this data burden. section 14.6

How Deep Learning with R, Third Edition covers it

How deep it goes
Explains order handling, token preprocessing, padding, truncation, architectures, and position embeddings without deriving their mathematics.
Examples
Demonstrates IMDb reviews, integer token sequences, fixed-length padding, RNNs, and an English-to-Spanish Transformer.
What it assumes
Assumes basic tokenization concepts while teaching ordered integer sequences, padding, truncation, and positional information.
What you can do after
Build it from scratch by vectorizing text, preparing padded batches, and selecting an RNN, 1D convnet, or position-aware Transformer.
How it connects
Connects sequence models to tokenization, embeddings, recurrent neural networks, convolutional networks, Transformers, attention, and pretraining.

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 R, Third Edition. The section links point to the parts of the book it draws on.