Course materials · Machine Learning Lab

Multiclass Classification · Learning Materials

English translation

Topics covered

Understanding the relationship between class scores, probabilities, and ground truth labels helps you interpret multiclass classification errors and incorrect labels.

Overall workflow

Class scores → Softmax and loss → Predicted labels → Confusion matrix

Multiclass outputs

For three classes, the model produces three scores per sample. Softmax normalizes the scores together. CrossEntropyLoss takes scores before normalization and integer labels.

Examining errors by class

A confusion matrix shows which classes are confused with each other, beyond a single accuracy value. When one label is incorrect, fit against the modified labels may differ from accuracy against the original meaning.

Worked example

Generate 6 samples per class, 18 in total, around the provided centers (-2,0),(2,0),(0,2). Use seed=7.

import numpy as np
import matplotlib.pyplot as plt
import torch
from torch import nn
rng=np.random.default_rng(7)
centers=np.array([[-2.,0.],[2.,0.],[0.,2.]])
X_np=np.concatenate([c+rng.normal(0,.25,(6,2)) for c in centers]);y_np=np.repeat(np.arange(3),6)
X=torch.tensor(X_np,dtype=torch.float32);y=torch.tensor(y_np,dtype=torch.long)
def fit_multi(labels=y):
    torch.manual_seed(7);m=nn.Linear(2,3);opt=torch.optim.SGD(m.parameters(),lr=.1);history=[]
    for _ in range(200):
        loss=nn.functional.cross_entropy(m(X),labels);opt.zero_grad();loss.backward();opt.step();history.append(loss.item())
    return m,history
model,history=fit_multi();logits=model(X).detach();prob=torch.softmax(logits,dim=1);pred=prob.argmax(dim=1)
print(prob[:3],pred[:3])
tensor([[0.9664, 0.0018, 0.0318],
        [0.9847, 0.0014, 0.0139],
        [0.9866, 0.0012, 0.0121]]) tensor([0, 0, 0])

Reference example

View more examples

Reference materials