pytoch pythonノート

1167 ワード

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