ido90 / Optimized-Kalman-Filter

Get an optimized Kalman Filter from data of system-states and observations.
MIT License
33 stars 4 forks source link

The dimension of the OKF #2

Closed killerkw closed 1 year ago

killerkw commented 1 year ago

I am studying the code about OKF. In the example.ipynb(https://github.com/ido90/Optimized-Kalman-Filter/blob/master/example.ipynb), the dimension of x is 4 and the dimension of z is 2. Now I use my own dataset to train OKF. In my setup, the dimension of x is 2 and the dimension of z is 6. I would like to know how I should set it correctly in the original code, thanks!

ido90 commented 1 year ago

In the example that you refer to, lidar_model_args passes all the required arguments to okf.OKF(**lidar_model_args). You just need to replace these arguments with those of your problem. This includes dim_x, dim_z, but also the matrices F, H with the right dimensions (in your case F should be 2x2 and H 6x2), as demonstrated in the example in: https://github.com/ido90/Optimized-Kalman-Filter/blob/master/okf/example/simple_lidar_model.py#L15

killerkw commented 1 year ago

Hello!I modified the code according to the comments you provided and now it generates this error

I want to know if (https://github.com/ido90/Optimized-Kalman-Filter/blob/85311ccf4032d864226a97964fefabcf254dd169/okf/example/simple_lidar_model.py#L31)](https://github.com/ido90/Optimized-Kalman-Filter/blob/85311ccf4032d864226a97964fefabcf254dd169/okf/example/simple_lidar_model.py#L31) needs to be modified. If so, how to modify it.Thaks.

ido90 commented 1 year ago

simple_lidar_model.py is only used as an example, is only called from the example notebook (not from within the module), and doesn't need to be modified. Note that in the notebook, it's imported as LID, then used to define the arguments (lidar_model_args) that are passed to okf.OKF(). Instead, when you call okf.OKF() yourself, you should replace lidar_model_args with your own arguments according to the problem. In particular, if you specify some F of size 2x2, and x is of size 2, then the sizes match.

Can you please provide your call to the constructor okf.OKF(), and the arguments you pass to it? Also, the error msg that you specify is not entirely clear to me, can you please explicitly print the dimensions of F and self.x right before the failure?

killerkw commented 1 year ago

Hello.The call of okf.OKF and the argument is

lidar_model_args = dict(
    dim_x = 2,                                    
    dim_z = 6,                                    
    P0=1,
    init_z2x = LID.initial_observation_to_state,  
    F = LID.get_F(),                             
    H = LID.get_H(),                             
    loss_fun=LID.loss_fun(),                      
    model_files_path = 'models',                       
)

models = [
    okf.OKF(model_name='KF',  optimize=False, **lidar_model_args),
    okf.OKF(model_name='OKF', optimize=True,  **lidar_model_args),
]

And the dimensions of F and self.x is

F size is  torch.Size([2, 2])
x size is  torch.Size([6])
ido90 commented 1 year ago

See my responses above. You changed some of the arguments (dim_x, dim_z) but not the others (init_z2x, F, H, loss_fun). You must define all the args according to your problem. Documentation is available in the module and an example in the link I specified above.

killerkw commented 1 year ago

I change the arguments(F, H) in https://github.com/ido90/Optimized-Kalman-Filter/blob/master/okf/example/simple_lidar_model.py. so I can call them directly.

def get_F():
    # x,y,vx,vy
    return torch.tensor([
        [1, 0],
        [0, 1]
    ], dtype=torch.double)

def get_H():
    # x,y,vx,vy -> x,y
    return torch.tensor([
        [1,0],
        [0,1],
        [0,0],
        [0,0],
        [0,0],
        [0,0],

    ], dtype=torch.double)

but I don't konw whether to modify init_z2x. I think it should delete some dimensions from 6 to 2, but I have no idea how to modify it.

ido90 commented 1 year ago

I see. Init_z2x takes the first observation z and returns a state x. (It is used because usually, it is more convenient to initialize x directly from the first observation, rather than trying to come up with a prior on x. )

So init_z2x indeed must return an array of size dim_x. If your init_z2x somehow returns an array of size 6, this explains the error.