Python – PyTorch abs() 方法
PyTorch torch.abs()
方法计算给定输入张量的元素绝对值。
Syntax: torch.abs(inp, out=None) ? Tensor
Arguments
- inp: This is input tensor.
- out: This is optional parameter and is the output tensor.
Return: It returns a Tensor having absolute value of input inp.
让我们通过几个例子来看看这个概念:
示例 1:
示例 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]
在评论中写代码?请使用 ide.geeksforgeeks.org,生成链接并在此处分享链接。