pytoch python 筆記

一、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用法

https://blog.csdn.net/qq_38469553/article/details/85290207

三、pytorch学习 中 torch.squeeze() 和torch.unsqueeze()的用法

https://blog.csdn.net/xiexu911/article/details/80820028

你可能感兴趣的:(学习笔记)