I am trying to reconstruct a neural network written in tensorflow. For the convolutional layer, they just use padding='SAME'
. This doesn't exist in pytorch. I know, that I can calculate the padding with p = (n - 1) / 2
for stride=1. But what if this doesn't result in an integer value? In my case, n is 4 and I always want to achieve same padding.
Asked
Active
Viewed 319 times
1

spadel
- 998
- 2
- 16
- 40
1 Answers
0
Use math.floor
function to round down to the nearest integer or the math.ceil
function to round up to the nearest integer:
import math
# for flooring
p = math.floor((n - 1) / 2))
# for ceiling
p = math.ceil((n - 1) / 2))
For example, by default, pytorch
uses flooring for MaxPool
layers. So, I think flooring
is a good starting point.

trsvchn
- 8,033
- 3
- 23
- 30
-
thank you for your answer. I tried to just use integer division with `//`, but this leads to different output dimensions, which is what I have to avoid. – spadel Oct 01 '20 at 23:16
-
What about ceiling, does it solve dimension problem? – trsvchn Oct 02 '20 at 17:53