使用 pytorch 洗牌给定张量的矢量化方法
Vectorized way to shuffle a given tensor using pytorch
我有一个形状为 (1,12,2,2) 的张量 A,如下所示:
([[[[1., 3.],
[9., 11.],
[[ 2., 4.],
[10., 12.]],
[[ 5., 7.],
[13., 15.]],
[[ 6., 8.],
[14., 16.]],
[[17., 19.],
[25., 27.]],
[[18., 20.],
[26., 28.]],
[[21., 23.],
[29., 31.]],
[[22., 24.],
[30., 32.]],
[[33., 35.],
[41., 43.]],
[[34., 36.],
[42., 44.]],
[[37., 39.],
[45., 47.]],
[[38., 40.],
[46., 48.]]]])
我想使用 pytorch 对其进行洗牌以生成以下形状为 (1,3,4,4) 的张量 B:
tensor([[[[ 1., 6., 3., 8.],
[21., 34., 23., 36.],
[ 9., 14., 11., 16.],
[29., 42., 31., 44.]],
[[ 2., 17., 4., 19.],
[22., 37., 24., 39.],
[10., 25., 12., 27.],
[30., 45., 32., 47.]],
[[ 5., 18., 7., 20.],
[33., 38., 35., 40.],
[13., 26., 15., 28.],
[41., 46., 43., 48.]]]])
我使用两个 for 循环实现了这个,如下所示:
B = torch.zeros(1,3,4,4, dtype=torch.float)
ctr = 0
for i in range(2):
for j in range(2):
B[:,:,i:4:2,j:4:2] = A[:,ctr:ctr+3,:,:]
ctr = ctr+3
我正在寻找在没有这些 for 循环的情况下在 pytorch 中以矢量化方式实现它的任何方法。也许使用 .permute()
等函数
这样就可以了
B = A.reshape(2,2,3,2,2).permute(2,3,0,4,1).reshape(1,3,4,4)
只是将上述解决方案推广到任何上采样因子 'r',例如像素洗牌。
B = A.reshape(-1,r,3,s,s).permute(2,3,0,4,1).reshape(1,3,rs,rs)
这里's'是每个通道的空间分辨率'A','r'是上采样因子。对于特定情况,r=2 和 s=2。此解决方案应该适用于 'r' 的任意值和 'A' 的适当大小。
所以对于手头的问题 s=2, r=2 等解法是
B = A.reshape(-1,2,3,2,2).permute(2,3,0,4,1).reshape(1,3,4,4)
@ddoGas 发布
类似地,如果 'A' 的大小为 (1, 192, 356, 532) 并且想要按 r=8 做
上采样
B = A.reshape(-1,8,3,356,532).permute(2,3,0,4,1).reshape(1,3,2848,4256)
我有一个形状为 (1,12,2,2) 的张量 A,如下所示:
([[[[1., 3.],
[9., 11.],
[[ 2., 4.],
[10., 12.]],
[[ 5., 7.],
[13., 15.]],
[[ 6., 8.],
[14., 16.]],
[[17., 19.],
[25., 27.]],
[[18., 20.],
[26., 28.]],
[[21., 23.],
[29., 31.]],
[[22., 24.],
[30., 32.]],
[[33., 35.],
[41., 43.]],
[[34., 36.],
[42., 44.]],
[[37., 39.],
[45., 47.]],
[[38., 40.],
[46., 48.]]]])
我想使用 pytorch 对其进行洗牌以生成以下形状为 (1,3,4,4) 的张量 B:
tensor([[[[ 1., 6., 3., 8.],
[21., 34., 23., 36.],
[ 9., 14., 11., 16.],
[29., 42., 31., 44.]],
[[ 2., 17., 4., 19.],
[22., 37., 24., 39.],
[10., 25., 12., 27.],
[30., 45., 32., 47.]],
[[ 5., 18., 7., 20.],
[33., 38., 35., 40.],
[13., 26., 15., 28.],
[41., 46., 43., 48.]]]])
我使用两个 for 循环实现了这个,如下所示:
B = torch.zeros(1,3,4,4, dtype=torch.float)
ctr = 0
for i in range(2):
for j in range(2):
B[:,:,i:4:2,j:4:2] = A[:,ctr:ctr+3,:,:]
ctr = ctr+3
我正在寻找在没有这些 for 循环的情况下在 pytorch 中以矢量化方式实现它的任何方法。也许使用 .permute()
等函数
这样就可以了
B = A.reshape(2,2,3,2,2).permute(2,3,0,4,1).reshape(1,3,4,4)
只是将上述解决方案推广到任何上采样因子 'r',例如像素洗牌。
B = A.reshape(-1,r,3,s,s).permute(2,3,0,4,1).reshape(1,3,rs,rs)
这里's'是每个通道的空间分辨率'A','r'是上采样因子。对于特定情况,r=2 和 s=2。此解决方案应该适用于 'r' 的任意值和 'A' 的适当大小。
所以对于手头的问题 s=2, r=2 等解法是
B = A.reshape(-1,2,3,2,2).permute(2,3,0,4,1).reshape(1,3,4,4)
@ddoGas 发布
类似地,如果 'A' 的大小为 (1, 192, 356, 532) 并且想要按 r=8 做
上采样B = A.reshape(-1,8,3,356,532).permute(2,3,0,4,1).reshape(1,3,2848,4256)