Max-Norm Weight Projection

Quick Overview

Implement a max-norm constraint that caps the L2 norm of each unit's incoming weight vector along a chosen axis of a weight matrix. Tests vectorized NumPy with broadcasting, projection onto an L2 ball, input validation and safe handling of zero vectors.

Max-Norm Weight Projection

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `max_norm_project(W, c, axis=0)`. Each unit's incoming weight vector is the slice of `W` along `axis` (`axis=0`: columns are units, `axis=1`: rows are units). Rescale any vector whose L2 norm exceeds `c` to norm exactly `c` (direction kept), and return a new float array. Example: ```python W = [[3.0, 0.6], [4.0, 0.8]] # column norms: 5.0, 1.0 max_norm_project(W, 2.0) # -> [[1.2, 0.6], # [1.6, 0.8]] ``` ```hint One factor per unit Compute one scale factor per unit and let broadcasting apply it to `W`. Decide what that factor should be for a unit already inside the ball, and make sure a zero vector never reaches a division. ``` ### Constraints & Assumptions - Vectors with norm `<= c` (including the zero vector) are returned exactly unchanged. - `c <= 0` or a non-finite `c` raises `ValueError`. - The input `W` must not be mutated, and the output contains no NaN. - `W` is a 2-D array-like of finite floats. ### What a Strong Answer Covers - Per-unit norms taken along the correct axis, in a shape that broadcasts back over `W`. - A rescaling that changes only vectors outside the ball and keeps their direction. - Validation of `c` and zero vectors handled without NaN or division warnings. - A new float array returned with the input left untouched. - Why rescaling along the ray is the Euclidean projection onto the ball. ### Follow-up Questions - How would you apply this inside a PyTorch training loop? - Why is max-norm often paired with dropout and high learning rates? - How does max-norm differ from L2 weight decay?

Overview: Implement a max-norm constraint that caps the L2 norm of each unit's incoming weight vector along a chosen axis of a weight matrix. Tests vectorized NumPy with broadcasting, projection onto an L2 ball, input validation and safe handling of zero vectors.

General logo
General
Nov 25, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement max_norm_project(W, c, axis=0). Each unit's incoming weight vector is the slice of W along axis (axis=0: columns are units, axis=1: rows are units). Rescale any vector whose L2 norm exceeds c to norm exactly c (direction kept), and return a new float array.

Example:

W = [[3.0, 0.6],
     [4.0, 0.8]]   # column norms: 5.0, 1.0
max_norm_project(W, 2.0)
# -> [[1.2, 0.6],
#     [1.6, 0.8]]

Constraints & Assumptions

  • Vectors with norm <= c (including the zero vector) are returned exactly unchanged.
  • c <= 0 or a non-finite c raises ValueError .
  • The input W must not be mutated, and the output contains no NaN.
  • W is a 2-D array-like of finite floats.

What a Strong Answer Covers Guidance

  • Per-unit norms taken along the correct axis, in a shape that broadcasts back over W .
  • A rescaling that changes only vectors outside the ball and keeps their direction.
  • Validation of c and zero vectors handled without NaN or division warnings.
  • A new float array returned with the input left untouched.
  • Why rescaling along the ray is the Euclidean projection onto the ball.

Follow-up Questions Guidance

  • How would you apply this inside a PyTorch training loop?
  • Why is max-norm often paired with dropout and high learning rates?
  • How does max-norm differ from L2 weight decay?