Skip to content

Question Classifier: a small model recognizes health questions

A program that recognizes whether a question is about a health topic. Instead of training a large language model, I use a ready-made small one and train only a tiny decision layer on top. Accuracy around 98%.

2022 completed

Built as part of a team portfolio project during my data-science training at Data Science Retreat in Berlin (January–April 2022); the classifier itself — concept, labeling, implementation — is my own work. Code on GitHub: sina-769/question_classifier.

What the classifier does

A question comes in, and the output is a yes/no decision: health topic or not. “What is the treatment for high-grade astrocytomas?” (a type of brain tumour) would be a yes, “When was the Eiffel Tower built?” a no. It is meant, for example, for a question-answering system that should handle health questions separately.

The method: use a ready-made model, train only the decision

The idea comes from chapter 2 of the O’Reilly book Natural Language Processing with Transformers. It has two parts, and only one of them learns.

  1. The reader: A ready-trained small language model (DistilBERT) reads each question and sums it up in 768 numbers. I do not change this model; it is “frozen”.
  2. The scale: On top sits a simple calculation rule (logistic regression). It weighs the 768 numbers and decides: health yes or no. Only these weights are learned, from my example questions.

The whole thing runs on an ordinary computer without special hardware in a few minutes. On questions the model never saw during training, it is right in about 98% of cases.

Parameters are a model’s adjustment screws. Only 769 of them are trained here: the 768 weights of the scale plus a baseline value. The language model itself has 66 million, and they stay as they are. Anyone who retrains a large model from scratch for a yes/no decision wastes compute and energy. Small is enough if the prior knowledge is right.

The principle behind it: meaning as position in space

What actually happens when a question enters the model? DistilBERT does not compute with words but with numbers. For each question, the model produces 768 numbers — and these numbers are, at heart, a mathematical representation of meaning (semantics): questions with a similar meaning land on neighboring points in this 768-dimensional space, questions with a different meaning somewhere else. Questions about headaches land close to questions about migraine and far from questions about football. The model did not invent this “map of meaning” — it is baked into the weights from training on huge amounts of text. The classifier only uses the map; it never changes it.

The figure shows a slice of it: with UMAP (a nonlinear method that squeezes high-dimensional data onto two dimensions for display — as far as I know, 768 dimensions cannot be visualized), the training questions are projected onto a plane. Left: the point density of non-health questions (blue), right: health questions (red).

You can see: the two groups sit in different parts of the space — not perfectly separated, they overlap at the edges, but their density centers lie clearly elsewhere. That is exactly why a simple separation suffices. You can picture it as a border on the map: health on one side, everything else on the other. This border is exactly what the 769 adjustment screws define. The roughly 2% errors of the classifier come mostly from this grey zone of overlap.

UMAP projection of the frozen DistilBERT hidden states on the training split: left, the point density of non-health questions (blue); right, health questions (red). The density centers of the two groups lie in separate regions of the 2D embedding, overlapping at the edges.

What is in it, and what is not

The dataset: 2,226 questions, every one attributed to a source — 1,355 from SQuAD (Stanford Question Answering Dataset, questions about Wikipedia articles; mostly the non-health questions, plus a few health ones) and 871 from AskDocs (titles from the Reddit forum r/AskDocs, where people ask medical questions). The classification (health yes or no) is my own. For licensing reasons the question texts are not in the repo; a script pulls them from the original sources.

Why this project matters to me

It was the first time I rebuilt a method from a textbook and applied it to a question of my own. And I learned along the way that a simple decision does not need a large model.