DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on

fmod in PyTorch

Buy Me a Coffee

*Memos:

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-Default:None-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])
Enter fullscreen mode Exit fullscreen mode

Image of AssemblyAI tool

Challenge Submission: SpeechCraft - AI-Powered Speech Analysis for Better Communication

SpeechCraft is an advanced real-time speech analytics platform that transforms spoken words into actionable insights. Using cutting-edge AI technology from AssemblyAI, it provides instant transcription while analyzing multiple dimensions of speech performance.

Read full post

Top comments (0)

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay