What is the difference between mul and mul_ about multiplication in pytorch

What is the difference between mul and mul_ about multiplication in pytorch

2 Likes

The operations with an underscore are inplace operations, i.e. they are working directly on the tensor.
Have a look at the following example:

a = torch.ones(10)
a.mul_(2)
print(a)
b = a.mul(2)
print(a)
print(b)
6 Likes

Ah ha! so they just replace the values

excellent example ~, add the result~

>>> import torch
>>> a = torch.ones(10)
>>> a.mul_(2)
tensor([2., 2., 2., 2., 2., 2., 2., 2., 2., 2.])
>>> print(a)
tensor([2., 2., 2., 2., 2., 2., 2., 2., 2., 2.])
>>> b = a.mul(2)
>>> print(a)
tensor([2., 2., 2., 2., 2., 2., 2., 2., 2., 2.])
>>> print(b)
tensor([4., 4., 4., 4., 4., 4., 4., 4., 4., 4.])

2 Likes