[英]How do I reshape this tensor?
我有一个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)
但这不起作用..
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])
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())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.