Contents Projects Code Course home

Chapter 32Project 5: A Genre Classifier

What you will build

A four-way genre classifier, and an honest account of its mistakes. A model you cannot explain is worth little here. Beating a strong baseline is a goal, and proving with an ablation and an error analysis that you understand why it works is the point.

Allow about eight hours. Read Chapters 4 and 15 first.

What you need

Labelled passages from four genres. Take them from the Brown corpus: news reportage, fiction, government prose and learned writing. Cut each into passages of about three hundred words and keep about a hundred labelled passages per genre for training, setting a fifth of them aside as a validation split for the ablation. Hold out another set as a test set whose labels you do not look at until the end. Any library is allowed, and scikit-learn is the obvious choice.

The text is tokenised in the Brown style, with punctuation spaced away from words and quotation marks written as doubled backticks and apostrophes. A whitespace split therefore gives punctuation marks as tokens of their own. Whether you keep, strip or ignore them is one of the feature decisions you will ablate.

Stages

  1. A baseline to beat. Before you build anything, work out what a classifier that always predicts the majority class scores. On four balanced genres it is right a quarter of the time. Its macro-averaged F1F_1 is far lower than its accuracy suggests, because three of the four classes score zero.

    Check. The class it predicts has precision 0.250.25 and recall 11, so its F1F_1 is 0.40.4. The other three classes score 00, and the macro average is 0.10.1. Chapter 15 explains why macro averaging is the right score when classes must count equally.

  2. The leaderboard. Build the best classifier you can on the training split. A tf-idf representation with a linear model is a strong baseline if you tune the features with care. Try word nn-grams, a minimum document frequency and sublinear term frequency. Score your choices on the validation split by macro-averaged F1F_1, and predict the test set once, at the end, with the configuration you settled on.

  3. Ablation. Report at least three configurations you actually ran, each with its validation macro-F1F_1, for example raw counts, then tf-idf, then tf-idf with bigrams and a minimum document frequency. Add one decision about punctuation. The configuration you submit for the test set must be the best of these, and its test score must be reported next to its validation score. An ablation that claims a score your model does not reach is worthless.

  4. Error analysis. Build the confusion matrix on validation data. Name the genre with the lowest per-class F1F_1 and the pair of genres most often mistaken for each other. Read both off the matrix and not from a hunch. Your validation split is small, so say how much you trust your answer.

Extensions

  • Replace the tf-idf features with the averaged embeddings of Chapter 9 and compare the macro-F1F_1 and the confusion matrix.

  • Train the recurrent classifier of Chapter 15 on the same data and measure it like any other classifier.

  • Calibrate your scores, as in Chapter 15, and plot the reliability of the most confident predictions.

From Natural Language Processing: From Corpus Statistics to Grounded LLMs, by Ramaseshan Ramachandran.