Neural Mastery
← Practice
Vectorized Softmax Normalizationeasy

Vectorized Softmax Normalization

easy NumPy & Data Wrangling 10–15 min

NumPy allowed · plain Python earns 10 bonus points

Linear algebra step 1/4

A vector transforms

Matrix multiplication is the first computation behind every model.
Open lesson →
01vector
02matrix
03transform
04result
[2, −1] × [[1, 2], [−.5, 1]] = [0, −2]

Computed matrix × vector transformation

Compute numerically stable softmax probabilities across rows of a 2D matrix of logits.

Task

Implement vectorized_softmax(scores) to compute row-wise softmax probabilities. For each row, subtract the row maximum before exponentiating to prevent numerical overflow, then divide by the row exponential sum. Round each probability to 4 decimal places.

Function Signature

vectorized_softmax(scores: list[list[float]]) -> list[list[float]]

Examples

Example 1: Standard 2x3 logits
Input: scores = [[1, 2, 3], [0, 0, 0]]
Output: [[0.09, 0.2447, 0.6652], [0.3333, 0.3333, 0.3333]]
Example 2: Large magnitude logits
Input: scores = [[1000, 1001, 1002]]
Output: [[0.09, 0.2447, 0.6652]]

Constraints

  • scores is a list of rows of floats
  • Row lengths are consistent within each test case
Python3Saved ✓
import math

def vectorized_softmax(scores: list[list[float]]) -> list[list[float]]:
"""Compute row-wise numerically stable softmax probabilities.
Round each float to 4 decimal places.
Empty rows return empty lists.
"""
pass

Case 1: Standard 2x3 logits
Input: scores = [[1, 2, 3], [0, 0, 0]]
Expected: [[0.09, 0.2447, 0.6652], [0.3333, 0.3333, 0.3333]]
Case 2: Large magnitude logits
Input: scores = [[1000, 1001, 1002]]
Expected: [[0.09, 0.2447, 0.6652]]