concept

Valid 2D cross-correlation

Slide an unflipped kernel over every fully contained image patch, multiply corresponding values and sum. For stride one without padding, output dimensions are H-KH+1 and W-KW+1. One-channel toy operation; not a full CNN.

Formal Syntax & Structure

artificial-intelligence syntaxartificial-intelligence
feature_map = correlate(image, kernel)

Working Example

Example Snippetartificial-intelligence
def correlate(image, kernel):    if not image or not image[0] or not kernel or not kernel[0]:        raise ValueError("Empty image or kernel")    h, w = len(image), len(image[0])    kh, kw = len(kernel), len(kernel[0])    if any(len(row) != w for row in image) or any(len(row) != kw for row in kernel):        raise ValueError("Ragged rows")    if kh > h or kw > w:        raise ValueError("Kernel too large")    return [[sum(image[r+i][c+j] * kernel[i][j]                 for i in range(kh) for j in range(kw))             for c in range(w-kw+1)] for r in range(h-kh+1)]
image = [[1, 1, 3], [1, 1, 3], [2, 2, 4]]kernel = [[-1, 1], [-1, 1]]print(correlate(image, kernel))