1. 程式人生 > >pytorch: torch.Tensor.view ------ reshape

pytorch: torch.Tensor.view ------ reshape

torch.Tensoe.view(python method, in torch.Tensor)

作用: 將輸入的torch.Tensor改變形狀(size)並返回.返回的Tensor與輸入的Tensor必須有相同的元素,相同的元素數目,但形狀可以不一樣

即,view起到的作用是reshape,view的引數的是改變後的shape.

示例如下:

>>> x = torch.randn(4, 4)
>>> x.size()
torch.Size([4, 4])
>>> y = x.view(16)
>>> y.size()
torch.Size([16])
>>> z = x.view(-1, 8)  # the size -1 is inferred from other dimensions
>>> z.size()
torch.Size([2, 8])

view_as:

        tensor_1.view_as(tensor_2):將tensor_1的形狀改成與tensor_2一樣