PyTorch .div()
The .div() function performs element-wise division between tensors or divides a tensor by a scalar value. It is a fundamental tensor operation in PyTorch, commonly used for performing machine learning and deep learning tasks such as data preprocessing, normalization, and optimization.
Element-wise operations are essential in tensor computations, enabling efficient parallel processing. The .div() function offers a simple and optimized way to handle division across tensors in neural networks and mathematical transformations.
Syntax
torch.div(input, other, *, rounding_mode=None, out=None)
Parameters:
input: The input tensor (dividend).other: The tensor or scalar to divide by (divisor).rounding_mode(Optional): Controls the rounding behavior. Can beNone(default),trunc, orfloor.out(Optional): The output tensor to store the result.
Return value:
A tensor with the result of element-wise division. If out is provided, the returned tensor is the same as out.
Example 1: Basic Usage of .div() with Tensors
This example demonstrates how to use .div() to perform element-wise division between two tensors of the same shape:
import torch# Create input tensorstensor1 = torch.tensor([4.0, 9.0, 16.0, 25.0])tensor2 = torch.tensor([2.0, 3.0, 4.0, 5.0])# Perform element-wise divisionresult = torch.div(tensor1, tensor2)# Print resultsprint("Tensor1 (dividend):", tensor1)print("Tensor2 (divisor):", tensor2)print("Division result:", result)
Here is the output:
Tensor1 (dividend): tensor([ 4., 9., 16., 25.])Tensor2 (divisor): tensor([2., 3., 4., 5.])Division result: tensor([2., 3., 4., 5.])
The .div() operation computes the element-wise division of tensor1 by tensor2. Each element in the resulting tensor is the quotient of the corresponding elements in the input tensors.
Example 2: Division with Rounding Modes
The .div() function supports optional rounding modes when performing integer division, which control how the result is rounded:
import torch# Create input tensorstensor1 = torch.tensor([5, 7, 10, 15])tensor2 = torch.tensor([2, 2, 3, 4])# Division with different rounding modesresult_default = torch.div(tensor1, tensor2)result_floor = torch.div(tensor1, tensor2, rounding_mode='floor')result_trunc = torch.div(tensor1, tensor2, rounding_mode='trunc')# Print resultsprint("Default division:", result_default)print("Floor division:", result_floor)print("Trunc division:", result_trunc)
Here is the output:
Default division: tensor([2.5000, 3.5000, 3.3333, 3.7500])Floor division: tensor([2, 3, 3, 3])Trunc division: tensor([2, 3, 3, 3])
The rounding modes control how the division results are handled:
- Default mode performs true division, returning floating-point results.
floormode rounds the result down toward negative infinity (i.e., floor division).truncmode truncates the decimal part, rounding toward zero.
Example 3: Data Normalization with .div()
The .div() function is commonly used in data preprocessing and normalization. Here’s an example of normalizing a dataset:
import torch# Create a sample dataset (3 samples with 4 features each)dataset = torch.tensor([[10.0, 20.0, 30.0, 40.0],[15.0, 25.0, 35.0, 45.0],[20.0, 30.0, 40.0, 50.0]])# Calculate the range (max - min) for each featurefeature_min = dataset.min(dim=0).valuesfeature_max = dataset.max(dim=0).valuesfeature_range = feature_max - feature_min# Normalize the dataset to [0, 1] range using divnormalized_data = torch.div(dataset - feature_min, feature_range)print("Original dataset:")print(dataset)print("\nNormalized dataset:")print(normalized_data)
Here is the output:
Original dataset:tensor([[10., 20., 30., 40.],[15., 25., 35., 45.],[20., 30., 40., 50.]])Normalized dataset:tensor([[0.0000, 0.0000, 0.0000, 0.0000],[0.5000, 0.5000, 0.5000, 0.5000],[1.0000, 1.0000, 1.0000, 1.0000]])
This example demonstrates how .div() can be used to normalize data to a specific range, which is a commonly performed preprocessing step in machine learning workflows.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn PyTorch on Codecademy
- Learn to build machine learning models with Python.
- Includes 10 Courses
- With Certificate
- Beginner Friendly.23 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours