Apply Softmax for Predictions
Problem Statement
Convert raw model outputs (logits) into probabilities using Softmax.
Background
Neural networks output raw scores called logits. To interpret these as probabilities, you need to apply Softmax, which converts them to values in [0, 1] that sum to 1 across classes.
Your Task
Write a function apply_softmax(logits) that takes a batch of logits and returns statistics about the resulting probability distribution.
Output Format
Return a dictionary with keys: "probabilities_sum" (list of floats, 1 decimal — should each be ~1.0), "predictions" (list of ints — predicted class per sample), "max_probs" (list of floats, 2 decimals — confidence per sample).
Example
logits = [[2.0, 1.0, 0.1], [0.5, 2.5, 0.3]]
{'probabilities_sum': [1.0, 1.0], 'predictions': [0, 1], 'max_probs': [0.66, 0.8]}logitsis a batch: each row is one sample and its 3 numbers are raw, unbounded class scores, not yet probabilities.- Each row is normalised on its own with . Row 1: , , , total , so .
- Row 2: , , , total , so .
probabilities_sum: every row adds to exactly 1 by construction, so both give1.0at 1 decimal place.predictions: the class (counting from 0) holding the largest probability in each row, giving[0, 1].max_probs: those winning probabilities at 2 decimal places, and , which prints as0.8.- Notice the winner is always the class with the largest logit, because preserves order. Only the confidence depends on the gaps: row 2's lead of yields , row 1's lead of only .
Constraints
- Use nn.Softmax(dim=1)
- Round probabilities appropriately
- Handle batch dimension correctly