PyTorch的torch.transpose函数仅转置2D输入。文档是here。
另一方面,Tensorflow的tf.transpose函数允许您转置N任意维度的张量。
有人能解释一下为什么PyTorch没有/不能有N维转置功能吗?这是不是由于PyTorch中计算图构造的动态性质与Tensorflow的定义然后运行范式的原因?
发布于 2017-06-30 16:46:28
它只是在pytorch中以不同的方式调用。torch.Tensor.permute将允许你在pytorch中交换维度,就像TensorFlow中的tf.transpose一样。
作为如何将4D图像张量从NHWC转换为NCHW的示例(未经过测试,因此可能包含错误):
>>> img_nhwc = torch.randn(10, 480, 640, 3)
>>> img_nhwc.size()
torch.Size([10, 480, 640, 3])
>>> img_nchw = img_nhwc.permute(0, 3, 1, 2)
>>> img_nchw.size()
torch.Size([10, 3, 480, 640])发布于 2019-07-01 03:18:48
Einops支持任意维度的冗长换位:
from einops import rearrange
x = torch.zeros(10, 100, 100, 3)
y = rearrange(x, 'b c h w -> b h w c')
x2 = rearrange(y, 'b h w c -> b c h w') # inverse to the first(同样的代码也适用于tensorfow )
https://stackoverflow.com/questions/44841654
复制相似问题