我有一个数组列表
T = [np.array([[1, 0, 0], [1, 0, 0], [0, 1, 0], [0, 0, 1]]), np.array([[1, 0, 0], [0, 0, 1], [0, 1, 0], [1, 0, 0]]), np.array([[0, 0, 1], [0, 0, 1], [1, 0, 0], [0, 1,
0]]), np.array([[0, 1, 0], [0, 0, 1], [0, 1, 0], [0, 0, 1]]), np.array([[1, 0, 0], [1, 0, 0], [0, 0, 1], [0, 1, 0]])]
然后是一个张量
T = np.array(T)
该张量的形状为(5, 4, 3)
。我有兴趣 reshape 该张量以获得 (4, 5, 3)
。换句话说,4 个维度为 (5, 3) 的矩阵。第一个矩阵就是简单的
[[1, 0, 0],
[1, 0, 0],
[0, 0, 1],
[0, 1, 0]
[1, 0, 0]]
第二个
[[1, 0, 0],
[0, 0, 1],
[0, 0, 1],
[0, 0, 1]
[1, 0, 0]]
等等
我尝试转置张量,但这对我没有帮助。
如何 reshape T
以获得维度 (4, 5, 3)
?
最佳答案
使用以下内容
np.transpose(T, (1, 0, 2))
第二个参数是张量轴的位置。由于T
的形状为(5, 4, 3)
,这意味着轴0
是5
,轴1
为 4
,轴 2
为 3
。
因此,要使张量具有 (4, 5, 3)
的形状,只需使用 np.transpose
排列它们即可。
替代方法
T.transpose(1, 0, 2)
更多解释来自documentation
axes : list of ints, optional
By default, reverse the dimensions, otherwise permute the axes according to the values given.
关于python - 将一个张量变换为另一个张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54390735/