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.div(): Element-wise Division on Tensors

Home torch.div(): Element-wise Division on Tensors
torch.div() Method in PyTorch
  • Written by krunallathiya21
  • June 24, 2025
  • 0 Com
PyTorch

The torch.div() method performs element-wise division of tensors. It divides each element of the input tensor by a scalar or another tensor, returning a new tensor with the divided values.

Division by a scalar in PyTorch

Here is the basic formula: output_tensor = input_tensor / other_tensor

Syntax

torch.div(input, other, rounding_mode=None, out=None)

Parameters

Argument Description
input (Tensor) It is a numerator tensor.
other (Tensor or Scalar) It is the denominator, which can be either a tensor of compatible shape or a scalar.
rounding_mode (str, optional) It specifies the division rounding behaviour.
  1. None (default): It is a standard floating-point division.
  2. trunc: It rounds towards zero (truncation).
  3. floor: It rounds towards negative infinity (floor division).
out (Tensor) It is a pre-allocated tensor to store the result in.

Division by a scalar

Let’s define a 1D tensor and divide that tensor by a scalar value.

import torch

input = torch.tensor([22.0, 18.0, 10.0])

scalar = 2

division_tensor = torch.div(input, scalar)

print(division_tensor)

# Output: tensor([11.,  9.,  5.])

In this code, you can see that 22.0 is divided by 2, which equals 11. 18.0 is divided by 2, which is 9, and 10.0 is divided by 2, which is equal to 5.

Element-wise division of tensors

Element-wise division of tensors

Let’s divide a tensor of the same size by another tensor of the same size.

import torch

input = torch.tensor([10.0, 20.0, 30.0])

other = torch.tensor([1.0, 4.0, 5.0])

division_tensor = torch.div(input, other)

print(division_tensor)

# Output: tensor([10.,  5.,  6.])

Each element of the input tensor is divided by the corresponding element of the other tensor.

Rounding modes

Let’s explore different rounding modes and control the division behavior with rounding_mode.

import torch

input = torch.tensor([11.0, 23.0, 35.0])

other = torch.tensor([1.0, 4.0, 5.0])

division_tensor = torch.div(input, other)

print(division_tensor)
# Output: tensor([11.0000,  5.7500,  7.0000])

# Standard division
result = torch.div(input, other)
print(result)
# Output: tensor([11.0000,  5.7500,  7.0000])

# Truncated division
result_trunc = torch.div(input, other, rounding_mode="trunc")
print(result_trunc)
# Output: tensor([11.,  5.,  7.])

# Floor division
result_floor = torch.div(input, other, rounding_mode="floor")
print(result_floor)
# Output: tensor([11.,  5.,  7.])

In the above code, we demonstrated the element-wise division of two tensors using torch.div(). 

By default, it performs floating-point division, but with rounding_mode=’trunc’ or ‘floor’, it returns truncated or floor-rounded integer results. 

For positive inputs, trunc and floor give the same output, but they differ for negative values.

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