按自定义顺序重塑张量 (PyTorch)

Reshape tensor in custom order (PyTorch)

我有以下张量

t = torch.tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ,15, 16, 17])

我想按以下方式重塑它:

t_reshape = torch.tensor([[0, 1, 2, 6, 7, 8, 12, 13, 14], 
                          [3, 4, 5, 9, 10, 11, 15, 16, 17]])

有没有办法以这种方式有效地重塑张量?

您可以通过重塑、移调和重塑来实现:

>>> t.reshape(3,2,-1).transpose(0,1).reshape(2,-1)
tensor([[ 0,  1,  2,  6,  7,  8, 12, 13, 14],
        [ 3,  4,  5,  9, 10, 11, 15, 16, 17]])