Skip to content
DeepForgeby svx
All problems
MediumDeep Learningdl-023

SAME Padding Sizes

Problem

Compute the asymmetric padding needed for SAME padding with a stride. The output length is ceil(n / stride); total padding is max(0, (output - 1) * stride + dilation * (k - 1) + 1 - n), split as floor(total / 2) before and the remainder after.

The signature is same_padding(n, k, stride=1, dilation=1). Return [pad_before, pad_after].

Hint

Extra padding on odd totals goes after the input, matching common framework behavior.

Test cases

case 1
in: [5,3]
expected: [1,1]
case 2
in: [6,3,2]
expected: [0,1]
case 3
in: [7,3,2]
expected: [1,1]
case 4
in: [4,3,3]
expected: [1,1]
case 5
in: [5,3,1,2]
expected: [2,2]
Starter codePython
def same_padding(n, k, stride=1, dilation=1):
    # Your code here
    pass

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