Created
December 8, 2019 07:49
-
-
Save AhmadMoussa/d32c41c11366440bc5eaf4efb48a2e73 to your computer and use it in GitHub Desktop.
Calculate padding of Conv1D and ConvTranspose1D in pytorch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
def calculatePadding(L, KSZ, S, D, deconv = False): | |
''' | |
:param L: Input length (or width) | |
:param KSZ: Kernel size (or width) | |
:param S: Stride | |
:param D: Dilation Factor | |
:return: Returns padding such that output width is exactly half of input width | |
''' | |
if not deconv: | |
return math.ceil((S * (L/2) - L + D * (KSZ - 1)-1)/2) | |
else: | |
print(L, S, D, KSZ) | |
pad = math.ceil(((L - 1) * S + D * (KSZ - 1) + 1 - L * 2)/2) | |
print("PAD", pad) | |
output_size = (L - 1) * S - 2 * pad + D * (KSZ - 1) + 1 | |
print("OUTPUT SIZE", output_size) | |
return pad |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment