快捷鍵

torch.copysign

torch.copysign(input, other, *, out=None) Tensor

建立一個新的浮點張量,其數值大小為 input,且元素的正負號為 other

outi={inputiif otheri0.0inputiif otheri0.0\text{out}_{i} = \begin{cases} -|\text{input}_{i}| & \text{if } \text{other}_{i} \leq -0.0 \\ |\text{input}_{i}| & \text{if } \text{other}_{i} \geq 0.0 \\ \end{cases}

支援廣播至通用形狀,以及整數和浮點數輸入。

參數
  • input (Tensor) – 數值大小。

  • other (TensorNumber) – 包含數值,其正負號位元(signbit(s))會套用到 input 中的數值大小。

關鍵字參數

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

範例

>>> a = torch.randn(5)
>>> a
tensor([-1.2557, -0.0026, -0.5387,  0.4740, -0.9244])
>>> torch.copysign(a, 1)
tensor([1.2557, 0.0026, 0.5387, 0.4740, 0.9244])
>>> a = torch.randn(4, 4)
>>> a
tensor([[ 0.7079,  0.2778, -1.0249,  0.5719],
        [-0.0059, -0.2600, -0.4475, -1.3948],
        [ 0.3667, -0.9567, -2.5757, -0.1751],
        [ 0.2046, -0.0742,  0.2998, -0.1054]])
>>> b = torch.randn(4)
tensor([ 0.2373,  0.3120,  0.3190, -1.1128])
>>> torch.copysign(a, b)
tensor([[ 0.7079,  0.2778,  1.0249, -0.5719],
        [ 0.0059,  0.2600,  0.4475, -1.3948],
        [ 0.3667,  0.9567,  2.5757, -0.1751],
        [ 0.2046,  0.0742,  0.2998, -0.1054]])
>>> a = torch.tensor([1.])
>>> b = torch.tensor([-0.])
>>> torch.copysign(a, b)
tensor([-1.])

注意

copysign 可以處理帶正負號的零。如果 other 參數具有負零 (-0),則對應的輸出值將為負數。

文件

取得 PyTorch 的完整開發人員文件

檢視文件

教學課程

取得適合初學者和進階開發人員的深入教學課程

檢視教學課程

資源

尋找開發資源並獲得問題解答

檢視資源