Browse Source

log and return clipping counters in apply_noise

Jens Keim 3 years ago
parent
commit
91bf08fc6e
1 changed files with 8 additions and 0 deletions
  1. 8 0
      privacy_engine_xl.py

+ 8 - 0
privacy_engine_xl.py

@@ -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):
     """