[英]I am getting runtime warnings when trying to normalize and update weights
我正在嘗試計算粒子過濾器中某些粒子的權重,然后相應地對這些權重進行歸一化。 我的代碼:
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
但是,我收到錯誤:/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中遇到的無效值
正如評論中所說,我在您的代碼中添加了括號,但可能還有另一件事。 我覺得您正在嘗試將權重與可能性相乘,然后對結果進行歸一化。 為此,您應該在 2 中切斷循環:
我會這樣寫:
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.