I’m trying to expand a dense tensor into a dilated one with zero weights to fill the new holes. Suppose I have a [N,K,K] tensor, where N is the number of features and K is the kernel size. A dilated kernel with dilation D will have effective kernel size L=K*(K-1)*(D-1). I want to map [N,K,K] -> [N,L,L], which is essentially blowing up the kernel along the first dimension to create the dilated pattern, while setting the newly inserted entries to zero.

Is there an easy way to perform such an operation in PyTorch? Right now I’m manually figuring out all non-zero dilation indices by looping over the effective kernel window, which is quite inefficient. I’m thinking there’s a way to do this with a smart combination of expand/reshape, but I’m not seeing it.

I think you can construct a transformation matrix, and use a matrix multiplication to implement it. I can show you an example of how to insert 0 in rows and columns. Suppose your K=2, weights are ‘A, B, C, D’, the transformations are like (hopefully you can get my point)

Awesome. I worked it out and it ended up being the matrix you get from keeping every other D rows of an identity matrix of size L. Here’s an implementation and visualization.