All problems
Attention Weight Sparsity Fraction
Problem
Measure attention sparsity as the fraction of weights strictly below a threshold.
The signature is attention_sparsity_fraction(weights, threshold). Count entries with p < threshold across all rows and divide by the total number of entries.
Hint
The comparison is strict, so a weight exactly equal to the threshold is not counted.
Test cases
case 1
in: [[[0.5,0.5]],0.1]
expected: 0
case 2
in: [[[0.05,0.95],[0,1]],0.1]
expected: 0.5
case 3
in: [[[1]],1]
expected: 0
case 4
in: [[[0,0],[0,0]],0.5]
expected: 1
Starter codePython
def attention_sparsity_fraction(weights, threshold):
# Your code here
pass4 test cases · run your code against them in the editor