Convolution is linear, so it can be written as a matrix acting on the flattened image, and transposed convolution is exactly multiplication by the transpose of that matrix.
All arrays are single-channel 2-D. "Convolution" means the deep-learning cross-correlation used in the book, with no padding:
with stride and output size by .
Implement:
conv2d_valid(X, K, stride=1): the formula above.conv_matrix(K, in_shape, stride=1): the matrix with one row per output pixel and one column per input pixel, both flattened in row-major order, such that C @ X.ravel() equals conv2d_valid(X, K, stride).ravel().conv_transpose2d(X, K, stride=1): the stencil form. Start from zeros of shape for an input, and for every input pixel add into the block whose top-left corner is .For an input of size , the transposed convolution must equal (C.T @ X.ravel()) reshaped to the output size, where C = conv_matrix(K, out_shape, stride) is built for the larger output shape.