CUQI-DTU / CUQIpy

https://cuqi-dtu.github.io/CUQIpy/
Apache License 2.0
42 stars 8 forks source link

Robust handling of initial_point #519

Open chaozg opened 6 days ago

chaozg commented 6 days ago

initial_point=1 gives error while initial_point=np.array([0.1]) works fine. Possibly related to #487 for Gibbs.

MWE to reproduce error:

import cuqi
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(100)

beta = cuqi.distribution.Beta(0.5, 0.5)
sampler = cuqi.experimental.mcmc.MALA(beta, initial_point=0.1, scale=0.1)
sampler.sample(10)
samples = sampler.get_samples()
samples.plot_trace()

Error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[15], [line 9](vscode-notebook-cell:?execution_count=15&line=9)
      [7](vscode-notebook-cell:?execution_count=15&line=7) sampler = cuqi.experimental.mcmc.MALA(beta, initial_point=0.1, scale=0.1)
      [8](vscode-notebook-cell:?execution_count=15&line=8) sampler.sample(10)
----> [9](vscode-notebook-cell:?execution_count=15&line=9) samples = sampler.get_samples()
     [10](vscode-notebook-cell:?execution_count=15&line=10) samples.plot_trace()

File ~/projects/CUQIpy/cuqi/experimental/mcmc/_sampler.py:155, in Sampler.get_samples(self)
    [153](https://file+.vscode-resource.vscode-cdn.net/Users/chaz/projects/CUQIpy/~/projects/CUQIpy/cuqi/experimental/mcmc/_sampler.py:153) def get_samples(self) -> Samples:
    [154](https://file+.vscode-resource.vscode-cdn.net/Users/chaz/projects/CUQIpy/~/projects/CUQIpy/cuqi/experimental/mcmc/_sampler.py:154)     """ Return the samples. The internal data-structure for the samples is a dynamic list so this creates a copy. """
--> [155](https://file+.vscode-resource.vscode-cdn.net/Users/chaz/projects/CUQIpy/~/projects/CUQIpy/cuqi/experimental/mcmc/_sampler.py:155)     return Samples(np.array(self._samples).T, self.target.geometry)

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (10,) + inhomogeneous part.
nabriis commented 6 days ago

I am unable to reproduce this in main just now. @chaozg

nabriis commented 6 days ago

We can focus on other areas for now.

nabriis commented 6 days ago

Can reproduce on the branch related to #518.

Running warmup first seems to work?

import cuqi
import matplotlib.pyplot as plt
import numpy as np
# np.random.seed(100)

beta = cuqi.distribution.Beta(0.5, 0.5)
sampler = cuqi.experimental.mcmc.MALA(beta, initial_point=0.1, scale=0.1)
sampler.warmup(20000)
sampler.sample(20000)
samples = sampler.get_samples()
samples.plot_trace()

I got this result: image