📜  Python – PyTorch div() 方法

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

Python – PyTorch div() 方法

PyTorch torch.div()方法将输入的每个元素除以一个常数,并返回一个新的修改后的张量。
夹紧方式

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

输出:

1
  4
  6
  8
 10
 14
[torch.FloatTensor of size 6]
  2
  8
 12
 16
 20
 28
[torch.FloatTensor of size 6]

示例 2:

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

输出:

-0.8453
-0.1101
 0.9431
-0.3041
 1.4305
-0.0390
[torch.FloatTensor of size 6]
-2.8176
-0.3669
 3.1436
-1.0137
 4.7683
-0.1300
[torch.FloatTensor of size 6]