Skip to content
DeepForgeby svx
All problems
EasyOptimizationop-023

Gradient Centralization

Problem

Apply gradient centralization to a gradient vector.

Compute the mean of all components and subtract it from each component, so the returned vector sums to zero. An empty input returns [].

Hint

The result has zero mean by construction.

Test cases

case 1
in: [[1,2,3]]
expected: [-1,0,1]
case 2
in: [[-1,0,1,2]]
expected: [-1.5,-0.5,0.5,1.5]
case 3
in: [[]]
expected: []
case 4
in: [[4,4,4]]
expected: [0,0,0]
Starter codePython
def gradient_centralization(grads):
    # Your code here
    pass

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