简体   繁体   English

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

[英]Reshape tensor in custom order (PyTorch)

I have the following tensor我有以下张量

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

I want to reshape it in the following way:我想通过以下方式重塑它:

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

Are there ways to efficiently reshape tensors in that fashion?有没有办法以这种方式有效地重塑张量?

You can achieve this by reshaping, transposing and reshaping back:你可以通过重塑、转置和重塑来实现这一点:

>>> 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]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM