torch.copysign¶
- torch.copysign(input, other, *, out=None) Tensor ¶
创建一个具有
input
的幅度和other
的符号的新的浮点张量,逐元素。支持广播到公共形状,并支持整数和浮点数输入。
- 参数:
输入(张量)- 模长。
其他(张量或数字)- 包含值,其符号位应用于
input
中的模长。
- 关键字参数:
输出(张量,可选)- 输出张量。
示例:
>>> 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 处理有符号零。如果其他参数为负零(-0),则相应的输出值将为负。