Tensor.view(*shape) -> Tensor
returns a new tensor with the same data as the self tensor but of a different shape
x = torch.randn(4, 4)
x.size()
output: torch.Size([4,4])
y = x.view(16)
y.size()
output: torch.Size([16])
z = x.view(-1, 8) # the size -1 is inferred from other dimensions
z.size()
output: torch.Size([2,8])
a = torch.randn(1, 2, 3, 4)
a.size()
b = a.transpose(1, 2) # Swaps 2nd and 3rd dimension
b.size()
c = a.view(1, 3, 2, 4) # Does not change tensor layout in memory
c.size()
torch.equal(b, c)
网友评论