torch.Tensor.view(*shape)

image

x = torch.randn(4, 4)
print("x size:",x.size())
y = x.view(16)
print("y size:",y.size())
z = x.view(-1, 8)  # the size -1 is inferred from other dimensions
print("z size:",z.size())

a = torch.randn(1, 2, 3, 4)
print("a size:",a.size())
b = a.transpose(1, 2)  # Swaps 2nd and 3rd dimension
print("b size:",b.size())
c = a.view(1, 3, 2, 4)  # Does not change tensor layout in memory
print("c size:",c.size())
print("b equal c:",torch.equal(b, c))

image

posted on 2022-12-08 22:29  朴素贝叶斯  阅读(64)  评论(0编辑  收藏  举报

导航