Git Product home page Git Product logo

recurrent-slds's Introduction

recurrent-slds

Recurrent Switching Linear Dynamical Systems (rSLDS), like the standard SLDS they are based upon, are models for decomposing nonlinear time-series data into discrete segments with relatively simple dynamics. The recurrent SLDS introduces an additional dependency between the discrete and continuous latent states, allowing the discrete state probability to depend upon the previous continuous state. These dependencies are highlighted in red in the graphical model below.

Probabilistic Model

These dependencies effectively cut up the continuous latent space into partitions with unique, linear dynamics. Composing these pieces gives rise to globally nonlinear dynamics. Here's an example of a 2D continuous latent state:

Probabilistic Model

In control literature, these models are known as hybrid systems. We develop efficient Bayesian inference algorithms for a class of recurrent SLDS. The important ingredient is an augmentation scheme to enable conjugate block Gibbs updates of the continuous latent states. Complete details of the algorithm are given in the following paper:

@inproceedings{linderman2017recurrent,
    title={Bayesian learning and inference in recurrent switching linear dynamical systems},
    author={Scott W. Linderman* and Johnson*, Matthew J. and Miller, Andrew C. and Adams, Ryan P. and Blei, David M. and Paninski, Liam},
    booktitle={Proceedings of the 20th International Conference on Artificial Intelligence and Statistics (AISTATS)},
    year={2017},
    link = {http://proceedings.mlr.press/v54/linderman17a/linderman17a.pdf},
}

Here's a link to the AISTATS paper.

Installation

This package is built upon many others, all of which are actively being developed. Follow the links to install these packages from source:

github.com/mattjj/pybasicbayes
github.com/mattjj/pyhsmm
github.com/mattjj/pylds
github.com/mattjj/pyslds
github.com/slinderman/pypolyagamma

Note that some of these have cython code which may need to be compiled. Follow the instructions on the project pages if you run into trouble.

Demo

Start with the NASCAR demo:

python examples/nascar.py

This will fit a recurrent switching linear dynamical system to synthetic NASCAR data, as in Figure 3 of the AISTATS paper.

recurrent-slds's People

Contributors

slinderman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

recurrent-slds's Issues

Installation dependencies

I finally got to trying to install and run rSLDS. THe following dependencies are ones that I don't think it made explicit, but that I had to figure out by installing things, seeing what fails, search the web for missing modules, etc. They are:

  • hips
  • HIPS-Lib
  • autoregressive
  • pyslds (needed upgrade to get _SLDSVBEMMixin)

autoregressive also needed imports of some things fixed to say to import them from autoregressive; and example is 'models'. In the end I had to update and rebuild numpy and scipy, and a number of other dependencies were downloaded and built more automatically. I thought you might want to know these things for your setup file, etc.

Value error when running the nascar example

I was just trying to run rSLDS on the NASCAR example by just running in a notebook cell
% run examples/nascar.py as instructed by the tutorial.

I then got this error:
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (4,) + inhomogeneous part.

with this being the whole error message:

`Setting seed to 0

ValueError Traceback (most recent call last)
File /mnt/cube/jugorman/rSLDS/recurrent-slds/examples/nascar.py:581
576 plt.show()
579 if name == "main":
580 # Simulate NASCAR data
--> 581 true_model, inputs, z_true, x_true, y, mask = simulate_nascar()
583 # Run PCA to get 2D dynamics
584 x_init, C_init = fit_pca(y)

File /mnt/cube/jugorman/rSLDS/recurrent-slds/examples/nascar.py:136, in simulate_nascar()
133 reg_b += mu_b[:,None]
135 # Make a recurrent SLDS with these params #
--> 136 dynamics_distns = [
137 Regression(
138 A=np.column_stack((A,b)),
139 sigma=1e-4 * np.eye(D_latent),
140 nu_0=D_latent + 2,
141 S_0=1e-4 * np.eye(D_latent),
142 M_0=np.zeros((D_latent, D_latent + 1)),
143 K_0=np.eye(D_latent + 1),
144 )
145 for A,b in zip(As, bs)]
147 init_dynamics_distns = [
148 Gaussian(
149 mu=np.array([0.0, 1.0]),
150 sigma=1e-3 * np.eye(D_latent))
151 for _ in range(K_true)]
153 C = np.hstack((npr.randn(args.D_obs, D_latent), np.zeros((args.D_obs, 1))))

File /mnt/cube/jugorman/rSLDS/recurrent-slds/examples/nascar.py:137, in (.0)
133 reg_b += mu_b[:,None]
135 # Make a recurrent SLDS with these params #
136 dynamics_distns = [
--> 137 Regression(
138 A=np.column_stack((A,b)),
139 sigma=1e-4 * np.eye(D_latent),
140 nu_0=D_latent + 2,
141 S_0=1e-4 * np.eye(D_latent),
142 M_0=np.zeros((D_latent, D_latent + 1)),
143 K_0=np.eye(D_latent + 1),
144 )
145 for A,b in zip(As, bs)]
147 init_dynamics_distns = [
148 Gaussian(
149 mu=np.array([0.0, 1.0]),
150 sigma=1e-3 * np.eye(D_latent))
151 for _ in range(K_true)]
153 C = np.hstack((npr.randn(args.D_obs, D_latent), np.zeros((args.D_obs, 1))))

File /mnt/cube/jugorman/envs/rSLDS/lib/python3.8/site-packages/pybasicbayes/distributions/regression.py:36, in Regression.init(self, nu_0, S_0, M_0, K_0, affine, A, sigma)
32 have_hypers = not any_none(nu_0,S_0,M_0,K_0)
34 if have_hypers:
35 self.natural_hypparam = self.mf_natural_hypparam =
---> 36 self._standard_to_natural(nu_0,S_0,M_0,K_0)
38 if A is sigma is None and have_hypers:
39 self.resample()

File /mnt/cube/jugorman/envs/rSLDS/lib/python3.8/site-packages/pybasicbayes/distributions/regression.py:85, in Regression._standard_to_natural(nu, S, M, K)
83 C = Kinv
84 d = nu
---> 85 return np.array([A,B,C,d])`

All I have done is just downloaded the required packages and tried to run the example as it currently is

Had a hard time installing it, but made it through...

hi,

For anyone trying to get rSLDS to work, it took a long time to install everything as these packages are not maintained.
Here's what I did, hoping it helps someone down the line:

*starting with new conda environment from scratch:

conda create -n slds_env pip python=3.5.4
conda activate slds_env
pip install numpy==1.13.3
#after doing git clone for all the recommended packages: pybasicbayes, pyhsmm, pypolyagamma, pylds, pyslds, ...
cd pybasicbayes/
pip install .
cd ..
pip install requests
conda install cython
cd pyhsmm
pip install .
cd ../pypolyagamma/
pip install .
cd ../pylds
pip install .
cd ../pyslds/
pip install .
#git clone autoregressive from mattjj/pyhsmm-autoregressive
cd ../pyhsmm-autoregressive/
#modify the setup.py doc in pyhsmm-autoregressive. The following two line need to be replaced:

#eigenurl = 'http://bitbucket.org/eigen/eigen/get/3.2.6.tar.gz'
eigenurl = 'https://gitlab.com/libeigen/eigen/-/archive/3.2.6/eigen-3.2.6.tar.gz'

#thedir = glob(join('deps', 'eigen-eigen-'))[0]
thedir = glob(join('deps', 'eigen-
'))[0]

pip install .
cd ../recurrent-slds/
pip install .
conda install tqdm
conda install scikit-learn
python examples/nascar.py

Can't find the Autoregressive module

Trying to run this but have not been able to download and install the Autoregressive module.
I have not been able to find where this module is on Github.

Running other (non-nascar) examples: prior fails

ihmm works and makes some plots.
prior fails with:
Traceback (most recent call last):
File "prior.py", line 432, in
true_model = simulate_prior()
File "prior.py", line 405, in simulate_prior
alpha=3.)
File "/usr/lib/python3.6/site-packages/rslds/models.py", line 232, in init
D_in=dynamics_distns[0].D_out, **kwargs)
TypeError: init() got multiple values for keyword argument 'D_in'
softmax-nascar fails as for nascar (filed as a separate issue)

nascar.py fails

After what appears to be a successful install, python3 nascar.py produces this:
True W_markov:
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
True W_input:
[[ 100. 0.]
[-100. 0.]
[ 0. 100.]]
Fitting PCA
Fitting Sticky ARHMM
......................... [ 25/1000, 0.01sec avg, ETA 13.09 ]
......................... [ 50/1000, 0.01sec avg, ETA 12.89 ]
......................... [ 75/1000, 0.01sec avg, ETA 12.51 ]
......................... [ 100/1000, 0.01sec avg, ETA 12.10 ]
......................... [ 125/1000, 0.01sec avg, ETA 11.80 ]
......................... [ 150/1000, 0.01sec avg, ETA 11.46 ]
......................... [ 175/1000, 0.01sec avg, ETA 11.13 ]
......................... [ 200/1000, 0.01sec avg, ETA 10.83 ]
......................... [ 225/1000, 0.01sec avg, ETA 10.51 ]
......................... [ 250/1000, 0.01sec avg, ETA 10.19 ]
......................... [ 275/1000, 0.01sec avg, ETA 9.86 ]
......................... [ 300/1000, 0.01sec avg, ETA 9.61 ]
......................... [ 325/1000, 0.01sec avg, ETA 9.36 ]
......................... [ 350/1000, 0.01sec avg, ETA 9.13 ]
......................... [ 375/1000, 0.01sec avg, ETA 8.80 ]
......................... [ 400/1000, 0.01sec avg, ETA 8.51 ]
......................... [ 425/1000, 0.01sec avg, ETA 8.12 ]
......................... [ 450/1000, 0.01sec avg, ETA 7.74 ]
......................... [ 475/1000, 0.01sec avg, ETA 7.38 ]
......................... [ 500/1000, 0.01sec avg, ETA 7.01 ]
......................... [ 525/1000, 0.01sec avg, ETA 6.64 ]
......................... [ 550/1000, 0.01sec avg, ETA 6.30 ]
......................... [ 575/1000, 0.01sec avg, ETA 5.93 ]
......................... [ 600/1000, 0.01sec avg, ETA 5.59 ]
......................... [ 625/1000, 0.01sec avg, ETA 5.24 ]
......................... [ 650/1000, 0.01sec avg, ETA 4.88 ]
......................... [ 675/1000, 0.01sec avg, ETA 4.53 ]
......................... [ 700/1000, 0.01sec avg, ETA 4.18 ]
......................... [ 725/1000, 0.01sec avg, ETA 3.82 ]
......................... [ 750/1000, 0.01sec avg, ETA 3.47 ]
......................... [ 775/1000, 0.01sec avg, ETA 3.12 ]
......................... [ 800/1000, 0.01sec avg, ETA 2.77 ]
......................... [ 825/1000, 0.01sec avg, ETA 2.42 ]
......................... [ 850/1000, 0.01sec avg, ETA 2.08 ]
......................... [ 875/1000, 0.01sec avg, ETA 1.73 ]
......................... [ 900/1000, 0.01sec avg, ETA 1.38 ]
......................... [ 925/1000, 0.01sec avg, ETA 1.04 ]
......................... [ 950/1000, 0.01sec avg, ETA 0.69 ]
......................... [ 975/1000, 0.01sec avg, ETA 0.34 ]
......................... [ 1000/1000, 0.01sec avg, ETA 0.00 ]

0.01sec avg, 13.77 total

Fitting Decision List
Level 0 Best k: 1
Level 1 Best k: 3
Level 2 Best k: 0
Fitting standard SLDS
Initializing dynamics with Gibbs sampling
......................... [ 25/100, 0.01sec avg, ETA 0.57 ]
......................... [ 50/100, 0.01sec avg, ETA 0.39 ]
......................... [ 75/100, 0.01sec avg, ETA 0.19 ]
......................... [ 100/100, 0.01sec avg, ETA 0.00 ]

0.01sec avg, 0.78 total

Traceback (most recent call last):
File "nascar.py", line 915, in
fit_slds(inputs, z_perm, x_init, y, mask, C_init, N_iters=N_samples)
File "nascar.py", line 679, in fit_slds
slds.resample_model()
File "/usr/lib/python3.6/site-packages/pyhsmm/models.py", line 442, in resample_model
self.resample_states(num_procs=num_procs)
File "/usr/lib/python3.6/site-packages/pyhsmm/models.py", line 467, in resample_states
s.resample()
File "/usr/lib/python3.6/site-packages/pyslds/states.py", line 448, in resample
self.resample_gaussian_states()
File "/usr/lib/python3.6/site-packages/pyslds/states.py", line 459, in resample_gaussian_states
info_sample(*self.info_params)
File "/usr/lib/python3.6/site-packages/pylds/lds_messages_interface.py", line 65, in wrapped
return func(*check(*args,**kwargs))
File "/usr/lib/python3.6/site-packages/pylds/lds_messages_interface.py", line 95, in _info_argcheck
[J_pair_11, J_pair_21, J_pair_22, J_node])
File "/usr/lib/python3.6/site-packages/pylds/lds_messages_interface.py", line 24, in _ensure_ndim
assert X.shape[0] == T
AssertionError

ImportError: No module named pylds.lds_messages_interface

Hi,
I'm trying to run the nascar.py example to understand how the model works but this error keeps showing:
Traceback (most recent call last):
File "nascar.py", line 23, in
from rslds.decision_list import DecisionList
ImportError: No module named rslds.decision_list

I've installed pybasicbayes, pyhsmm, pylds, pyslds and pypolyagamma. The installation didn't show any error.

error in resample_gaussian_states when running nascar.py

Hi,
I'm trying to run nascar.py and get the following error! I've installed all packages from their git pages. I'd appreciate any help! Here is the error log:

Traceback (most recent call last):
File "nascar.py", line 618, in
fit_slds(inputs, z_perm, x_init, y, mask, C_init)
File "nascar.py", line 331, in fit_slds
slds.resample_model()
File "/pyhsmm/pyhsmm/models.py", line 442, in resample_model
self.resample_states(num_procs=num_procs)
File "/pyhsmm/pyhsmm/models.py", line 467, in resample_states
s.resample()
File "/recurrent-slds/rslds/states.py", line 175, in resample
super(PGRecurrentSLDSStates, self).resample(niter=niter)
File "/pyslds/pyslds/states.py", line 1262, in resample
self.resample_gaussian_states()
File "/recurrent-slds/rslds/states.py", line 179, in resample_gaussian_states
super(PGRecurrentSLDSStates, self).resample_gaussian_states()
File "/pyslds/pyslds/states.py", line 429, in resample_gaussian_states
info_sample(*self.info_params)
File "/lib/python3.8/site-packages/pylds/lds_messages_interface.py", line 65, in wrapped
return func(*check(*args,**kwargs))
File "/lib/python3.8/site-packages/pylds/lds_messages_interface.py", line 93, in _info_argcheck
J_pair_11, J_pair_21, J_pair_22, J_node =
File "/lib/python3.8/site-packages/pylds/lds_messages_interface.py", line 24, in _ensure_ndim
assert X.shape[0] == T
AssertionError

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.