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

