📜  Python – PyTorch trunc() 方法

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

Python – PyTorch trunc() 方法

PyTorch torch.trunc()方法在删除数字的小数部分后返回一个新的张量,其中包含 input/ 元素的截断整数值。

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

输出:

1.1257
 0.4493
-0.7309
 1.5523
-0.2877
 0.1155
[torch.FloatTensor of size 6]
 1
 0
-0
 1
-0
 0
[torch.FloatTensor of size 6]

示例 2:

# Importing the PyTorch library 
import torch 
    
# A constant tensor of size n
a = torch.FloatTensor([1.5, 3.9, -6.9, 3.678])
print(a)
  
# Applying the trunc function and 
# storing the result in 'out'
out = torch.trunc(a)
print(out)

输出:

1.5000
 3.9000
-6.9000
 3.6780
[torch.FloatTensor of size 4]
 1
 3
-6
 3
[torch.FloatTensor of size 4]