Paddle 建议paddle支持直接对布尔类型的tensor进行聚合运算

8gsdolmq  于 2021-11-30  发布在  Java
关注(0)|答案(2)|浏览(552)

版本:2.0.1
举例:
pytorch:
a1 = torch.tensor([1,2,3,4,5])
a2 = torch.tensor([1,2,3,2,1])
torch.sum(a1==a2)
Out[22]: tensor(3)

paddle:
b1 = paddle.to_tensor([1,2,3,4,5])
b2 = paddle.to_tensor([1,2,3,2,1])
paddle.sum(b1==b2)
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/IPython/core/interactiveshell.py", line 3417, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 1, in
paddle.sum(b1==b2)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/paddle/tensor/math.py", line 757, in sum
'reduce_all', reduce_all_flag)
RuntimeError: (NotFound) Operator reduce_sum does not have kernel for data_type[bool]:data_layout[ANY_LAYOUT]:place[CPUPlace]:library_type[PLAIN].
[Hint: Expected kernel_iter != kernels.end(), but received kernel_iter == kernels.end().] (at /home/teamcity/work/ef54dc8a5b211854/paddle/fluid/imperative/prepared_operator.cc:128)
[operator < reduce_sum > error]

这样处理才可以:
paddle.sum(paddle.cast(b1==b2, dtype='int64'))
Out[25]:
Tensor(shape=[1], dtype=int64, place=CPUPlace, stop_gradient=True,
[3])

建议:不用cast,像pytorch,numpy一样直接可对布尔数组进行sum、mean等聚合运算

g9icjywg

g9icjywg1#

您好,我们已经收到了您的问题,会安排技术人员尽快解答您的问题,请耐心等待。请您再次检查是否提供了清晰的问题描述、复现代码、环境&版本、报错信息等。同时,您也可以通过查看官网API文档常见问题历史IssueAI社区来寻求解答。祝您生活愉快~

Hi! We've received your issue and please be patient to get responded. We will arrange technicians to answer your questions as soon as possible. Please make sure that you have posted enough message to demo your request. You may also check out the APIFAQGithub Issue and AI community to get the answer.Have a nice day!

mjqavswn

mjqavswn2#

您好,这个问题其实普通api对数据类型提升(type promote)的支持。目前仅支持通用的加减乘除类api。您的问题,内部将进行反馈,后续进展将会及时告知您。

相关问题