dstl / Stone-Soup

A software project to provide the target tracking community with a framework for the development and testing of tracking algorithms.
https://stonesoup.rtfd.io
MIT License
384 stars 126 forks source link

ValueError in `gm_reduce_single()` - mismatch in shape between delta_means matrix and weights #972

Closed Orchidaceae closed 3 months ago

Orchidaceae commented 3 months ago

The problem occurs at line 629 in the gm_reduce_single function when weights is multiplied by the resulting matrix from operation delta_means @ delta_means.T. Trying to reduce two gaussians: delta_means got shape (4, 2) and the resulting matrix from the matrix multiplication has shape (4, 4) which makes it not possible to multiply it with weights of shape (2,); generating the following error:

*** ValueError: operands could not be broadcast together with shapes (2,) (4,4)

My input arguments:

(Pdb) means.shape
(2, 4, 1)
(Pdb) covars.shape
(4, 4, 2)
(Pdb) weights.shape
(2,)
sdhiscocks commented 3 months ago

Hi @Orchidaceae. I think issue could be with shape of means input. It should be (num_dims, num_components) so in your example (4, 2).

Orchidaceae commented 3 months ago

@sdhiscocks I see what you mean, but I tried reshaping means to (4, 2) it looks like it worked but somehow I still get a mysterious RuntimeWarning when calculatingweights*delta_means@delta_means.T:

(Pdb) weights*delta_means@delta_means.T
/home/.venv/lib/python3.10/site-packages/stonesoup/types/array.py:54: RuntimeWarning: invalid value encountered in multiply
  result = super().__array_ufunc__(ufunc, method, *inputs, **kwargs)
StateVectors([[ 1.10242141e-01, -9.09809306e-31, -3.72657892e-27,
               -9.09809306e-31],
              [-9.09944114e-31,  1.92592994e-34,  7.88860905e-31,
                1.92592994e-34],
              [-3.72713109e-27,  7.88860905e-31,  3.23117427e-27,
                7.88860905e-31],
              [-9.09944114e-31,  1.92592994e-34,  7.88860905e-31,
                1.92592994e-34]])
(Pdb) weights
array([Probability(0.267590938254398), Probability(0.7324090617456019)],
      dtype=object)
(Pdb) delta_means
StateVectors([[ 5.49306796e-01, -2.00693204e-01],
              [-1.38777878e-17, -1.38777878e-17],
              [-5.68434189e-14, -5.68434189e-14],
              [-1.38777878e-17, -1.38777878e-17]])

and the shape of the operands still seem to be incompatible:

(Pdb) (delta_means@delta_means.T).shape
(4, 4)
(Pdb) weights.shape
(2,)

not sure how I should interpret this, does the operation work or not?

sdhiscocks commented 3 months ago

I'm not able to recreate the issue here. Execution order should be (weights*delta_means)@delta_means.T so shapes should be okay.

What version of Stone Soup and NumPy are you using? On what operating system?

Result I get is similar, except all the negative values you have, are positive with different value.

sdhiscocks commented 3 months ago

Test script

import numpy as np
from stonesoup.types.array import StateVectors
from stonesoup.types.numeric import Probability

weights = np.array(
    [Probability(0.267590938254398), Probability(0.7324090617456019)],
     dtype=object)

delta_means = StateVectors([
    [ 5.49306796e-01, -2.00693204e-01],
    [-1.38777878e-17, -1.38777878e-17],
    [-5.68434189e-14, -5.68434189e-14],
    [-1.38777878e-17, -1.38777878e-17]])

print(f'{weights=}')
print(f'{delta_means=}')
print(f'weights*delta_means@delta_means.T=\n{weights*delta_means@delta_means.T}')

Output

weights=array([Probability(0.267590938254398), Probability(0.7324090617456019)],
      dtype=object)
delta_means=StateVectors([[ 5.49306796e-01, -2.00693204e-01],
              [-1.38777878e-17, -1.38777878e-17],
              [-5.68434189e-14, -5.68434189e-14],
              [-1.38777878e-17, -1.38777878e-17]])
weights*delta_means@delta_means.T=
[[1.10242141e-01 4.29103278e-27 1.75760702e-23 4.29103278e-27]
 [4.29103239e-27 1.92592994e-34 7.88860905e-31 1.92592994e-34]
 [1.75760687e-23 7.88860905e-31 3.23117427e-27 7.88860905e-31]
 [4.29103239e-27 1.92592994e-34 7.88860905e-31 1.92592994e-34]]
Orchidaceae commented 3 months ago

Alright, I'm using Ubuntu 22.04, Stone-Soup 1.2 and numpy 1.26.2. I'm also a bit confused about all negative numbers. Here are my input values to the function:

(Pdb) means
array([[1.1800e+02, 1.1725e+02],
       [1.0000e-01, 1.0000e-01],
       [1.3600e+02, 1.3600e+02],
       [1.0000e-01, 1.0000e-01]])
(Pdb) covars
array([[[ 3.  ,  0.75],
        [ 0.  ,  0.  ],
        [ 0.  ,  0.  ],
        [ 0.  ,  0.  ]],

       [[ 0.  ,  0.  ],
        [10.  , 10.  ],
        [ 0.  ,  0.  ],
        [ 0.  ,  0.  ]],

       [[ 0.  ,  0.  ],
        [ 0.  ,  0.  ],
        [ 3.  ,  0.75],
        [ 0.  ,  0.  ]],

       [[ 0.  ,  0.  ],
        [ 0.  ,  0.  ],
        [ 0.  ,  0.  ],
        [10.  , 10.  ]]])
(Pdb) weights
array([0.26759094, 0.73240906])
Orchidaceae commented 3 months ago

Test script

import numpy as np
from stonesoup.types.array import StateVectors
from stonesoup.types.numeric import Probability

weights = np.array(
    [Probability(0.267590938254398), Probability(0.7324090617456019)],
     dtype=object)

delta_means = StateVectors([
    [ 5.49306796e-01, -2.00693204e-01],
    [-1.38777878e-17, -1.38777878e-17],
    [-5.68434189e-14, -5.68434189e-14],
    [-1.38777878e-17, -1.38777878e-17]])

print(f'{weights=}')
print(f'{delta_means=}')
print(f'weights*delta_means@delta_means.T=\n{weights*delta_means@delta_means.T}')

Output

weights=array([Probability(0.267590938254398), Probability(0.7324090617456019)],
      dtype=object)
delta_means=StateVectors([[ 5.49306796e-01, -2.00693204e-01],
              [-1.38777878e-17, -1.38777878e-17],
              [-5.68434189e-14, -5.68434189e-14],
              [-1.38777878e-17, -1.38777878e-17]])
weights*delta_means@delta_means.T=
[[1.10242141e-01 4.29103278e-27 1.75760702e-23 4.29103278e-27]
 [4.29103239e-27 1.92592994e-34 7.88860905e-31 1.92592994e-34]
 [1.75760687e-23 7.88860905e-31 3.23117427e-27 7.88860905e-31]
 [4.29103239e-27 1.92592994e-34 7.88860905e-31 1.92592994e-34]]

I tested this script and here is my output:

weights=array([Probability(0.267590938254398), Probability(0.7324090617456019)],
      dtype=object)
delta_means=StateVectors([[ 5.49306796e-01, -2.00693204e-01],
              [-1.38777878e-17, -1.38777878e-17],
              [-5.68434189e-14, -5.68434189e-14],
              [-1.38777878e-17, -1.38777878e-17]])
/home/.venv/lib/python3.10/site-packages/stonesoup/types/array.py:45: RuntimeWarning: invalid value encountered in multiply
  result = super().__array_ufunc__(ufunc, method, *inputs, **kwargs)
weights*delta_means@delta_means.T=
[[1.10242141e-01 4.29103272e-27 1.75760701e-23 4.29103272e-27]
 [4.29103254e-27 1.92592994e-34 7.88860905e-31 1.92592994e-34]
 [1.75760694e-23 7.88860905e-31 3.23117427e-27 7.88860905e-31]
 [4.29103254e-27 1.92592994e-34 7.88860905e-31 1.92592994e-34]]
Orchidaceae commented 3 months ago

I tested the script on my second Ubuntu 22.04 machine (Stone-Soup 1.2, Numpy 1.26.4) and got a the same warning:

weights=array([Probability(0.267590938254398), Probability(0.7324090617456019)],
      dtype=object)
delta_means=StateVectors([[ 5.49306796e-01, -2.00693204e-01],
              [-1.38777878e-17, -1.38777878e-17],
              [-5.68434189e-14, -5.68434189e-14],
              [-1.38777878e-17, -1.38777878e-17]])
/home/.venv/lib/python3.10/site-packages/stonesoup/types/array.py:45: RuntimeWarning: invalid value encountered in multiply
  result = super().__array_ufunc__(ufunc, method, *inputs, **kwargs)
weights*delta_means@delta_means.T=
[[1.10242141e-01 4.29103272e-27 1.75760701e-23 4.29103272e-27]
 [4.29103254e-27 1.92592994e-34 7.88860905e-31 1.92592994e-34]
 [1.75760694e-23 7.88860905e-31 3.23117427e-27 7.88860905e-31]
 [4.29103254e-27 1.92592994e-34 7.88860905e-31 1.92592994e-34]]

Then also tested the script on my Windows machine (Stone-Soup 1.2, Numpy 1.26.4) and the warning was gone:

weights=array([Probability(0.267590938254398), Probability(0.7324090617456019)],
      dtype=object)
delta_means=StateVectors([[ 5.49306796e-01, -2.00693204e-01],  
              [-1.38777878e-17, -1.38777878e-17],
              [-5.68434189e-14, -5.68434189e-14],
              [-1.38777878e-17, -1.38777878e-17]])
weights*delta_means@delta_means.T=
[[1.10242141e-01 4.29103272e-27 1.75760701e-23 4.29103272e-27] 
 [4.29103254e-27 1.92592994e-34 7.88860905e-31 1.92592994e-34] 
 [1.75760694e-23 7.88860905e-31 3.23117427e-27 7.88860905e-31] 
 [4.29103254e-27 1.92592994e-34 7.88860905e-31 1.92592994e-34]]
sdhiscocks commented 3 months ago

I'm not able to recreate the warning message (Windows or Linux), but looks like the results are the same. It's probably safe to ignore the warning message, as it may be where code is designed to safely handle certain errors, like underflows, but warning is being printed rather than suppressed in one of the numerical C libraries which could differ on OS and OS versions.

Orchidaceae commented 3 months ago

I see, thank you for looking into this.