PyTorch

PyTorch 기본 함수 정리(In-place Operation)

수터디 2022. 6. 18. 12:36
x = torch.FloatTensor([[1,2], [3,4]])
print(x)
# tensor([[1., 2.],
#         [3., 4.]])

# inplace 안했을 때
print(x.mul(2.))
print(x)
#tensor([[2., 4.],
#        [6., 8.]])
#tensor([[1., 2.],
#        [3., 4.]])

# inplace 했을 때
# PyTorch는 garbage collector가 잘 구현되어 있어서 in-place 사용 이점이 없을 수 있음
print(x.mul_(2.))
print(x)
#tensor([[2., 4.],
#        [6., 8.]])
#tensor([[2., 4.],
#        [6., 8.]])