对于张量:
x = torch.tensor([
[
[[0.4495, 0.2356],
[0.4069, 0.2361],
[0.4224, 0.2362]],
[[0.4357, 0.6762],
[0.4370, 0.6779],
[0.4406, 0.6663]]
],
[
[[0.5796, 0.4047],
[0.5655, 0.4080],
[0.5431, 0.4035]],
[[0.5338, 0.6255],
[0.5335, 0.6266],
[0.5204, 0.6396]]
]
])
首先,我们想把它分成2个(x.shape[0])张量,然后再把它们合并。在这里,只要我得到正确的输出,我就不需要实际拆分它,但是对我来说,在视觉上拆分它然后将它们合并在一起会更有意义
例如:
# the shape of the splits are always the same
split1 = torch.tensor([
[[0.4495, 0.2356],
[0.4069, 0.2361],
[0.4224, 0.2362]],
[[0.4357, 0.6762],
[0.4370, 0.6779],
[0.4406, 0.6663]]
])
split2 = torch.tensor([
[[0.5796, 0.4047],
[0.5655, 0.4080],
[0.5431, 0.4035]],
[[0.5338, 0.6255],
[0.5335, 0.6266],
[0.5204, 0.6396]]
])
split1 = torch.cat((split1[0], split1[1]), dim=1)
split2 = torch.cat((split2[0], split2[1]), dim=1)
what_i_want = torch.cat((split1, split2), dim=0).reshape(x.shape[0], split1.shape[0], split1.shape[1])
对于上面的结果,我认为直接重塑x.Reforme([2,3,4])会起作用,它会导致正确的尺寸,但结果不正确
总的来说,我是:
多谢各位
您的示例使用
numpy
方法(我没有安装tensorflow):numpy
有一个stack
将数组连接到一个新的轴上,因此我们可以跳过最后一个连接并使用直接的方法是交换中间的两个维度:
reshape
可用于组合“相邻”维度,但不会对基础数据重新排序。也就是说x.ravel()
与reshape
保持相同。虽然允许将(2,2,3,2)重塑为(2,3,4),但值的明显顺序可能不是您想要的。如果您尝试重塑,这可能更容易看到将其与转置进行比较:
我在[569]中所做的转置/交换可能很难理解。有足够多不同的方法对维度进行重新排序,很难一概而论
在matlab、fortran等语言中,顺序为column major:
对于高维张量,这意味着元素的顺序是从最后一维到第一维
您可以使用^{} 后跟^{} 轻松地将其可视化:
结果与
如您所见,元素首先按行(最后一个维度)排序,然后按列排序,最后按第一个维度排序
当你} 张量,则会更改元素的基本顺序
reshape
一个张量时,你不会改变元素的基本顺序,只会改变张量的shape
。但是,如果使用^{看一下
a.view(3,2,4)
和a.permute(0,1,2)
之间的区别——得到的两个张量的shape
是相同的,但不是元素的顺序:相关问题 更多 >
编程相关推荐