#137Edge models — magnitude pruningEasy

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.
  • thresholdfloat 0\ge 0.

Output

Returns an np.ndarray the same shape as W: weights with wthreshold|w| \ge \text{threshold} 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: 0.05<0.1|-0.05| < 0.1 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 wthreshold|w| \ge \text{threshold}; zero the rest.
  • Return an np.ndarray of 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