我尝试在autograd
上使用pykalman
,但出现此错误:
ValueError:设置具有序列的数组元素。
如何解决此错误? (我知道我可以直接获得导数,这只是重现我的错误的一个最小示例。)
代码:
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)