|
@@ -76,10 +76,16 @@ def apply_noise(weights, batch_size, max_norm, noise_multiplier, noise_type, dev
|
|
|
if max_norm == None:
|
|
|
max_norm = 1.0
|
|
|
|
|
|
+ clipped = 0
|
|
|
+ total = 0
|
|
|
+
|
|
|
for p in weights:
|
|
|
+ total += 1
|
|
|
if clipping:
|
|
|
norm = torch.norm(p, p=2)
|
|
|
div_norm = max(1, norm/max_norm)
|
|
|
+ if div_norm != 1:
|
|
|
+ clipped += 1
|
|
|
p /= div_norm
|
|
|
|
|
|
noise = generate_noise(max_norm, p, noise_multiplier, noise_type, device)
|
|
@@ -87,6 +93,8 @@ def apply_noise(weights, batch_size, max_norm, noise_multiplier, noise_type, dev
|
|
|
noise /= batch_size
|
|
|
p += noise
|
|
|
|
|
|
+ return clipped, total
|
|
|
+
|
|
|
# Client side Noise
|
|
|
class PrivacyEngineXL(opacus.PrivacyEngine):
|
|
|
"""
|