view()函数作用是将一个多行的Tensor,拼接成一行。
import torch
a = torch.Tensor(2,3)
print(a)
# tensor([[0.0000, 0.0000, 0.0000],
# [0.0000, 0.0000, 0.0000]])
print(a.view(1,-1))
# tensor([[0.0000, 0.0000, 0.0000, 0.0000, 0.0000, 0.0000]])
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一样
更多参考
https://blog.csdn.net/program_developer/article/details/82112372
https://blog.csdn.net/Strive_For_Future/article/details/83240116
https://blog.csdn.net/lanchunhui/article/details/51258503