Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

【PaddlePaddle Hackathon 2】18、为 Paddle 新增 paddle.heaviside 和 paddle.Tensor.heaviside API #4641

Merged
merged 10 commits into from May 18, 2022
1 change: 1 addition & 0 deletions docs/api/paddle/Overview_cn.rst
Expand Up @@ -62,6 +62,7 @@ tensor数学操作
" :ref:`paddle.floor_divide <cn_api_tensor_floor_divide>` ", "逐元素整除算子,输入 x 与输入 y 逐元素整除,并将各个位置的输出元素保存到返回结果中"
" :ref:`paddle.greater_equal <cn_api_tensor_cn_greater_equal>` ", "逐元素地返回 x>=y 的逻辑值"
" :ref:`paddle.greater_than <cn_api_tensor_cn_greater_than>` ", "逐元素地返回 x>y 的逻辑值"
" :ref:`paddle.heaviside <cn_api_tensor_heaviside>` ", "逐元素地对 x 计算由 y 中的对应元素决定的赫维赛德阶跃函数"
" :ref:`paddle.increment <cn_api_tensor_increment>` ", "在控制流程中用来让 x 的数值增加 value"
" :ref:`paddle.kron <cn_api_paddle_tensor_kron>` ", "计算两个张量的克罗内克积"
" :ref:`paddle.less_equal <cn_api_tensor_cn_less_equal>` ", "逐元素地返回 x<=y 的逻辑值"
Expand Down
36 changes: 25 additions & 11 deletions docs/api/paddle/Tensor_cn.rst
Expand Up @@ -639,9 +639,12 @@ cpu()
.. code-block:: python

import paddle
x = paddle.to_tensor(1.0, place=paddle.CUDAPlace(0))
print(x.place) # CUDAPlace(0)


if paddle.device.cuda.device_count() > 0:
x = paddle.to_tensor(1.0, place=paddle.CUDAPlace(0))
print(x.place) # CUDAPlace(0)

x = paddle.to_tensor(1.0)
y = x.cpu()
print(y.place) # CPUPlace

Expand Down Expand Up @@ -674,11 +677,12 @@ cuda(device_id=None, blocking=False)
x = paddle.to_tensor(1.0, place=paddle.CPUPlace())
print(x.place) # CPUPlace

y = x.cuda()
print(y.place) # CUDAPlace(0)
if paddle.device.cuda.device_count() > 0:
y = x.cuda()
print(y.place) # CUDAPlace(0)

y = x.cuda(1)
print(y.place) # CUDAPlace(1)
y = x.cuda(1)
print(y.place) # CUDAPlace(1)

cumsum(axis=None, dtype=None, name=None)
:::::::::
Expand Down Expand Up @@ -1144,6 +1148,14 @@ greater_than(y, name=None)

请参考 :ref:`cn_api_tensor_cn_greater_than`

heaviside(y, name=None)
:::::::::

返回:计算后的Tensor

返回类型:Tensor
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

返回类型需要写到返回里面

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我看别的全都是这么写的呀。那具体地改成什么呢?返回,Tensor,计算后的Tensor吗?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个文件比较特殊,可以先这样写


请参考 :ref:`cn_api_paddle_tensor_heaviside`

histogram(bins=100, min=0, max=0)
:::::::::
Expand Down Expand Up @@ -1632,11 +1644,13 @@ pin_memory(y, name=None)
.. code-block:: python

import paddle
x = paddle.to_tensor(1.0, place=paddle.CUDAPlace(0))
print(x.place) # CUDAPlace(0)

if paddle.device.cuda.device_count() > 0:
x = paddle.to_tensor(1.0, place=paddle.CUDAPlace(0))
print(x.place) # CUDAPlace(0)

y = x.pin_memory()
print(y.place) # CUDAPinnedPlace
y = x.pin_memory()
print(y.place) # CUDAPinnedPlace

pow(y, name=None)
:::::::::
Expand Down
37 changes: 37 additions & 0 deletions docs/api/paddle/heaviside_cn.rst
@@ -0,0 +1,37 @@
.. _cn_api_paddle_tensor_heaviside:

heaviside
-------------------------------

.. py:function:: paddle.heaviside(x, y, name=None)


逐元素地对 Tensor `x` 计算由 Tensor `y` 中的对应元素决定的赫维赛德阶跃函数,其计算公式为

.. math::
\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.

.. note::
``paddle.heaviside`` 遵守广播机制,如您想了解更多,请参见 :ref:`cn_user_guide_broadcasting`。
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

历史问题,描述有误,请 @TCChenlong 统一做修改:

  • 您 --> 你
  • 链接失效


参数
:::::::::
- **x** (Tensor)- 赫维赛德阶跃函数的输入 Tensor。数据类型为 float32、float64、int32 或 int64。
- **y** (Tensor)- 决定了一个赫维赛德阶跃函数的 Tensor。数据类型为 float32、float64、int32 或 int64。
- **name** (str,可选)- 操作的名称(可选,默认值为 None)。更多信息请参见 :ref:`api_guide_Name`。

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


代码示例
::::::::::
COPY-FROM: paddle.heaviside:heaviside-example