📜  Python – PyTorch fmod() 方法

📅  最后修改于: 2022-05-13 01:54:58.371000             🧑  作者: Mango

Python – PyTorch fmod() 方法

PyTorch torch.fmod()方法给出了除数的元素余数。除数可以是数字或张量。

让我们通过几个例子来看看这个概念:
示例 1:
# Importing the PyTorch library 
import torch 
    
# A constant tensor of size n
a = torch.FloatTensor([5, 6, 7, 4])
print(a)
  
# Applying the fmod function and 
# storing the result in 'out'
out = torch.fmod(a, 3)
print(out)

输出:

5
 6
 7
 4
[torch.FloatTensor of size 4]
 2
 0
 1
 1
[torch.FloatTensor of size 4]

示例 2:

# Importing the PyTorch library 
import torch 
    
# A constant tensor of size n
a = torch.FloatTensor([5, 6, 7, 4])
b = torch.FloatTensor([2, 3, 4, 1])
print(a, b)
  
# Applying the fmod function and 
# storing the result in 'out'
out = torch.fmod(a, b)
print(out)

输出:

5
 6
 7
 4
[torch.FloatTensor of size 4]
 2
 3
 4
 1
[torch.FloatTensor of size 4]

 1
 0
 3
 0
[torch.FloatTensor of size 4]