Skip to content
DeepForgeby svx
All problems
EasyOptimizationop-168

Differential Privacy Clip

Problem

Clip a gradient and add calibrated Gaussian noise for DP-SGD.

Scale grad down when its norm exceeds clip_norm, then add random.gauss(0.0, 1.0) * noise_multiplier * clip_norm / batch_size to every coordinate after random.seed(seed). Return the noisy clipped gradient.

Hint

Clipping bounds each per-sample contribution before the noise is added.

Test cases

case 1
in: [[3,4],2.5,1,10,0]
expected: [1.735428851170166,1.6508554738247125]
case 2
in: [[0.1,0.2],5,0.5,4,42]
expected: [0.009943544013794783,0.09193524979280045]
case 3
in: [[1,0],1,0,8,7]
expected: [1,0]
Starter codePython
def dp_clip(grad, clip_norm, noise_multiplier, batch_size, seed):
    # Your code here
    pass

3 test cases · run your code against them in the editor