Paddle的欧式距离计算存在误差。
Created by: BigFishMaster
我计算两个float32向量的距离时发现精度有问题,误差比numpy高一个数量级。两个向量是2048维的,且完全一样。距离公式采用的是a^2-2ab+b^2.(欧式公式展开) 结果:
paddle:0.00170898
numpy:-4.88281250e-04
paddle fluid的距离代码:其中feature是一个行向量[[x1,x2,x3...x2048]]
ab = fluid.layers.matmul(feature, feature, False, True)
a2 = fluid.layers.square(feature)
a2 = fluid.layers.reduce_sum(a2, dim = 1)
a2 = fluid.layers.reshape(a2, shape = [-1])
d = fluid.layers.elementwise_add(-2*ab, a2, axis = 0)
d = fluid.layers.elementwise_add(d, a2, axis = 1)