INNER CODE UNIT · Python

n_to_sample

Tisha-runwal/Personalized-Federated-Learning-for-Privacy--Preserving-and-Scalable-IoT-Driven-Smart-Healthcare · pfl_hcare/fl/server.py:378

    n_to_sample = max(0, n_select - len(forced))

    if n_to_sample > 0 and candidates:
        # Eq.9: p_i = ||grad_i|| / sum(||grad_j||)
        norms = np.array([gradient_norms.get(cid, 1.0) for cid in candidates])
        norms = np.clip(norms, 1e-8, None)  # avoid zero
        probs = norms / norms.sum()
        n_sample = min(n_to_sample, len(candidates))
        sampled_idx = rng.choice(len(candidates), size=n_sample, replace=False, p=probs)
        selected = forced + [candidates[i] for i in sampled_idx]
    else:
        selected = forced[:n_select] if len(forced) > n_select else forced

    # Ensure at least 2 clients
    if len(selected) < 2:
        remaining = [c for c in range(num_clients) if c not in selected]
        while len(selected) < min(2, num_clients) and remaining:
            selected.append(remaining.pop(0))

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…