DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Updated on

Conv3d in PyTorch

Buy Me a Coffee

*Memos:

Conv3d() can get the 4D or 5D tensor of the one or more elements computed by 3D convolution from the 4D or 5D tensor of one or more elements as shown below:

*Memos:

  • The 1st argument for initialization is in_channels(Required-Type:float). *It must be 1 <= x.
  • The 2nd argument for initialization is out_channels(Required-Type:float). *It must be 1 <= x.
  • The 3rd argument for initialization is kernel_size(Required-Type:int or tuple or list of int). *It must be 1 <= x.
  • The 4th argument for initialization is stride(Optional-Default:1-Type:int or tuple or list of int). *It must be 1 <= x.
  • The 5th argument for initialization is padding(Optional-Default:0-Type:int, str or tuple or list of int): *Memos:
    • It must be 0 <= x if not str.
    • It must be either 'valid' or 'same' for str.
  • The 6th argument for initialization is dilation(Optional-Default:1-Type:int, tuple or list of int). *It must be 1 <= x.
  • The 7th argument for initialization is groups(Optional-Default:1-Type:int). *It must be 1 <= x.
  • The 8th argument for initialization is bias(Optional-Default:True-Type:bool). *My post explains bias argument.
  • The 9th argument for initialization is padding_mode(Optional-Default:'zeros'-Type:str). *'zeros', 'reflect', 'replicate' or 'circular' can be selected.
  • The 10th argument for initialization is device(Optional-Default:None-Type:str, int or device()): *Memos:
  • The 11th argument for initialization is dtype(Optional-Default:None-Type:dtype): *Memos:
  • The 1st argument is input(Required-Type:tensor of float or complex): *Memos:
    • It must be the 4D or 5D tensor of one or more elements.
    • The number of the elements of the 4th deepest dimension must be same as in_channels.
    • Its device and dtype must be same as Conv3d()'s.
    • complex must be set to dtype of Conv3d() to use a complex tensor.
    • The tensor's requires_grad which is False by default is set to True by Conv3d().
  • conv3d1.device and conv3d1.dtype don't work.


import torch
from torch import nn

tensor1 = torch.tensor([[[[8., -3., 0., 1., 5., -2.]]]])

tensor1.requires_grad
# False

torch.manual_seed(42)

conv3d1 = nn.Conv3d(in_channels=1, out_channels=3, kernel_size=1)
tensor2 = conv3d1(input=tensor1)
tensor2
# tensor([[[[7.0349, -1.3750, 0.9186, 1.6831, 4.7413, -0.6105]]],
#         [[[6.4210, -2.7091, -0.2191, 0.6109, 3.9309, -1.8791]]],
#         [[[-1.6724, 0.9046, 0.2018, -0.0325, -0.9696, 0.6703]]]],
#        grad_fn=<SqueezeBackward1>)

tensor2.requires_grad
# True

conv3d1
# Conv3d(1, 3, kernel_size=(1, 1, 1), stride=(1, 1, 1))

conv3d1.in_channels
# 1

conv3d1.out_channels
# 3

conv3d1.kernel_size
# (1, 1, 1)

conv3d1.stride
# (1, 1, 1)

conv3d1.padding
# (0, 0, 0)

conv3d1.dilation
# (1, 1, 1)

conv3d1.groups
# 1

conv3d1.bias
# Parameter containing:
# tensor([0.9186, -0.2191, 0.2018], requires_grad=True)

conv3d1.padding_mode
# 'zeros'

conv3d1.weight
# Parameter containing:
# tensor([[[[[0.7645]]]], [[[[0.8300]]]], [[[[-0.2343]]]]],
# requires_grad=True)

torch.manual_seed(42)

conv3d2 = nn.Conv3d(in_channels=3, out_channels=3, kernel_size=1)
conv3d2(input=tensor2)
# tensor([[[[5.9849, -2.4511, -0.1504, 0.6165, 3.6841, -1.6842]]],
#         [[[3.2258, 0.2207, 1.0403, 1.3134, 2.4062, 0.4939]]],
#         [[[-0.5434, 0.0364, -0.1217, -0.1744, -0.3853, -0.0163]]]],
#        grad_fn=<SqueezeBackward1>)

torch.manual_seed(42)

conv3d = nn.Conv3d(in_channels=1, out_channels=3, kernel_size=1, stride=1, 
                   padding=0, dilation=1, groups=1, bias=True,
                   padding_mode='zeros', device=None, dtype=None)
conv3d(input=tensor1)
# tensor([[[[7.0349, -1.3750, 0.9186, 1.6831, 4.7413, -0.6105]]],
#         [[[6.4210, -2.7091, -0.2191, 0.6109, 3.9309, -1.8791]]],
#         [[[-1.6724, 0.9046, 0.2018, -0.0325, -0.9696, 0.6703]]]],
#        grad_fn=<SqueezeBackward1>)

my_tensor = torch.tensor([[[[8., -3., 0.],
                            [1., 5., -2.]]]])
torch.manual_seed(42)

conv3d = nn.Conv3d(in_channels=1, out_channels=3, kernel_size=1)
conv3d(input=my_tensor)
# tensor([[[[7.0349, -1.3750, 0.9186], [1.6831, 4.7413, -0.6105]]],
#         [[[6.4210, -2.7091, -0.2191], [0.6109, 3.9309, -1.8791]]],
#         [[[-1.6724, 0.9046, 0.2018], [-0.0325, -0.9696, 0.6703]]]],
#        grad_fn=<SqueezeBackward1>)

my_tensor = torch.tensor([[[[8.], [-3.], [0.],
                            [1.], [5.], [-2.]]]])
torch.manual_seed(42)

conv3d = nn.Conv3d(in_channels=1, out_channels=3, kernel_size=1)
conv3d(input=my_tensor)
# tensor([[[[7.0349], [-1.3750], [0.9186], [1.6831], [4.7413], [-0.6105]]],
#         [[[6.4210], [-2.7091], [-0.2191], [0.6109], [3.9309], [-1.8791]]],
#         [[[-1.6724], [0.9046], [0.2018], [-0.0325], [-0.9696], [0.6703]]]],
#        grad_fn=<SqueezeBackward1>)

my_tensor = torch.tensor([[[[[8.], [-3.], [0.]],
                            [[1.], [5.], [-2.]]]]])
torch.manual_seed(42)

conv3d = nn.Conv3d(in_channels=1, out_channels=3, kernel_size=1)
conv3d(input=my_tensor)
# tensor([[[[[7.0349], [-1.3750], [0.9186]],
#           [[1.6831], [4.7413], [-0.6105]]],
#          [[[6.4210], [-2.7091], [-0.2191]]
#           [[0.6109], [3.9309], [-1.8791]]],
#          [[[-1.6724], [0.9046], [0.2018]],
#           [[-0.0325], [-0.9696], [0.6703]]]]],
#        grad_fn=<ConvolutionBackward0>)

my_tensor = torch.tensor([[[[[8.+0.j], [-3.+0.j], [0.+0.j]],
                            [[1.+0.j], [5.+0.j], [-2.+0.j]]]]])
torch.manual_seed(42)

conv3d = nn.Conv3d(in_channels=1, out_channels=3, kernel_size=1, 
                   dtype=torch.complex64)
conv3d(input=my_tensor)
# tensor([[[[[5.6295+7.2273j], [-2.7805-1.9027j], [-0.4869+0.5873j]],
#           [[0.2777+1.4173j], [3.3358+4.7373j], [-2.0159-1.0727j]]],
#          [[[-0.9926+6.6153j], [1.5844-3.4895j], [0.8815-0.7336j]],
#           [[0.6473+0.1850j], [-0.2898+3.8594j], [1.3501-2.5709j]]],
#          [[[-0.8836+1.8015j], [1.5265-0.4182j], [0.8692+0.1872j]],
#           [[0.6501+0.3889j], [-0.2263+1.1961j], [1.3074-0.2164j]]]]],
#        grad_fn=<AddBackward0>)


Enter fullscreen mode Exit fullscreen mode

Top comments (0)