代码
import torch
v1 = torch.arange(0, 16)
print(v1)
v2 = v1.view(-1, 8)
print(v2)
v3 = v1.view(-1, 1)
print(v3)
v4 = v1.view(-1)
运行结果
tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
tensor([[ 0, 1, 2, 3, 4, 5, 6, 7],
[ 8, 9, 10, 11, 12, 13, 14, 15]])
tensor([[ 0],
[ 1],
[ 2],
[ 3],
[ 4],
[ 5],
[ 6],
[ 7],
[ 8],
[ 9],
[10],
[11],
[12],
[13],
[14],
[15]])
tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])
总结
view(-1, *)是根据后面的维度自动调整维度,view(-1)是直接拍成一维的