remainder() and fmod() in PyTorch

*Memos:

My post explains div().

My post explains add(), sub() and mul().

remainder() can do the modulo(mod) calculation of Python’s modulus operation with two of the 0D or more D tensors of zero or more elements or the 0D or more D tensor of zero…


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

*Memos:

remainder() can do the modulo(mod) calculation of Python’s modulus operation with two of the 0D or more D tensors of zero or more elements or the 0D or more D tensor of zero or more elements and a scalar, getting the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • remainder() can be used with torch or a tensor.
  • The 1st argument(input) with torch(Type:tensor or scalar of int or float) or using a tensor(Type:tensor of int or float)(Required). *torch must use a scalar without input=.
  • The 2nd argument with torch or the 1st argument with a tensor is other(Required-Type:tensor or scalar of int or float).
  • There is out argument with torch(Optional-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • Setting 0(int) to other gets ZeroDivisionError.
  • The combination of a scalar(input) and a scalar(other) cannot be used.
  • The result has the same sign as other.
import torch

tensor1 = torch.tensor([9, 7, 6])
tensor2 = torch.tensor([[4, -4, 3], [-2, 5, -5]])

torch.remainder(input=tensor1, other=tensor2)
tensor1.remainder(other=tensor2)
# tensor([[1, -1, 0], [-1, 2, -4]])

torch.remainder(9, other=tensor2)
# tensor([[1, -3, 0], [-1, 4, -1]])

torch.remainder(input=tensor1, other=4)
# tensor([1, 3, 2])

tensor1 = torch.tensor([-9, -7, -6])
tensor2 = torch.tensor([[4, -4, 3], [-2, 5, -5]])

torch.remainder(input=tensor1, other=tensor2)
# tensor([[3, -3, 0],

torch.remainder(-9, other=tensor2)
# tensor([[3, -1, 0], [-1, 1, -4]])

torch.remainder(input=tensor1, other=4)
# tensor([3, 1, 2])

tensor1 = torch.tensor([9.75, 7.08, 6.26])
tensor2 = torch.tensor([[4.26, -4.54, 3.37], [-2.16, 5.43, -5.98]])

torch.remainder(input=tensor1, other=tensor2)
# tensor([[1.2300, -2.0000, 2.8900],
#         [-1.0500, 1.6500, -5.7000]])

torch.remainder(9.75, other=tensor2)
# tensor([[1.2300, -3.8700, 3.0100], [-1.0500, 4.3200, -2.2100]])

torch.remainder(input=tensor1, other=4.26)
# tensor([1.2300, 2.8200, 2.0000])

fmod() can do the modulo(mod) calculation of C++’s std::fmod with two of the 0D or more D tensors of zero or more elements or the 0D or more D tensor of zero or more elements and a scalar, getting the 0D or more D tensor of zero or more elements as shown below:

*Memos:

  • fmod() can be used with torch or a tensor.
  • The 1st argument(input) with torch or using a tensor(Required-Type:tensor of int or float).
  • The 2nd argument with torch or the 1st argument with a tensor is other(Required-Type:tensor or scalar of int or float).
  • There is out argument with torch(Optional-Type:tensor): *Memos:
    • out= must be used.
    • My post explains out argument.
  • Setting 0(int) to other gets ZeroDivisionError.
  • The result has the same sign as the original tensor.
import torch

tensor1 = torch.tensor([9, 7, 6])
tensor2 = torch.tensor([[4, -4, 3], [-2, 5, -5]])

torch.fmod(input=tensor1, other=tensor2)
tensor1.fmod(other=tensor2)
# tensor([[1, 3, 0], [1, 2, 1]])

torch.fmod(input=tensor1, other=4)
# tensor([1, 3, 2])

tensor1 = torch.tensor([-9, -7, -6])
tensor2 = torch.tensor([[4, -4, 3], [-2, 5, -5]])

torch.fmod(input=tensor1, other=tensor2)
# tensor([[-1, -3, 0], [-1, -2, -1]])

torch.fmod(input=tensor1, other=4)
# tensor([-1, -3, -2])

tensor1 = torch.tensor([9.75, 7.08, 6.26])
tensor2 = torch.tensor([[4.26, -4.54, 3.37], [-2.16, 5.43, -5.98]])

torch.fmod(input=tensor1, other=tensor2)
# tensor([[1.2300, 2.5400, 2.8900], [1.1100, 1.6500, 0.2800]])

torch.fmod(input=tensor1, other=4.26)
# tensor([1.2300, 2.8200, 2.0000])


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)


Print Share Comment Cite Upload Translate Updates
APA

Super Kai (Kazuya Ito) | Sciencx (2024-07-14T20:57:52+00:00) remainder() and fmod() in PyTorch. Retrieved from https://www.scien.cx/2024/07/14/remainder-and-fmod-in-pytorch/

MLA
" » remainder() and fmod() in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Sunday July 14, 2024, https://www.scien.cx/2024/07/14/remainder-and-fmod-in-pytorch/
HARVARD
Super Kai (Kazuya Ito) | Sciencx Sunday July 14, 2024 » remainder() and fmod() in PyTorch., viewed ,<https://www.scien.cx/2024/07/14/remainder-and-fmod-in-pytorch/>
VANCOUVER
Super Kai (Kazuya Ito) | Sciencx - » remainder() and fmod() in PyTorch. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/14/remainder-and-fmod-in-pytorch/
CHICAGO
" » remainder() and fmod() in PyTorch." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2024/07/14/remainder-and-fmod-in-pytorch/
IEEE
" » remainder() and fmod() in PyTorch." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2024/07/14/remainder-and-fmod-in-pytorch/. [Accessed: ]
rf:citation
» remainder() and fmod() in PyTorch | Super Kai (Kazuya Ito) | Sciencx | https://www.scien.cx/2024/07/14/remainder-and-fmod-in-pytorch/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.