在pykalman上使用自动标签

2024-04-28 11:29:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在pykalman上使用autograd,我收到以下错误:

ValueError: setting an array element with a sequence.

如何解决此错误? (我知道我可以直接得到导数,这只是一个再现我错误的最小示例。)

代码:

from pykalman import KalmanFilter
import numpy as np
from autograd import grad

def model(measurements):

    initial_state_mean = [measurements[0, 0],
                          0,
                          measurements[0, 1],
                          0]

    transition_matrix = [[1, 1, 0, 0],
                         [0, 1, 0, 0],
                         [0, 0, 1, 1],
                         [0, 0, 0, 1]]

    observation_matrix = [[1, 0, 0, 0],
                          [0, 0, 1, 0]]

    kf = KalmanFilter(transition_matrices=transition_matrix,
                      observation_matrices=observation_matrix,
                      initial_state_mean=initial_state_mean)

    kf = kf.em(measurements, n_iter=5)
    df = grad(kf.smooth)
    (smoothed_state_means, smoothed_state_covariances) = kf.smooth(measurements)
    (d_smoothed_state_means, d_smoothed_state_covariances) = df(measurements)
    return smoothed_state_means, d_smoothed_state_means

measurements = np.asarray([(399,293),(403,299),(409,308),(416,315),(418,318),(420,323),(429,326),(423,328),(429,334),(431,337),(433,342),(434,352),(434,349),(433,350),(431,350),(430,349),(428,347),(427,345),(425,341),(429,338),(431,328),(410,313),(406,306),(402,299),(397,291),(391,294),(376,270),(372,272),(351,248),(336,244),(327,236),(307,220)],dtype=float)

res, dres = model(measurements)

Tags: fromimport错误meanmatrixinitialmeansstate