Is there any way to create a tensor with a specific pattern in Pytorch?
I'm working with linear transformation in the form of Y=Q(X+A), where X is the input tensor and Y is the output, Q and A are two tensors to be learned. Q is an arbitrary tensor, therefore I can use nn.Linear. But A is a (differentiable) tensor that has some specific pattern, as a short example,
A = [[a0,a1,a2,a2,a2], 
     [a1,a0,a1,a2,a2],
     [a2,a1,a0,a1,a2],
     [a2,a2,a1,a0,a1],
     [a2,a2,a2,a1,a0]]. 

So I cannot define such a pattern in nn.Linear. Is there any way to define such a tensor in Pytorch?

This looks like a Toeplitz matrix. A possible implementation in PyTorch is:
def toeplitz(c, r):
    vals = torch.cat((r, c[1:].flip(0)))
    shape = len(c), len(r)
    i, j = torch.ones(*shape).nonzero().T
    return vals[j-i].reshape(*shape)

In your case with a0 as 0, a1 as 1 and a2 as 2:
&gt;&gt;&gt; toeplitz(torch.tensor([0,1,2,2,2]), torch.tensor([0,1,2,2,2]))
tensor([[0, 1, 2, 2, 2],
        [1, 0, 1, 2, 2],
        [2, 1, 0, 1, 2],
        [2, 2, 1, 0, 1],
        [2, 2, 2, 1, 0]])

For a more detailed explanation refer to my other answer here.
