Paddle 【论文复现】python 数值常量与 bool 型 paddle.tensor 相加会报错,这与 numpy 和 pytorch 很不一样

ahy6op9u  于 2021-11-30  发布在  Java
关注(0)|答案(2)|浏览(259)
  • 版本、环境信息

1)PaddlePaddle版本:2.1.2
2)CPU/GPU:CPU
3)系统环境:Windows 10

  • 复现信息:paddle 处理如下简单的 python 数值常量与 bool 型 paddle.tensor 相加计算时会报错,这个算子报错就算了,这样的报错信息更是让上手 PaddlePaddle 的萌新雪上加霜:
In[88]:  t = 0
In[89]:  t += paddle.to_tensor([True])
Traceback (most recent call last):
  File "D:\Dev\Anaconda3-2019-10\envs\pytorch15-conda\lib\site-packages\IPython\core\interactiveshell.py", line 3343, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-109-d52ae30fe538>", line 1, in <module>
    t += paddle.to_tensor([True])
  File "D:\Dev\Anaconda3-2019-10\envs\pytorch15-conda\lib\site-packages\paddle\fluid\dygraph\math_op_patch.py", line 206, in __impl__
    return scalar_method(self, other_var)
  File "D:\Dev\Anaconda3-2019-10\envs\pytorch15-conda\lib\site-packages\paddle\fluid\dygraph\math_op_patch.py", line 160, in _scalar_add_
    return _scalar_elementwise_op_(var, 1.0, value)
  File "D:\Dev\Anaconda3-2019-10\envs\pytorch15-conda\lib\site-packages\paddle\fluid\dygraph\math_op_patch.py", line 109, in _scalar_elementwise_op_
    return core.ops.scale(var, 'scale', scale, 'bias', bias)
RuntimeError: (NotFound) Operator scale 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 C:\home\workspace\Paddle_release\paddle\fluid\imperative\prepared_operator.cc:135)
  [operator < scale > error]

然而,numpy 和 pytorch 对同样操作的处理则更加优雅,直接把 bool 类型的 arraytensor 当成 0/1 数值与之相加:

In[87]:  import numpy as np
In[88]:  t = 0
In[89]:  t += np.array([True])
In[90]:  t
Out[90]: array([1])

In[91]:  import torch
In[92]:  t = 0
In[93]:  t += torch.tensor([True])
In[94]:  t
Out[4]: tensor([1])
  • 建议描述:希望 paddle 处理 python 数值常量与 bool 型 paddle.tensor 相加计算时能得到跟 numpy 和 pytorch 一样的输出,这样肯定能降低大家的学习门槛,至少上手深度学习框架的没有没用过 numpy 的。
0sgqnhkj

0sgqnhkj1#

您好,我们已经收到了您的问题,会安排技术人员尽快解答您的问题,请耐心等待。请您再次检查是否提供了清晰的问题描述、复现代码、环境&版本、报错信息等。同时,您也可以通过查看官网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!

6ss1mwsb

6ss1mwsb2#

谢谢反馈,目前 paddle 内的类型提升不支持自动把 bool 类型提升成 int 类型。这是一个已知的 issue,我们正在讨论是否支持提升 Bool 到 int.

相关问题