With noisy logits [2.0, 1.0, 0.5, -1.0] and k = 2, experts 0 and 1 are in the top 2. Removing expert 0 leaves [1.0, 0.5, -1.0], whose 2nd largest is 0.5; removing expert 2 leaves [2.0, 1.0, -1.0], whose 2nd largest is 1.0. So expert 0's chance is Phi((1.8 - 0.5)/1) = Phi(1.3) = 0.9032, and expert 2's is Phi((0.6 - 1.0)/1) = Phi(-0.4) = 0.3446: in the top k but not certain, or out but close.
Equal importance does not mean equal work: what fills an expert's memory is the number of examples it receives, and a count has no gradient. The Sparsely-Gated MoE paper replaces the count with a smooth estimate, using the noise that its gate already adds. Implement the estimate and the load loss built on it.
The gate scores each expert with a clean logit ci=(xWg)i and a noise scale si=Softplus((xWnoise)i)>0, then keeps the top k of the noisy logits hi=ci+εisi. The chance that expert i makes the top k if only its own noise is drawn again, the others kept, is
P(x,i)=Φ(sici−kth_excluding(h,k,i))
where kth_excluding(h,k,i) is the k-th largest entry of h with entry i removed and Φ is the standard normal CDF.
kth_excluding(v, k, i)
v a 1-D array, k >= 1, i an index. Return the k-th largest value of v with entry i removed, as a Python float.
load_estimate(clean, noisy, noise_std, k)
clean, noisy, noise_std: arrays of shape (batch, n) with c, h and s for each example; 1 <= k < n.
Return the 1-D float array Loadi=∑xP(x,i).
Use Φ(z)=21(1+erf(z/2)) (math.erf is fine).
load_loss(clean, noisy, noise_std, k, w_load)
Return wload⋅CV2(Load) as a Python float, with CV2 = population variance over squared mean plus 1e-10.
Hints
For an expert currently inside the top k, removing it makes the (k+1)-th largest value overall the threshold; for one outside, the threshold is the k-th largest overall.
Never round P to 0 or 1: its smoothness is the whole point.