一、pytorch中tensor.expand()和tensor.expand_as()函数解读
tensor.expend()函数,可以看出expand()函数括号里面为变形后的size大小,而且原来的tensor和tensor.expand()是不共享内存的。
>>> a = torch.tensor([[2],[3],[4]])
>>> print(a.size())
torch.Size([3, 1])
>>> a.expand(3,2)
tensor([[2, 2],
[3, 3],
[4, 4]])
>>> a.expand(3,3)
tensor([[2, 2, 2],
[3, 3, 3],
[4, 4, 4]])
>>> a
tensor([[2],
[3],
[4]])
>>>
tensor.expand_as()函数,可以看出,b和a.expand_as(b)的size是一样大的。且是不共享内存的。
>>> b = torch.tensor([[2,3],[3,4],[4,5]])
>>> print(b.size())
torch.Size([3, 2])
>>> a.expand_as(b)
tensor([[2, 2],
[3, 3],
[4, 4]])
>>> b = torch.tensor([[2,3,3],[3,4,5],[4,5,7]])
>>> a.expand_as(b)
tensor([[2, 2, 2],
[3, 3, 3],
[4, 4, 4]])
>>> a
tensor([[2],
[3],
[4]])
>>>
二、torch.max用法
pytorch中torch.max和Tensor.view函数用法_坎幽黑尔弥?的博客-优快云博客_pytorch求tensor的最大值
三、pytorch学习 中 torch.squeeze() 和torch.unsqueeze()的用法
pytorch学习 中 torch.squeeze() 和torch.unsqueeze()的用法_nancheng911的博客-优快云博客_torch.unsqueeze