PyTorch torch.nn.Conv3d function
PyTorch torch.nn Reference Manual
torch.nn.Conv3d is the three-dimensional convolution module in PyTorch.
It processes three-dimensional inputs, such as video or medical images (depth x height x width).
Function definition
torch.nn.Conv3d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)
Input shape
(batch, channels, depth, height, width)
Usage examples
Example 1: Basic usage
Example
import
torch
import
torch.
nn
as
nn
3D convolution
conv3d
=
nn.
Conv3d
(
in_channels
=
3
,
out_channels
=
32
,
kernel_size
=
3
,
padding
=
1
)
Input: batch=2, channels=3, depth=16, height=32, width=32
x
=
torch.
randn
(
2
,
3
,
16
,
32
,
32
)
output
=
conv3d
(
x
)
(
"Input shape:"
,
x.
shape
)
(
"Output shape:"
,
output.
shape
)
Example 2: Video classification
Example
import
torch
import
torch.
nn
as
nn
class
VideoCNN
(
nn.
Module
)
:
def
init
(
self
,
num_classes
=
10
)
:
super
(
VideoCNN
,
self
)
.
init
(
)
self
.
conv1
=
nn.
Conv3d
(
3
,
32
,
kernel_size
=
3
,
padding
=
1
)
self
.
pool
=
nn.
MaxPool3d
(
2
)
self
.
conv2
=
nn.
Conv3d
(
32
,
64
,
kernel_size
=
3
,
padding
=
1
)
self
.
gap
=
nn.
AdaptiveAvgPool3d
(
1
)
self
.
fc
=
nn.
Linear
(
64
,
num_classes
)
def
forward
(
self
,
x
)
:
x
=
torch.
relu
(
self
.
conv1
(
x
)
)
x
=
self
.
pool
(
x
)
x
=
torch.
relu
(
self
.
conv2
(
x
)
)
x
=
self
.
gap
(
x
)
x
=
x.
view
(
x.
size
(
0
)
,
1
)
return
self
.
fc
(
x
)
model
=
VideoCNN
(
)
x
=
torch.
randn
(
4
,
3
,
16
,
112
,
112
)
16-frame video
output
=
model
(
x
)
(
"Video:"
,
x.
shape
,
"-> Class:"
,
output.
shape
)
Example 3: Parameter calculation
Example
import
torch
import
torch.
nn
as
nn
conv
=
nn.
Conv3d
(
64
,
128
,
kernel_size
=
3
,
padding
=
1
)
Parameter count: out_ch * in_ch * k_d * k_h * k_w + bias
params
=
128
64
3
3
3
(
"Parameter count:"
,
params
)
(
"Weight shape:"
,
conv.
weight
.
shape
)
Use Cases
- Video processing: Action recognition
- Medical imaging: CT, MRI
- 3D segmentation: Voxel data
Note: 3D convolutions are computationally expensive and require more GPU memory.