A flexible state space model for learning nonlinear dynamical systems