Saturday, October 18, 2025
HomeLanguagesPython – PyTorch div() method

Python – PyTorch div() method

PyTorch torch.div() method divides every element of the input with a constant and returns a new modified tensor.
Clamp method

Syntax: torch.div(inp, other, out=None)

Arguments

  • inp: This is input tensor.
  • other: This is a number to be divided to each element of input inp.
  • out: The output tensor.

Return: It returns a Tensor.

Let’s see this concept with the help of few examples:
Example 1:




# Importing the PyTorch library 
import torch 
    
# A constant tensor of size n
a = torch.FloatTensor([1, 4, 6, 8, 10, 14])
print(a)
  
# Applying the div function and 
# storing the result in 'out'
out = torch.div(a, 0.5)
print(out)


Output:

1
  4
  6
  8
 10
 14
[torch.FloatTensor of size 6]
  2
  8
 12
 16
 20
 28
[torch.FloatTensor of size 6]

Example 2:




# Importing the PyTorch library 
import torch 
    
# A constant tensor of size n
a = torch.randn(6)
print(a)
  
# Applying the div function and 
# storing the result in 'out'
out = torch.div(a, 0.3)
print(out)


Output:

 -0.8453
-0.1101
 0.9431
-0.3041
 1.4305
-0.0390
[torch.FloatTensor of size 6]
-2.8176
-0.3669
 3.1436
-1.0137
 4.7683
-0.1300
[torch.FloatTensor of size 6]
Last Updated :
26 May, 2020
Like Article
Save Article

<!–

–>

Similar Reads
Related Tutorials
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS