liveBook · Manning

Softmax

As explained in Inside Deep Learning: Math, Algorithms, Models

A function that turns a vector of scores into normalized weights over items or classes by exponentiating and renormalizing them.

Softmax converts a model’s C raw class scores into C normalized probabilities. It exponentiates each score and renormalizes the results so they lie between 0 and 1 and sum to 1. The largest score remains the most likely class, while the probability gap expresses confidence. section 2.3.2

Use softmax to connect classification outputs to training and interpretation:

  • Pair the resulting probability for the true class with cross-entropy to obtain a classification loss. section 2.3.2
  • Treat the values entering softmax as logits and the outputs as probabilities. section 2.3.3
  • Apply softmax across the class dimension, such as dim=1 for batched PyTorch outputs. section 2.3.3
def visualize2DSoftmax(X, y, model, title=None): 
    x_min = np.min(X[:,0])-0.5 
    x_max = np.max(X[:,0])+0.5 
    y_min = np.min(X[:,1])-0.5
    y_max = np.max(X[:,1])+0.5 
    xv, yv = np.meshgrid(np.linspace(x_min, x_max, num=20), 
     np.linspace(y_min, y_max, num=20), indexing=’ij’) 
    xy_v = np.hstack((xv.reshape(-1,1), yv.reshape(-1,1))) 
    with torch.no_grad():
        logits = model(torch.tensor(xy_v, dtype=torch.float32)) 
        y_hat = F.softmax(logits, dim=1).numpy()

    cs = plt.contourf(xv, yv, y_hat[:,0].reshape(20,20), 
     levels=np.linspace(0,1,num=20), cmap=plt.cm.RdYlBu) 
    ax = plt.gca() 
    sns.scatterplot(x=X[:,0], y=X[:,1], hue=y, style=y, ax=ax) 
    if title is not None: 
        ax.set_title(title)

visualize2DSoftmax(X, y, model)

Visualize the probability assigned to a class across a two-dimensional input space, alongside the model’s data points. section 2.3.3

How Inside Deep Learning: Math, Algorithms, Models covers it

How deep it goes
Explains score conversion, normalization, confidence, terminology, and PyTorch application without deriving deeper statistical justifications.
Examples
Uses true-class confidence values, two-class circles-and-crosses data, PyTorch code, and a contour visualization.
What it assumes
Assumes multiclass labels and neural-network outputs while developing the distinction between scores and probabilities.
What you can do after
Apply it via PyTorch to convert model outputs into class probabilities and visualize learned confidence regions.
How it connects
Ties softmax to classification, logits, probability distributions, cross-entropy loss, and neural networks.

Also explained in

Compare how 17 books explain softmax →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Inside Deep Learning: Math, Algorithms, Models. The section links point to the parts of the book it draws on.