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


