我正在计算KL散度。但是即使求和后,我也对KL差异产生负值。代码或输入数据中是否有错误?如何解决此错误?
`P= np.array([0.54914578,0.74747151,0.88347895,
0.93624765,0.90605124,0.80999872,0.67410945,
0.52509581, 0.38434376, 0.26513069])
Q= np.array([1.89429187, 0., 1.89429187, 0. , 0.,
1.89429187, 0., 0., 0., 1.89429187])
KL_div =[]
for i in range(len(P)):
if P[i] != 0 and Q[i]!=0:
s= P[i]*np.log(P[i]/Q[i])
KL_div.append(s)
KL_div = np.array(KL_div)
Sum= np.sum(KL_div)
print("Sum: ",Sum)`