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 3】15 新增 API count_nonzero #5085

Merged
merged 4 commits into from Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/api/api_label
Expand Up @@ -138,6 +138,7 @@ paddle.concat .. _api_paddle_concat:
paddle.conj .. _api_paddle_conj:
paddle.cos .. _api_paddle_cos:
paddle.cosh .. _api_paddle_cosh:
paddle.count_nonzero .. _api_paddle_count_nonzero:
paddle.cross .. _api_paddle_cross:
paddle.cumsum .. _api_paddle_cumsum:
paddle.dist .. _api_paddle_dist:
Expand Down
1 change: 1 addition & 0 deletions docs/api/paddle/Overview_cn.rst
Expand Up @@ -50,6 +50,7 @@ tensor数学操作
" :ref:`paddle.conj <cn_api_tensor_conj>` ", "逐元素计算Tensor的共轭运算"
" :ref:`paddle.cos <cn_api_fluid_layers_cos>` ", "余弦函数"
" :ref:`paddle.cosh <cn_api_fluid_layers_cosh>` ", "双曲余弦函数"
" :ref:`paddle.count_nonzero <cn_api_tensor_cn_count_nonzero>` ", "沿给定的轴 axis 统计非零元素的数量"
" :ref:`paddle.cumsum <cn_api_tensor_cn_cumsum>` ", "沿给定 axis 计算张量 x 的累加和"
" :ref:`paddle.cumprod <cn_api_tensor_cn_cumprod>` ", "沿给定 dim 计算张量 x 的累乘"
" :ref:`paddle.digamma <cn_api_paddle_digamma>` ", "逐元素计算输入x的digamma函数值"
Expand Down
1 change: 1 addition & 0 deletions docs/api/paddle/Tensor/Overview_en.rst
Expand Up @@ -104,6 +104,7 @@ Methods
copy_
cos
cosh
count_nonzero
cpu
cross
cuda
Expand Down
9 changes: 9 additions & 0 deletions docs/api/paddle/Tensor_cn.rst
Expand Up @@ -626,6 +626,15 @@ cosh(name=None)
print(out)
# [1.08107237 1.02006674 1.00500417 1.04533851]

count_nonzero(axis=None, keepdim=False, name=None)
:::::::::

返回:沿给定的轴 ``axis`` 统计输入Tensor ``x`` 中非零元素的个数。

返回类型:Tensor

请参考 :ref:`cn_api_tensor_cn_count_nonzero`

cpu()
:::::::::

Expand Down
24 changes: 24 additions & 0 deletions docs/api/paddle/count_nonzero_cn.rst
@@ -0,0 +1,24 @@
.. _cn_api_tensor_cn_count_nonzero:

count_nonzero
-------------------------------

.. py:function:: paddle.count_nonzero(x, axis=None, keepdim=False, name=None)

沿给定的轴 ``axis`` 统计输入Tensor ``x`` 中非零元素的个数。

参数
::::::::::
- **x** (Tensor) - 输入的Tensor,数据类型为:bool、float16、float32、float64、int32、int64。
- **axis** (None|int|list|tuple,可选) - 指定对 ``x`` 进行计算的轴。``axis`` 可以是int或者int元素的列表。``axis`` 值应该在范围[-D, D)内,D是 ``x`` 的维度。如果 ``axis`` 或者其中的元素值小于0,则等价于 :math:`axis + D`。如果 ``axis`` 是None,则对 ``x`` 的全部元素计算中位数。默认值为None。
- **keepdim** (bool,可选) - 是否在输出Tensor中保留减小的维度。如果 ``keepdim`` 为True,则输出Tensor和 ``x`` 具有相同的维度(减少的维度除外,减少的维度的大小为1)。否则,输出Tensor的形状会在 ``axis`` 上进行squeeze操作。默认值为True。
- **name** (str,可选) - 具体用法请参见 :ref:`api_guide_Name`,一般无需设置,默认值为 None。


返回
::::::::::
``Tensor``,沿着 ``axis`` 统计输入Tensor中非零元素的个数,数据类型int64。

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