All problems
Gradient Penalty (Finite Difference)
Problem
Estimate the WGAN gradient penalty with a central finite difference: g_i = (d_plus_i - d_minus_i) / (2 * epsilon), then return the mean of g_i^2 over all i.
The signature is gradient_penalty_finite_difference(d_plus, d_minus, epsilon). d_plus and d_minus are critic scores at perturbed points.
Hint
The penalty pushes the critic's gradient norm toward 1, so the squared slopes are averaged.
Test cases
case 1
in: [[1,0],[0,0],0.1]
expected: 12.5
case 2
in: [[0.5,0.5],[0.5,0.5],0.05]
expected: 0
case 3
in: [[1,2],[0.5,1.5],0.5]
expected: 0.25
case 4
in: [[3,1],[1,3],0.25]
expected: 16
Starter codePython
def gradient_penalty_finite_difference(d_plus, d_minus, epsilon):
# Your code here
pass4 test cases · run your code against them in the editor