我有一个torch.tensor
,看起来像这样:
tensor([[[A,B,C],
[D,E,F],
[G,H,I]],
[[J,K,L],
[M,N,O],
[P,Q,R]]]
我想重塑此张量,使其尺寸为(18, 1)
。我希望新的张量看起来像这样:
tensor([A,
J,
B,
K,
C,
L,
D,
M,
...
I,
R])
我尝试过tensor.view(-1,1)
,但这不起作用。
答案 0 :(得分:4)
a = torch.arange(18).view(2,3,3)
print(a)
#tensor([[[ 0, 1, 2],
# [ 3, 4, 5],
# [ 6, 7, 8]],
#
# [[ 9, 10, 11],
# [12, 13, 14],
# [15, 16, 17]]])
aa = a.permute(1,2,0).flatten()
print(aa)
#tensor([ 0, 9, 1, 10, 2, 11, 3, 12, 4, 13, 5, 14, 6, 15, 7, 16, 8, 17])
答案 1 :(得分:1)
view
或reshape
都在这里工作:
t = torch.tensor([[[1,2,3],
[4,5,6],
[7,8,9]],
[[1,2,3],
[1,2,3],
[1,2,3]]])
print(t.size())
t = t.permute(1,2,0).reshape(-1,1)
print(t)
print(t.size())