Neural Mastery
← Practice
Vectorized Numerically Stable Log-Sum-Expeasy

Vectorized Numerically Stable Log-Sum-Exp

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 robust log-sum-exp reductions across matrix rows.

Task

Implement vectorized_log_sum_exp(matrix): for each row, compute max_x + log(sum(exp(x_i - max_x))). If a row is empty, return 0.0. Round each float to 4 decimal places.

Function Signature

vectorized_log_sum_exp(matrix: list[list[float]]) -> list[float]

Examples

Example 1: Standard small values
Input: matrix = [[1, 2, 3], [0, 0]]
Output: [3.4076, 0.6931]
Example 2: Very large values overflow protection
Input: matrix = [[1000, 1001, 1002]]
Output: [1002.4076]

Constraints

  • matrix is a list of rows of floats
Python3Saved ✓
import math

def vectorized_log_sum_exp(matrix: list[list[float]]) -> list[float]:
"""Compute row-wise numerically stable log-sum-exp.
Round each float to 4 decimal places.
"""
pass

Case 1: Standard small values
Input: matrix = [[1, 2, 3], [0, 0]]
Expected: [3.4076, 0.6931]
Case 2: Very large values overflow protection
Input: matrix = [[1000, 1001, 1002]]
Expected: [1002.4076]