Python module
conv
The conv
module provides classes for performing convolution operations in
various dimensions (1D, 2D, and 3D) on tensor inputs. These convolution
operations are core building blocks for neural networks, especially in computer
vision and sequence processing tasks.
Here’s an example demonstrating how to use a 1D convolution:
import max.nn as nn
from max.graph import Graph, ops, Weight
from max.dtype import DType
import numpy as np
with Graph(name="conv_example") as graph:
# Define dimensions
batch_size = 2
seq_length = 10
in_channels = 16
out_channels = 32
kernel_size = 3
# Create input tensor [batch_size, sequence_length, channels]
x_data = np.zeros((batch_size, seq_length, in_channels), dtype=np.float32)
x = ops.constant(x_data, dtype=DType.float32)
# Create weights for convolution
filter_1d = Weight(
name="filter_weight",
dtype=DType.float32,
shape=[kernel_size, in_channels, out_channels]
)
bias_1d = Weight(
name="bias_weight",
dtype=DType.float32,
shape=[out_channels]
)
# Create and apply Conv1D layer
conv1d = nn.Conv1D(
filter=filter_1d,
bias=bias_1d,
stride=1,
padding=1
)
output_1d = conv1d(x)
print(f"Conv1D output shape: {output_1d.shape}")
# Output: Conv1D output shape: [Dim(2), Dim(10), Dim(32)]
import max.nn as nn
from max.graph import Graph, ops, Weight
from max.dtype import DType
import numpy as np
with Graph(name="conv_example") as graph:
# Define dimensions
batch_size = 2
seq_length = 10
in_channels = 16
out_channels = 32
kernel_size = 3
# Create input tensor [batch_size, sequence_length, channels]
x_data = np.zeros((batch_size, seq_length, in_channels), dtype=np.float32)
x = ops.constant(x_data, dtype=DType.float32)
# Create weights for convolution
filter_1d = Weight(
name="filter_weight",
dtype=DType.float32,
shape=[kernel_size, in_channels, out_channels]
)
bias_1d = Weight(
name="bias_weight",
dtype=DType.float32,
shape=[out_channels]
)
# Create and apply Conv1D layer
conv1d = nn.Conv1D(
filter=filter_1d,
bias=bias_1d,
stride=1,
padding=1
)
output_1d = conv1d(x)
print(f"Conv1D output shape: {output_1d.shape}")
# Output: Conv1D output shape: [Dim(2), Dim(10), Dim(32)]
Conv1D
class max.nn.conv.Conv1D(filter: Value | BufferValue | TensorValue | Shape | Dim | int | float | integer | floating | ndarray, bias: Value | BufferValue | TensorValue | Shape | Dim | int | float | integer | floating | ndarray | None = None, stride: int = 1, padding: int = 0, dilation: int = 1, groups: int = 1)
A 1D convolution over an input signal composed of several input planes.
Example
conv = nn.Conv1D(
filter=filter_1d,
bias=bias_1d,
stride=1,
padding=1
)
conv = nn.Conv1D(
filter=filter_1d,
bias=bias_1d,
stride=1,
padding=1
)
bias
bias*: Value | BufferValue | TensorValue | Shape | Dim | int | float | integer | floating | ndarray | None* = None
dilation
dilation*: int* = 1
filter
filter*: Value | BufferValue | TensorValue | Shape | Dim | int | float | integer | floating | ndarray*
groups
groups*: int* = 1
padding
padding*: int* = 0
stride
stride*: int* = 1
Conv2D
class max.nn.conv.Conv2D(filter: Value | BufferValue | TensorValue | Shape | Dim | int | float | integer | floating | ndarray, bias: Value | BufferValue | TensorValue | Shape | Dim | int | float | integer | floating | ndarray | None = None, stride: int | tuple[int, int] = (1, 1), padding: int | tuple[int, int, int, int] = (0, 0, 0, 0), dilation: int | tuple[int, int] = (1, 1), groups: int = 1)
A 2D convolution over an input signal composed of several input planes.
Example
conv = nn.Conv2D(
filter=filter_2d,
bias=bias_2d,
stride=2,
padding=1
)
output = conv(x)
conv = nn.Conv2D(
filter=filter_2d,
bias=bias_2d,
stride=2,
padding=1
)
output = conv(x)
bias
bias*: Value | BufferValue | TensorValue | Shape | Dim | int | float | integer | floating | ndarray | None* = None
dilation
filter
filter*: Value | BufferValue | TensorValue | Shape | Dim | int | float | integer | floating | ndarray*
groups
groups*: int* = 1
padding
stride
Conv3D
class max.nn.conv.Conv3D(filter: Value | BufferValue | TensorValue | Shape | Dim | int | float | integer | floating | ndarray, bias: Value | BufferValue | TensorValue | Shape | Dim | int | float | integer | floating | ndarray | None = None, stride: int | tuple[int, int, int] = (1, 1, 1), padding: int | tuple[int, int, int, int, int, int] = (0, 0, 0, 0, 0, 0), dilation: int | tuple[int, int, int] = (1, 1, 1), groups: int = 1)
A 3D convolution over an input signal composed of several input planes.
Example
conv = nn.Conv3D(
filter=filter_3d,
bias=bias_3d,
stride=1,
padding=1
)
conv = nn.Conv3D(
filter=filter_3d,
bias=bias_3d,
stride=1,
padding=1
)
bias
bias*: Value | BufferValue | TensorValue | Shape | Dim | int | float | integer | floating | ndarray | None* = None
dilation
filter
filter*: Value | BufferValue | TensorValue | Shape | Dim | int | float | integer | floating | ndarray*
groups
groups*: int* = 1
padding
padding*: int | tuple[int, int, int, int, int, int]* = (0, 0, 0, 0, 0, 0)
stride
Conv3DV2
class max.nn.conv.Conv3DV2(depth: int, height: int, width: int, in_channels: int, out_channels: int, dtype: DType, stride: int | tuple[int, int, int] = 1, padding: int | tuple[int, int, int, int, int, int] = 0, dilation: int | tuple[int, int, int] = 1, num_groups: int = 1, device: DeviceRef | None = None, has_bias: bool = False, permute: bool = False, name: str | None = None)
A 3D convolution over an input signal composed of several input planes.
Example
conv = nn.Conv3DV2(
depth=,
height=,
width=,
in_channels=,
out_channels=,
dtype=DType.float32,
stride=1,
padding=0,
has_bias=False,
name="conv3d_weight",
device=DeviceRef.GPU(),
)
conv = nn.Conv3DV2(
depth=,
height=,
width=,
in_channels=,
out_channels=,
dtype=DType.float32,
stride=1,
padding=0,
has_bias=False,
name="conv3d_weight",
device=DeviceRef.GPU(),
)
Initializes the Conv3D layer with weights and optional bias.
-
Parameters:
- depth – kernel_size[0]
- height – kernel_size[1]
- width – kernel_size[2]
- in_channels – number of channels in the input image.
- out_channels – dimensionality of the output.
- dtype – The data type for both weights and bias.
- stride – Stride of the convolution. Default: 1
- padding – Padding added to all six sides of the input. Default: 0
- dilation – Spacing between kernel elements. Default: 1
- num_groups – Number of blocked connections from input channels to output channels. Default: 1.
- device – The target device for computation. Weights remain on CPU until moved during computation.
- name – Base name for weights (appended with
.weight
and.bias
if applicable). - has_bias – When
True
, adds a bias vector to the layer. Defaults toFalse
.
bias
The optional bias vector stored on CPU with shape (out_channels,).
Model init moves the bias to device
if present.
device
device*: DeviceRef | None*
The device where matrix operations are performed.
dilation
Not implemented yet. Assuming dilation = 1 for now.
filter
filter*: Weight*
The weight matrix stored on CPU with shape (depth, height, width, in_channels / num_groups, out_channels).
Model init moves the weight to device
.
num_groups
num_groups*: int*
Not implemented yet. Assuming num_groups = 1 for now.
padding
Controls the amount of padding applied before and after the input for depth, height, and width dimensions.
permute
permute*: bool*
bool controls whether self.filter is permuted from PyTorch order to max order. PyTorch order is: (out_channels, in_channels / num_groups, depth, height, width) Max API order: (depth, height, width, in_channels / num_groups, out_channels).
stride
Controls the stride for the cross-correlation.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!