Sprint Chase Technologies
  • Home
  • About
    • Why Choose Us
    • Contact Us
    • Team Members
    • Testimonials
  • Services
    • Web Development
    • Web Application Development
    • Mobile Application Development
    • Web Design
    • UI/UX Design
    • Social Media Marketing
    • Projects
  • Blog
    • PyTorch
    • Python
    • JavaScript
  • IT Institute
menu
close

Need Help? Talk to an Expert

+91 8000107255
Sprint Chase Technologies
  • Home
  • About
    • Why Choose Us
    • Contact Us
    • Team Members
    • Testimonials
  • Services
    • Web Development
    • Web Application Development
    • Mobile Application Development
    • Web Design
    • UI/UX Design
    • Social Media Marketing
    • Projects
  • Blog
    • PyTorch
    • Python
    • JavaScript
  • IT Institute

Need Help? Talk to an Expert

+91 8000107255

torch.fmod(): Element-wise Modulus in PyTorch

Home torch.fmod(): Element-wise Modulus in PyTorch
torch.fmod() Method in PyTorch
  • Written by krunallathiya21
  • June 24, 2025
  • 0 Com
PyTorch

The torch.fmod() method calculates the element-wise modulus (remainder) of dividing one tensor by another, following the C-style modulus operation. It returns a tensor containing the remainders of the division of each element in the dividend tensor by the corresponding element in the divisor tensor.

torch.fmod() method

The output tensor has the same shape as the input when the divisor is a tensor.

Syntax

torch.fmod(input, divisor, out=None)

Parameters

Argument Description
input (Tensor)

It represents the dividend tensor (the tensor to be divided).

division (Tensor or Scalar)

It represents the divisor tensor or a scalar value by which to divide the input.

out (Tensor, optional)

It is the output tensor to store the result.

Element-wise modulus with Scalar Divisor

Let’s calculate the remainder of each element in a tensor when divided by a scalar.

import torch

# Input tensor
input_tensor = torch.tensor([10, 7, -7, 0])

# Compute modulus with scalar divisor
remainder_tensor = torch.fmod(input_tensor, 3)

print(remainder_tensor)

# Output: tensor([ 1,  1, -1,  0])

Each element in input_tensor is divided by 3, and the remainder is computed.

For the value 10, 10/3 and the remainder is 1 because 3×3 = 9 and only 1 is the remainder to be 10.

For the value 7, 7/3, and the remainder is 1 because 2×3 = 6, and only 1 is the remainder to be 7.

For -7, the remainder is -1 because fmod preserves the dividend’s sign.

Element-Wise modulus with tensor divisor

Element-Wise modulus with tensor divisor

We can perform a modulus between two tensors of the same shape. And the output tensor has the same shape as the input.

import torch


input_tensor = torch.tensor([[10, -7], [8, 0]])
divisor_tensor = torch.tensor([[3, 2], [4, 5]])

result = torch.fmod(input_tensor, divisor_tensor)

print(result)
# Output: tensor([[ 1, -1],
#                 [ 0,  0]])

In the above code, each element in input_tensor is divided by the corresponding element in divisor_tensor. For example, -7 % 2 = -1, and 8 % 4 = 0 because there is no remainder in this case.

In-place operation with “out” Parameter

You can store the result in a pre-allocated tensor for memory efficiency.

import torch

input_tensor = torch.tensor([10, -7, 8])

divisor = 3

out_tensor = torch.empty_like(input_tensor)

torch.fmod(input_tensor, divisor, out=out_tensor)

print(out_tensor)
# Output: tensor([ 1, -1,  2])

That’s all!

Post Views: 2
LEAVE A COMMENT Cancel reply
Please Enter Your Comments *

krunallathiya21

All Categories
  • JavaScript
  • Python
  • PyTorch
site logo

Address:  TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

sprintchasetechnologies@gmail.com

(+91) 8000107255.

ABOUT US
  • About
  • Team Members
  • Testimonials
  • Contact

Copyright by @SprintChase  All Rights Reserved

  • PRIVACY
  • TERMS & CONDITIONS