評價此頁

torch.sgn#

torch.sgn(input, *, out=None) Tensor#

此函式是 torch.sign() 在複數張量上的擴充套件。它計算一個新的張量,該張量中的元素具有與 input 中對應元素相同的角度,並且絕對值(即模)為 1(對於複數張量),對於非複數張量則等同於 torch.sign()。

outi={0inputi==0inputiinputiotherwise\text{out}_{i} = \begin{cases} 0 & |\text{{input}}_i| == 0 \\ \frac{{\text{{input}}_i}}{|{\text{{input}}_i}|} & \text{otherwise} \end{cases}
引數

input (Tensor) – 輸入張量。

關鍵字引數

out (Tensor, optional) – 輸出張量。

示例

>>> t = torch.tensor([3+4j, 7-24j, 0, 1+2j])
>>> t.sgn()
tensor([0.6000+0.8000j, 0.2800-0.9600j, 0.0000+0.0000j, 0.4472+0.8944j])