Saturday, March 14, 2026
HomeLanguagesPython – PyTorch frac() method

Python – PyTorch frac() method

PyTorch torch.frac() method computes the fractional portion of each element in input.

Syntax: torch.frac(input, out=None)

Arguments

  • input: This is input tensor.
  • 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([-5.4, 6.6, -7.1099, 4.4567])
print(a)
  
# Applying the frac function and 
# storing the result in 'out'
out = torch.frac(a)
print(out)


Output:

-5.4000
 6.6000
-7.1099
 4.4567
[torch.FloatTensor of size 4]

-0.4000
 0.6000
-0.1099
 0.4567
[torch.FloatTensor of size 4]

Example 2:




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


Output:

-0.5260
-1.8843
 0.8062
 1.2264
-0.1505
-0.3217
[torch.FloatTensor of size 6]

-0.5260
-0.8843
 0.8062
 0.2264
-0.1505
-0.3217
[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
32506 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6882 POSTS0 COMMENTS
Nicole Veronica
12005 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12099 POSTS0 COMMENTS
Shaida Kate Naidoo
7011 POSTS0 COMMENTS
Ted Musemwa
7255 POSTS0 COMMENTS
Thapelo Manthata
6967 POSTS0 COMMENTS
Umr Jansen
6956 POSTS0 COMMENTS