Created by: qili93
Required(必填, multiple choices, two at most)
-
PR type(PR 类型) is ( A ): A. New features(新功能)---------------- D. Performance optimization(性能优化) B. Bug fixes(问题修复)------------------ E. Breaking changes(向后不兼容的改变) C. Function optimization(功能优化)------F. Others(其它)
-
PR changes(改动点)is ( A ): A. OPs(operators)---------------------- C. Docs(文档) B. APIs(接口)--------------------------- D. Others(其它)
-
Use one sentence to describe what this PR does.(简述本次PR的目的和改动) Add new operator "paddle.histc"
Optional(选填, If None, please delete it)
Fluid Doc PR: https://github.com/PaddlePaddle/FluidDoc/pull/2147
Add histc op
Computes the histogram of a tensor. The elements are sorted into equal width bins between min and max. If min and max are both zero, the minimum and maximum values of the data are used.
Args:
input (Variable): A Tensor(or LoDTensor) with shape :math:[N_1, N_2,..., N_k]
. The data type of the input Tensor should be float32, float64, int32, int64.
bins (int): number of histogram bins
min (int): lower end of the range (inclusive)
max (int): upper end of the range (inclusive)
Returns:
Variable: Tensor or LoDTensor calculated by histc layer. The data type is int64.
Code Example 1:
import paddle
import numpy as np
startup_program = paddle.Program()
train_program = paddle.Program()
with paddle.program_guard(train_program, startup_program):
inputs = paddle.data(name='input', dtype='int32', shape=[2,3])
output = paddle.histc(inputs, bins=5, min=1, max=5)
place = paddle.CPUPlace()
exe = paddle.Executor(place)
exe.run(startup_program)
img = np.array([[2, 4, 2], [2, 5, 4]]).astype(np.int32)
res = exe.run(train_program,
feed={'input': img},
fetch_list=[output])
print(np.array(res[0])) # [0,3,0,2,1]
Code Example 2:
import paddle
import numpy as np
with paddle.imperative.guard(paddle.CPUPlace()):
inputs_np = np.array([1, 2, 1]).astype(np.float)
inputs = paddle.imperative.to_variable(inputs_np)
result = paddle.histc(inputs, bins=4, min=0, max=3)
print(result) # [0, 2, 1, 0]