liveBook · Manning

Multilabel classification

As explained in Deep Learning with Python, Third Edition

A classification task in which each example may belong to multiple classes at the same time, even though there are multiple possible classes overall.

Use multilabel classification when one sample can have several labels at once. An image containing both a cat and a dog needs both labels; the number of labels can vary from image to image. section 4.0

One topic or several?

A newswire assigned exactly one of 46 topics is a single-label multiclass example. If a newswire could belong to several topics, the task would instead be multilabel classification. That distinction determines how to set up the output layer and targets. section 4.2 section 20.1.6

Choose the classification setup by asking how many labels one sample may receive:

  • One of two exclusive classes: use one sigmoid output, binary_crossentropy, and a 0-or-1 target. section 20.1.6
  • Exactly one of several classes: use one output per class with softmax; choose categorical_crossentropy for one-hot targets or sparse_categorical_crossentropy for integer targets. section 20.1.6
  • Several classes per sample: use one sigmoid output per class, binary_crossentropy, and k-hot targets. section 20.1.6
inputs = keras.Input(shape=(num_input_features,))
x = layers.Dense(32, activation="relu")(inputs)
x = layers.Dense(32, activation="relu")(x)
outputs = layers.Dense(num_classes, activation="sigmoid")(x)
model = keras.Model(inputs, outputs)
model.compile(optimizer="rmsprop", loss="binary_crossentropy")

The Keras example puts the multilabel rule into code: its final Dense layer has num_classes sigmoid units, and compilation uses binary_crossentropy. section 20.1.6

Decide whether labels can coexist while framing the problem, before choosing the model configuration. A photo search engine is one example of a multiclass, multilabel task; the available images and annotations still matter. section 6.1.1

How Deep Learning with Python, Third Edition covers it

How deep it goes
Defines the task and gives a Keras output-layer and loss recipe, without a worked multilabel training run.
Examples
Uses a cat-and-dog image, a photo search task, and a hypothetical multilabel version of newswire topic classification.
What it assumes
Introduces labels and classification types while assuming input features and annotated targets are available for the model recipe.
What you can do after
Apply it via Keras by configuring one sigmoid unit per class, binary_crossentropy loss, and k-hot targets.
How it connects
Contrasts with binary and single-label multiclass classification and connects to k-hot encoding, sigmoid activation, and binary cross-entropy.

Also explained in

Compare how 3 books explain multilabel classification →

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.