Code
Validation of Multi-Channel Cross-Correlation Code
To validate the programmatic implementation of a multi-channel cross-correlation operation, we can construct multi-channel input and kernel tensors corresponding to a known example and compute their cross-correlation. For instance, using an input tensor X with two channels and a kernel tensor K with two channels, we can pass them to our cross-correlation function to ensure the computed output matches the expected mathematical result. The following Python code demonstrates this validation and explicitly shows the computed result tensor([[ 56., 72.], [104., 120.]]):
X = torch.tensor([[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]], [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]) K = torch.tensor([[[0.0, 1.0], [2.0, 3.0]], [[1.0, 2.0], [3.0, 4.0]]]) corr2d_multi_in(X, K) # Output: # tensor([[ 56., 72.], # [104., 120.]])
0
1
Updated 2026-05-12
Tags
D2L
Dive into Deep Learning @ D2L