TypeError: conv2d(): argument 'padding' (position 5) must be tuple of ints, not str

I created following conv layer:

import torch
from torch import nn
in_channels = 1
out_channels = 3
kernel_size = 2
conv1 = nn.Conv2d(in_channels, out_channels, kernel_size, padding='same')

After calling this layer I get the following error:

TypeError: conv2d(): argument 'padding' (position 5) must be tuple of ints, not str

PyTorch Version: 1.7.0 (in kaggle notebook)

Hi d4riush!

This works for me with pytorch version 1.9.0:

>>> import torch
>>> torch.__version__
'1.9.0'
>>> from torch import nn
>>> in_channels = 1
>>> out_channels = 3
>>> kernel_size = 2
>>> conv1 = nn.Conv2d(in_channels, out_channels, kernel_size, padding='same')
>>> conv1 (torch.randn (1, 1, 5, 5))
<path_to_pytorch>\torch\nn\modules\conv.py:440: UserWarning: Using padding='same' with even kernel lengths and odd dilation may require a zero-padded copy of the input be created (Triggered internally at  ..\aten\src\ATen\native\Convolution.cpp:660.)
  self.padding, self.dilation, self.groups)
tensor([[[[-0.4665,  0.1697, -0.5899, -0.2318,  0.4677],
          [-0.8381,  0.4828, -0.4036, -0.4699,  0.3754],
          [-0.6049, -0.3593, -0.4251,  0.2942, -0.3586],
          [-0.3345, -0.5207, -0.1593,  0.6332, -0.2843],
          [-0.2076, -0.2482, -0.5458, -0.2498,  0.3244]],

         [[-0.3107, -0.4693, -1.0476, -0.4348, -0.1129],
          [-1.0803, -0.3371, -0.1449, -0.2794, -0.3034],
          [-0.7755, -0.4563, -0.4941, -0.8762, -0.8973],
          [-0.5674, -0.5849, -0.1403,  0.5550, -0.0826],
          [-0.4254, -0.4933, -0.7118, -0.7279, -0.4403]],

         [[ 0.2665,  0.9369, -0.0974,  0.4658,  1.2056],
          [-0.4919,  0.7491,  0.1730,  0.5457,  0.9414],
          [-0.4147,  0.0814,  0.3814,  0.6475, -0.2871],
          [-0.0729,  0.0180,  0.5426,  1.3188,  0.5515],
          [ 0.2312,  0.2488,  0.1412,  0.4451,  0.7656]]]],
       grad_fn=<ThnnConv2DBackward>)

Best.

K. Frank

1 Like

Hi, yes I asked this on Stackoverflow, and based on comments turns out this is new feature which added in version 1.9.