#145Edge models — measure sparsityEasy

Edge models — measure sparsity

Background

After pruning you want to know how sparse the model became: the fraction of weights that are exactly zero. Higher sparsity means a smaller, faster model (with the right kernels).

sparsity=#{w:w=0}#weights\text{sparsity} = \frac{\#\{w : w = 0\}}{\#\text{weights}}

Problem statement

Implement sparsity(W) returning the fraction of entries that are exactly 0.

Input

  • W — array-like weight tensor.

Output

Returns a Python float in [0,1][0, 1].

Examples

Example 1

Input:  W = [[0, 1], [2, 0]]
Output: 0.5

Explanation: 2 of the 4 entries are zero.

Example 2 — fully pruned

Input:  W = [0, 0, 0]
Output: 1.0

Constraints

  • Count exact zeros divided by the total number of entries.
  • Return a plain float.

Notes

  • Reported alongside accuracy, sparsity tells you the size/speed payoff of pruning — e.g. "90% sparse with <1% accuracy drop".
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.

  • Half zeros example
  • Fully pruned reference -> 1.0
  • Sample with no zeros -> 0.0