#137Edge models — magnitude pruningEasyComputer VisionNeural NetworksML System Design
Edge models — magnitude pruning
Background
To run on phones and wearables, vision models are often pruned: small-magnitude weights contribute little, so zeroing them shrinks the model with minimal accuracy loss. The simplest rule is magnitude pruning — drop any weight whose absolute value is below a threshold.
Problem statement
Implement magnitude_prune(W, threshold) returning a copy of W with every entry whose absolute value is < threshold set to 0.
Input
W— array-like weight tensor.threshold—float.
Output
Returns an np.ndarray the same shape as W: weights with kept, the rest zeroed.
Examples
Example 1
Input: W = [[0.5, -0.05], [0.2, -0.8]], threshold = 0.1
Output: [[ 0.5 0. ], [ 0.2 -0.8]]
Explanation: is pruned to 0; the rest survive.
Example 2 — threshold 0 keeps everything
Input: W = [1, -2, 3], threshold = 0
Output: [1. -2. 3.]
Constraints
- Keep entries with ; zero the rest.
- Return an
np.ndarrayof the same shape.
Notes
- Pruning is one of the standard edge-optimization levers, alongside quantization (lower precision) and distillation (small student mimics a big teacher).
Python
Loading...
▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.
- •Example prunes the small weight
- •Reference threshold 0 keeps everything
- •Sample boundary |w|==threshold is kept