A Keras TextVectorization layer turns text into numerical inputs for a model. It can split text into words or characters, learn a vocabulary with adapt(), and encode inputs as multi-hot vectors or integer sequences. section 14.4.1 section 15.1.1
Bag-of-words classification
For IMDb sentiment classification, configure a 20,000-token vocabulary with whitespace splitting and output_mode="multi_hot". Adapt the layer on training text, then map it over the training, validation, and test datasets. Each review becomes a 20,000-element vector marking which vocabulary terms are present; a linear classifier can consume it directly. section 14.4.1
Listing 14.12 Applying a bag-of-words encoding to the IMDb reviews
from keras import layers max_tokens = 20_000 text_vectorization = layers.TextVectorization( max_tokens=max_tokens, split="whitespace", #1 output_mode="multi_hot", ) train_ds_no_labels = train_ds.map(lambda x, y: x) text_vectorization.adapt(train_ds_no_labels) bag_of_words_train_ds = train_ds.map( lambda x, y: (text_vectorization(x), y), num_parallel_calls=8 ) bag_of_words_val_ds = val_ds.map( lambda x, y: (text_vectorization(x), y), num_parallel_calls=8 ) bag_of_words_test_ds = test_ds.map( lambda x, y: (text_vectorization(x), y), num_parallel_calls=8 )
See the layer configuration, vocabulary adaptation, and dataset mappings. section 14.4.1
For a bigram model, set ngrams=2 and increase max_tokens to 30,000. Unigrams and bigrams compete for vocabulary space, and the same linear classifier design can use the resulting multi-hot vectors. Longer n-grams quickly increase the vocabulary needed for useful coverage. section 14.4.2
Listing 14.16 Applying a bigram encoding to the IMDb reviews
max_tokens = 30_000 text_vectorization = layers.TextVectorization( max_tokens=max_tokens, split="whitespace", #1 output_mode="multi_hot", ngrams=2, #2 ) text_vectorization.adapt(train_ds_no_labels) bigram_train_ds = train_ds.map( lambda x, y: (text_vectorization(x), y), num_parallel_calls=8 ) bigram_val_ds = val_ds.map( lambda x, y: (text_vectorization(x), y), num_parallel_calls=8 ) bigram_test_ds = test_ds.map( lambda x, y: (text_vectorization(x), y), num_parallel_calls=8 )
Compare the bigram configuration with the bag-of-words pipeline. section 14.4.2
Character sequences for a language model
Set split="character" and standardize=None to preserve case and punctuation. With output_sequence_length=100, a 100-character input becomes a sequence of 100 integers. For next-character prediction, tokenize both the input and its one-character-offset label sequence. section 15.1.1
Listing 15.3 Learning a character-level vocabulary with the TextVectorization layer
from keras import layers tokenizer = layers.TextVectorization( standardize=None, split="character", output_sequence_length=sequence_length, ) tokenizer.adapt(dataset.map(lambda text, labels: text))
See how to configure and adapt the character-level tokenizer. section 15.1.1
Translation can use separate layers for the source and target languages. Custom standardization preserves Spanish [start] and [end] markers while removing punctuation, including ¿. The target sequence is one position longer so it can be split into decoder inputs and next-token labels; sample weights exclude zero-padded labels. For non-toy translation, treat punctuation as separate tokens rather than stripping it. section 15.2.1
Listing 15.10 Tokenizing and preparing the translation data
batch_size = 64 def format_dataset(eng, spa): eng = english_tokenizer(eng) spa = spanish_tokenizer(spa) features = {"english": eng, "spanish": spa[:, :-1]} labels = spa[:, 1:] sample_weights = labels != 0 return features, labels, sample_weights def make_dataset(pairs): eng_texts, spa_texts = zip(*pairs) eng_texts = list(eng_texts) spa_texts = list(spa_texts) dataset = tf.data.Dataset.from_tensor_slices((eng_texts, spa_texts)) dataset = dataset.batch(batch_size) dataset = dataset.map(format_dataset, num_parallel_calls=4) return dataset.shuffle(2048).cache() train_ds = make_dataset(train_pairs) val_ds = make_dataset(val_pairs)
See how tokenized translation pairs become inputs, shifted labels, and padding weights. section 15.2.1

