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一样

 

猜你喜欢

转载自blog.csdn.net/Strive_For_Future/article/details/83240116