heaviside

paddle. heaviside ( x, y, name=None ) [源代码]

x 中的每个元素计算由 y 中相对应元素决定的赫维赛德阶跃函数,其计算公式为

\[\begin{split}\mathrm{heaviside}(x, y)= \left\{ \begin{array}{lcl} 0,& &\text{if } \ x < 0, \\ y,& &\text{if } \ x = 0, \\ 1,& &\text{if } \ x > 0. \end{array} \right.\end{split}\]

注解

paddle.heaviside 遵守广播机制,如您想了解更多,请参见 Tensor 介绍 .

参数

  • x (Tensor) - 赫维赛德阶跃函数的输入 Tensor。数据类型为 float16、float32、float64、int32 或 int64。

  • y (Tensor) - 决定了一个赫维赛德阶跃函数的 Tensor。数据类型为 float16、float32、float64、int32 或 int64。

  • name (str,可选) - 具体用法请参见 Name,一般无需设置,默认值为 None。

返回

Tensor,存储运算后的结果。如果 xy 有不同的形状且是可以广播的,那么返回 Tensor 的形状是 xy 经过广播后的形状。如果 xy 有相同的形状,那么返回 Tensor 的形状与 xy 相同。

代码示例

>>> import paddle
>>> x = paddle.to_tensor([-0.5, 0, 0.5])
>>> y = paddle.to_tensor([0.1])
>>> paddle.heaviside(x, y)
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
[0.        , 0.10000000, 1.        ])
>>> x = paddle.to_tensor([[-0.5, 0, 0.5], [-0.5, 0.5, 0]])
>>> y = paddle.to_tensor([0.1, 0.2, 0.3])
>>> paddle.heaviside(x, y)
Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0.        , 0.20000000, 1.        ],
 [0.        , 1.        , 0.30000001]])