liveBook · Manning

Bag-of-words model

As explained in Deep Learning with Python, Third Edition

A text model that ignores token order and represents a document by which words occur in it, often with per-word weights.

A bag-of-words model represents text as an unordered collection of tokens. It discards word order and learns a weight for each vocabulary term, making it a simple alternative to sequence models. section 14.3 section 14.4

From review to vector

For “this movie made me cry,” tokenization produces token IDs; removing order yields a set such as {0, 1, 3, 5}; multi-hot encoding then marks each vocabulary word as present or absent. The resulting fixed-size vector can train a classifier directly. section 14.4

A practical implementation follows this path:

  • Use Keras TextVectorization to tokenize text, learn a vocabulary with adapt(), and emit multi-hot vectors. section 14.4.1
  • Feed the vectors to a linear Dense classifier with one learned parameter per vocabulary term plus a bias. section 14.4.1
  • Use early stopping when validation loss stops improving; the compact model trains quickly but cannot capture word-order effects. section 14.4.1 section 14.3
"this movie made me cry"

{"[UNK]": 0, "movie": 1, "film": 2, "made": 3, "laugh": 4, "cry": 5}

Use the vocabulary listing to trace how a short review becomes token IDs before order is discarded and presence is encoded. section 14.4

How Deep Learning with Python, Third Edition covers it

How deep it goes
Defines token sets, multi-hot vectors, learned weights, and a linear classifier, but stops short of modeling word order.
Examples
Uses a short movie sentence and IMDb sentiment reviews, including a 20,000-term vector and an approximately 88% test accuracy.
What it assumes
Assumes basic tokenization and supervised text classification, while demonstrating raw-text preprocessing from tokenization through evaluation.
What you can do after
Build it from scratch with TextVectorization and a linear classifier for lightweight sentiment prediction.
How it connects
Contrasts with sequence models and connects tokenization, multi-hot encoding, linear classification, and text classification.

Also explained in

Compare how 5 books explain bag-of-words 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.