📜  Python – PyTorch abs() 方法

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

Python – PyTorch abs() 方法

PyTorch torch.abs()方法计算给定输入张量的元素绝对值。

让我们通过几个例子来看看这个概念:
示例 1:
# Importing the PyTorch library 
import torch 
    
# A constant tensor of size 1 
a = torch.FloatTensor([-15]) 
print(a) 
    
# Applying the abs function and 
# storing the result in 'b' 
b = torch.abs(a) 
print(b) 

输出:

-15
[torch.FloatTensor of size 1]
 15
[torch.FloatTensor of size 1]

示例 2:

# Importing the PyTorch library 
import torch 
    
# A constant tensor of size n 
a = torch.FloatTensor([15, -5, 3, -2]) 
print(a) 
    
# Applying the abs function and 
# storing the result in 'b' 
b = torch.abs(a) 
print(b) 

输出:

15
 -5
  3
 -2
[torch.FloatTensor of size 4]
 15
  5
  3
  2
[torch.FloatTensor of size 4]