Paddle paddle不支持对布尔数组的求和

mklgxw1f  于 2021-11-30  发布在  Java
关注(0)|答案(1)|浏览(517)

paddlepaddle 2.0.1

执行命令:paddle.sum([True, False, True, False]) 会报错:
Traceback (most recent call last):
File "", line 1, in
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)
ValueError: (InvalidArgument) reduce_sum(): argument 'X' (position 0) must be Tensor, but got list (at /home/teamcity/work/ef54dc8a5b211854/paddle/fluid/pybind/op_function.h:56)

需求场景:
在分类训练时累计预测正确数,希望可以这样写:
acc = paddle.sum(paddle.argmax(preds, axis=1) == labels[:, 0])

a1o7rhls

a1o7rhls1#

可以使用 cast 或者 where op将bool型的数值转为数值型,参与后续运算

相关问题