[英]I am getting runtime warnings when trying to normalize and update weights
I am trying to calculate the weights of certain particles in a particle filter and then normalize those weights accordingly.我正在尝试计算粒子过滤器中某些粒子的权重,然后相应地对这些权重进行归一化。 My code:
我的代码:
def update(particles, weights, landmark, sigma):
n = 0.0
for i in range(len(weights)):
distance = np.power((particles[i][0] - landmark[0]) ** 2 + (particles[i][1] -
landmark[1])**2, 0.5)
likelihood = exp(-(np.power(distance, 2))/2 * sigma ** 2)
weights[i] = weights[i] * likelihood
n += weights[i]
weights += 1.e-30
if n != 0:
weights = weights / n
However, I am getting the error: /Users/scottdayton/PycharmProjects/Uncertainty Research/particle.py:30: RuntimeWarning: overflow encountered in true_divide weights = weights / n /Users/scottdayton/PycharmProjects/Uncertainty Research/particle.py:30: RuntimeWarning: invalid value encountered in true_divide weights = weights / n但是,我收到错误:/Users/scottdayton/PycharmProjects/Uncertainty Research/particle.py:30: RuntimeWarning: 在 true_divide weights = weights = weights/n /Users/scottdayton/PycharmProjects/Uncertainty Research/particle.py:30 中遇到溢出: RuntimeWarning: true_divide weights = weights / n中遇到的无效值
As said in comments, I added parenthesis to your code but there might be another thing.正如评论中所说,我在您的代码中添加了括号,但可能还有另一件事。 I feel that you are trying to multiply weights with the likelihood and then normalize the result.
我觉得您正在尝试将权重与可能性相乘,然后对结果进行归一化。 To do so you should cut the loop in 2:
为此,您应该在 2 中切断循环:
I'll write it like this:我会这样写:
def update(particles, weights, landmark, sigma):
n = 0.0
# Correction of weights and computation of the sum
for i in range(len(weights)):
distance = np.power((particles[i][0] - landmark[0]) ** 2 + (particles[i][1] -
landmark[1])**2, 0.5)
likelihood = np.exp(-(np.power(distance, 2))/(2 * sigma ** 2))
weights[i] = weights[i] * likelihood + 1.e-30
n += weights[i]
# Normalization to sum up to one
for i in range(len(weights)):
weights[i] = weights[i] / n
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.