Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NaNs in gradients when fitting RSLDS #77

Closed
bantin opened this issue Dec 17, 2019 · 1 comment
Closed

NaNs in gradients when fitting RSLDS #77

bantin opened this issue Dec 17, 2019 · 1 comment

Comments

@bantin
Copy link
Collaborator

bantin commented Dec 17, 2019

I sometimes get errors (seems to be random based on the initialization) when trying to fit an RSLDS. The following lines reproduce the error:

import ssm
import autograd.numpy as np
np.random.seed(seed=123457)
true_lds = ssm.SLDS(15, 3, 10, transitions="rbf_recurrent", dynamics="gaussian", emissions="gaussian")
z, x, y = true_lds.sample(100)

new_lds = ssm.SLDS(15, 3, 10, transitions="rbf_recurrent", dynamics="gaussian", emissions="gaussian")
new_lds.fit(y, method="svi", variational_posterior="mf")

The traceback looks like this:

Traceback (most recent call last):
  File "/Users/bantin/Documents/Linderman-Shenoy/ssm/ssm/quick_test.py", line 8, in <module>
    new_lds.fit(y, method="svi", variational_posterior="mf")
  File "/Users/bantin/Documents/Linderman-Shenoy/ssm/ssm/util.py", line 108, in wrapper
    return f(self, datas, inputs=inputs, masks=masks, tags=tags, **kwargs)
  File "/Users/bantin/Documents/Linderman-Shenoy/ssm/ssm/lds.py", line 862, in fit
    elbos = _fitting_methods[method](posterior, datas, inputs, masks, tags, learning=True, **kwargs)
  File "/Users/bantin/Documents/Linderman-Shenoy/ssm/ssm/lds.py", line 430, in _fit_svi
    params, val, g, state = step(value_and_grad(_objective), params, itr, state)
  File "/Users/bantin/Documents/Linderman-Shenoy/ssm/ssm/optimizers.py", line 44, in _step
    step(_value_and_grad, _x, itr, state=state, *args, **kwargs)
  File "/Users/bantin/Documents/Linderman-Shenoy/ssm/ssm/optimizers.py", line 75, in adam_step
    val, g = value_and_grad(x, itr)
  File "/Users/bantin/Documents/Linderman-Shenoy/ssm/ssm/optimizers.py", line 41, in _value_and_grad
    v, g = value_and_grad(unflatten(x), i)
  File "/Users/Bantin/anaconda3/lib/python3.7/site-packages/autograd/wrap_util.py", line 20, in nary_f
    return unary_operator(unary_f, x, *nary_op_args, **nary_op_kwargs)
  File "/Users/Bantin/anaconda3/lib/python3.7/site-packages/autograd/differential_operators.py", line 140, in value_and_grad
    return ans, vjp(vspace(ans).ones())
  File "/Users/Bantin/anaconda3/lib/python3.7/site-packages/autograd/core.py", line 14, in vjp
    def vjp(g): return backward_pass(g, end_node)
  File "/Users/Bantin/anaconda3/lib/python3.7/site-packages/autograd/core.py", line 21, in backward_pass
    ingrads = node.vjp(outgrad[0])
  File "/Users/Bantin/anaconda3/lib/python3.7/site-packages/autograd/core.py", line 78, in <lambda>
    return lambda g: (vjp_0(g), vjp_1(g))
  File "/Users/Bantin/anaconda3/lib/python3.7/site-packages/autograd/scipy/linalg.py", line 29, in vjp
    v = al2d(solve_triangular(a, g, trans=_flip(a, trans), lower=lower))
  File "/Users/Bantin/anaconda3/lib/python3.7/site-packages/autograd/tracer.py", line 48, in f_wrapped
    return f_raw(*args, **kwargs)
  File "/Users/Bantin/anaconda3/lib/python3.7/site-packages/scipy/linalg/basic.py", line 336, in solve_triangular
    b1 = _asarray_validated(b, check_finite=check_finite)
  File "/Users/Bantin/anaconda3/lib/python3.7/site-packages/scipy/_lib/_util.py", line 239, in _asarray_validated
    a = toarray(a)
  File "/Users/Bantin/anaconda3/lib/python3.7/site-packages/numpy/lib/function_base.py", line 496, in asarray_chkfinite
    "array must not contain infs or NaNs")
ValueError: array must not contain infs or NaNs

I also get a message that LBFGS-B ran out of iteration before converging, during the initialization ARHMM fitting, which might be related.

bantin added a commit that referenced this issue Dec 18, 2019
Add a file constants.py to store constants. Add constant additions to np.log and division to avoid
logs of zero or division by zero. This fixes Issue #77
@bantin
Copy link
Collaborator Author

bantin commented Dec 18, 2019

Looks like this was caused by taking log of zero in messages.py:

alphas[0] = np.log(pi0) + log_likes[0]

This is addressed in 1927f6f

@bantin bantin closed this as completed Dec 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant