简体   繁体   English

尝试规范化和更新权重时收到运行时警告

[英]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 中切断循环:

  • Correction of the weights and sum.校正权重和总和。
  • Normalization to sum up to one.归一化总和为一。

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.

相关问题 为什么加载重量时出现此错误? - Why am I getting this error when loading weights? 尝试更新表中的行时,为什么会从SQLAlchemy获得“期望的FROM表达式”? - Why am I getting “FROM expression expected” from SQLAlchemy when I'm trying to update a row in a table? 我正在学习如何通过重要性采样来拟合 python 上的数据,我正在尝试对 x 和 y 值进行采样并找到它们的权重,但我遇到了错误 - I am learning how to data fit on python with importance sampling, I am trying to sample x and y values and find their weights but I am getting errors 尝试标准化训练数据时获取元组 - Getting back a tuple when trying to normalize training data 当我尝试启动PySpark时获得空指针异常 - Getting a Null Pointer Exception when I am trying to start PySpark 当我尝试在Windows上安装LocalStack时遇到此错误 - Getting this error when I am trying to install LocalStack on windows 为什么我在尝试打印时遇到AttributeError - Why am I getting an AttributeError when trying to print out 尝试在终端中导入 docx 时为什么会收到此 ImportError? - Why am I getting this ImportError when trying to import docx in the Terminal? 为什么我在尝试打印此变量时得到 Nan 值? - Why am i getting Nan value when trying to print this variable? 当我尝试使用Django运行服务器时出现TypeError吗? - Getting TypeError when I am trying to run server using Django?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM