How do I shift specific elements of a tensor with torch.roll?
I have a tensor x, that looks like this:
x = tensor([  1,  2,  3,  4,  5],
           [  6,  7,  8,  9, 10]
           [ 11, 12, 13, 14, 15])

I'm trying to switch the first two and last two numbers of each tensor, like this:
x = tensor([  4,  5,  3,  1,  2],
           [  9, 10,  8,  6,  7],
           [ 14, 15, 13, 11, 12])

How could I do this with torch.roll()? How would I switch 3 instead of 1?

Not sure if that can be done with torch.roll alone... However, you can expect the desired result by using a temporary tensor and a pair assignment:
&gt;&gt;&gt; x = torch.arange(1, 16).reshape(3,-1)
tensor([[ 1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10],
        [11, 12, 13, 14, 15]])

&gt;&gt;&gt; tmp = x.clone()

# swap the two sets of columns
&gt;&gt;&gt; x[:,:2], x[:,-2:] = tmp[:,-2:], tmp[:,:2]

Such that tensor x has been mutated as:
&gt;&gt;&gt; x
tensor([[ 4,  5,  3,  1,  2],
        [ 9, 10,  8,  6,  7],
        [14, 15, 13, 11, 12]])


You can pull off this operation with torch.roll and some indexing:
&gt;&gt;&gt; x = torch.arange(1, 21).reshape(4,-1)
tensor([[ 1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10],
        [11, 12, 13, 14, 15],
        [16, 17, 18, 19, 20]])

&gt;&gt;&gt; rolled = x.roll(-2,0)
tensor([[11, 12, 13, 14, 15],
        [16, 17, 18, 19, 20],
        [ 1,  2,  3,  4,  5],
        [ 6,  7,  8,  9, 10]])

# overwrite columns [1,-1[ from rolled with those from x
&gt;&gt;&gt; rolled[:, 1:-1] = x[:, 1:-1]

Such that at this end you get:
&gt;&gt;&gt; rolled
tensor([[11,  2,  3,  4, 15],
        [16,  7,  8,  9, 20],
        [ 1, 12, 13, 14,  5],
        [ 6, 17, 18, 19, 10]])

