PyTorch 中的 torch.rsqrt() 方法


torch.rsqrt() 方法計算每個輸入張量元素的平方根倒數。它支援實值和復值輸入。如果輸入張量中的元素為零,則輸出張量中的對應元素為 NaN。

語法

torch.rsqrt(input)

引數

  • input – 輸入張量

輸出

返回平方根倒數張量。

步驟

  • 匯入所需的庫。在以下所有示例中,所需的 Python 庫為 torch。確保已安裝該庫。

import torch
  • 建立一個 torch 張量並列印它。

input = torch.randn(3,4)
print("Input Tensor:
", input)
  • 使用 torch.rsqrt(input) 計算輸入張量中每個元素的平方根倒數。其中 input 為輸入張量。

recip = torch.rsqrt(input)
  • 顯示具有倒數的計算張量。

print("Reciprocal SQRT Tensor:
", recip)

示例 1

在這個 Python 程式中,我們計算實值和復值輸入張量的平方根倒數。

# Import the required library
import torch

# define an input tensor
input = torch.tensor([1.2, 3., 4., 4.2, -3.2])

# print the above defined tensor
print("Input Tensor:
", input) # compute the reciprocal of the square root recip = torch.rsqrt(input) # print the above computed tensor print("Reciprocal SQRT Tensor:
", recip) print("............................") # define a complex input tensor input = torch.tensor([1.2+2j, 3.+4.j, 4.2-3.2j]) # print the above defined tensor print("Input Tensor:
", input) # compute the reciprocal of the square root recip = torch.rsqrt(input) # print the above computed tensor print("Reciprocal SQRT Tensor:
", recip)

輸出

Input Tensor:
   tensor([ 1.2000, 3.0000, 4.0000, 4.2000, -3.2000])
Reciprocal SQRT Tensor:
   tensor([0.9129, 0.5774, 0.5000, 0.4880, nan])
............................
Input Tensor:
   tensor([1.2000+2.0000j, 3.0000+4.0000j, 4.2000-3.2000j])
Reciprocal SQRT Tensor:
   tensor([0.5698-0.3226j, 0.4000-0.2000j, 0.4123+0.1392j])

請注意,輸入張量中對應於零的倒數平方根張量元素為 NaN。

示例 2

# Import the required library
import torch

# define an input tensor
input = torch.randn(3,4)

# print the above defined tensor
print("Input Tensor:
", input) # compute the reciprocal of the square root recip = torch.rsqrt(input) # print the above computed tensor print("Reciprocal SQRT Tensor:
", recip) print("......................................") # define a complex input tensor real = torch.randn(3,3) imag = torch.randn(3,3) input = torch.complex(real, imag) # print the above defined tensor print("Input Tensor:
", input) # compute the reciprocal of the square root recip = torch.rsqrt(input) # print the above computed tensor print("Reciprocal SQRT Tensor:
", recip)

輸出

Input Tensor:
   tensor([[ 7.4712e-01, -1.5884e+00, -9.7091e-01, -2.9538e-01],
      [ 2.0326e-01, 1.6650e+00, -3.1351e-01, 1.1758e-03],
      [ 1.6752e+00, 7.2334e-01, -7.4212e-01, 3.6498e-01]])
Reciprocal SQRT Tensor:
   tensor([[ 1.1569, nan, nan, nan],
      [ 2.2181, 0.7750, nan, 29.1634],
      [ 0.7726, 1.1758, nan, 1.6553]])
......................................
Input Tensor:
   tensor([[ 1.3595+0.1929j, -0.3348+0.0729j, 2.0567-1.1657j],
      [ 0.9777-1.4377j, -0.0728+0.7813j, 0.9582+1.3582j],
      [-0.5014+0.7377j, -0.5462-0.9864j, 1.1664-0.5318j]])
Reciprocal SQRT Tensor:
   tensor([[0.8513-0.0601j, 0.1827-1.6986j, 0.6289+0.1658j],
      [0.6703+0.3548j, 0.7603-0.8344j, 0.6886-0.3569j],
      [0.4954-0.9358j, 0.4782+0.8113j, 0.8631+0.1875j]])

更新時間: 2022-01-27

337 次瀏覽

開啟你的 事業

完成課程以獲得認證

開始
廣告