{'fraction_non_alphanumeric': 0.03649930162464162, 'fraction_numerical': 0.009630228626038373, 'mean_word_length': 4.911994784876141, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Neural Adaptive Sequential Monte Carlo

Sequential Monte Carlo (SMC), or particle filtering, is a popular class of methods for sampling from an intractable target distribution using a sequence of simpler intermediate distributions. Like other importance sampling-based methods, performance is critically dependent on the proposal distribution: a bad proposal can lead to arbitrarily inaccurate estimates of the target distribution. This paper presents a new method for automatically adapting the proposal using an approximation of the Kullback-Leibler divergence between the true posterior and the proposal distribution. The method is very flexible, applicable to any parameterised proposal distribution and it supports online and batch variants. We use the new framework to adapt powerful proposal distributions with rich parameterisations based upon neural networks leading to Neural Adaptive Sequential Monte Carlo (NASMC). Experiments indicate that NASMC significantly improves inference in a non-linear state space model outperforming adaptive proposal methods including the Extended Kalman and Unscented Particle Filters. Experiments also indicate that improved inference translates into improved parameter learning when NASMC is used as a subroutine of Particle Marginal Metropolis Hastings. Finally we show that NASMC is able to train a neural network-based deep recurrent generative model achieving results that compete with the state-of-the-art for polymorphic music modelling. NASMC can be seen as bridging the gap between adaptive SMC methods and the recent work in scalable, black-box variational inference.


Introduction
Sequential Monte Carlo (SMC) is a class of algorithms that draw samples from a target distribution of interest by generating samples from a series of simpler intermediate distributions. More specifically, the sequence constructs a proposal for importance sampling (IS) [1,2]. SMC is particularly well-suited for performing inference in non-linear dynamical models with hidden variables, since filtering naturally decomposes into a sequence, and in many such cases it is the state-of-the-art inference method [2,3]. Generally speaking, inference methods can be used as modules in systems for parameter learning. SMC has been used in such a way for both approximate maximum-likelihood parameter learning [4] and in Bayesian approaches such as the recently developed Particle MCMC methods [3].
Critically, in common with any importance sampling method, the performance of SMC is strongly dependent on the choice of the proposal distribution. If the proposal is not well-matched to the target distribution, then the method can produce samples that have low effective sample size and this leads to Monte Carlo estimates that have pathologically high variance [1]. The SMC community has developed approaches to mitigate these limitations such as resampling to improve particle diversity when the effective sample size is low [1] and applying MCMC transition kernels to improve particle diversity [5,2,3]. A complementary line of research leverages distributional approximate inference methods, such as the extended Kalman Filter and Unscented Kalman Filter, to construct better proposals, leading to the Extended Kalman Particle Filter (EKPF) and Unscented Particle Filter (UPF) [5]. In general, however, the construction of good proposal distributions is still an open question that severely limits the applicability of SMC methods.
This paper proposes a new gradient-based black-box adaptive SMC method that automatically tunes flexible proposal distributions. The quality of a proposal distribution can be assessed using the (intractable) Kullback-Leibler (KL) divergence between the target distribution and the parametrized proposal distribution. We approximate the derivatives of this objective using samples derived from SMC. The framework is very general and tractably handles complex parametric proposal distributions. For example, here we use neural networks to carry out the parameterisation thereby leveraging the large literature and efficient computational tools developed by this community. We demonstrate that the method can efficiently learn good proposal distributions that significantly outperform existing adaptive proposal methods including the EKPF and UPF on standard benchmark models used in the particle filter community. We show that improved performance of the SMC algorithm translates into improved mixing of the Particle Marginal Metropolis-Hasting (PMMH) [3]. Finally, we show that the method allows higher-dimensional and more complicated models to be accurately handled using SMC, such as those parametrized using neural networks (NN), that are challenging for traditional particle filtering methods .
The focus of this work is on improving SMC, but many of the ideas are inspired by the burgeoning literature on approximate inference for unsupervised neural network models. These connections are explored in section 6.

Sequential Monte Carlo
We begin by briefly reviewing two fundamental SMC algorithms, sequential importance sampling (SIS) and sequential importance resampling (SIR). Consider a probabilistic model comprising (possibly multi-dimensional) hidden and observed states z 1:T and x 1:T respectively, whose joint distribution factorises as: This general form subsumes common state-space models, such as Hidden Markov Models (HMMs), as well as non-Markovian models for the hidden state, such as Gaussian processes. To lighten the notation we temporarily suppress the parameter dependence p θ (·) = p(·) whilst focussing on hidden state estimation.
The goal of the sequential importance sampler is to approximate the posterior distribution over the hidden state sequence, p(z 1: 1:T ), through a weighted set of N sampled trajectories drawn from a simpler proposal distribution {z (n) 1:T } n=1:N ∼ q(z 1:T |x 1:T ). Any form of proposal distribution can be used in principle, but a particularly convenient one takes the same factorisation as the true posterior q(z 1:T |x 1:T ) = q(z 1 |x 1 ) T t=2 q(z t |z 1:t−1 , x 1:t ), with filtering dependence on x. A short derivation (see supplementary material) then shows that the normalised importance weights are defined by a recursion: SIS is elegant as the samples and weights can be computed in sequential fashion using a single forward pass. However, naïve implementation suffers from a severe pathology: the distribution of importance weights often become highly skewed as t increases, with many samples attaining very low weight. To alleviate the problem, the Sequential Importance Resampling (SIR) algorithm [1] adds an additional step that resamples z (n) t at time t from a multinomial distribution given byw(z (n) 1:t ) and gives the new particles equal weight. 1 This replaces degenerated particles that have low weight with samples that have more substantial importance weights without violating the validity of the method. SIR requires knowledge of the full trajectory of previous samples at each stage to draw the samples and compute the importance weights. For this reason, when carrying out resampling, each new particle needs to update its ancestry information. Letting a (n) τ,t represent the ancestral index of particle n at time t for state z τ , where 1 ≤ τ ≤ t, and collecting these into the set A }. Finally, to lighten notation, we use the shorthand w (n) t = w(z (n) 1:t ) for the weights. Note that, when employing resampling, these do not depend on the previous weights w (n) t−1 since resampling has given the previous particles uniform weight. The implementation of SMC is given by Algorithm 1 in the supplementary material.

The Critical Role of Proposal Distributions in Sequential Monte Carlo
The choice of the proposal distribution in SMC is critical. Even when employing the resampling step, a poor proposal distribution will produce trajectories that, when traced backwards, quickly collapse onto a single ancestor. Clearly this represents a poor approximation to the true posterior p(z 1:T |x 1:T ). These effects can be mitigated by increasing the number of particles and/or applying more complex additional MCMC moves than resampling [5,2], but these strategies increase the computational cost.
The conclusion is that the proposal should be chosen with care. The optimal choice for an unconstrained proposal that has access to all of the observed data at all times is the intractable posterior distribution q φ (z 1:T |x 1:T ) = p θ (z 1:T |x 1:T ). Given the restrictions imposed by the factorisation, this becomes q(z t |z 1:t−1 , x 1:t ) = p(z t |z 1:t−1 , x 1:t ), which is still typically intractable. The bootstrap filter instead uses the prior q(z t |z 1:t−1 , x 1:t ) = p(z t |z 1:t−1 , x 1:t−1 ) which is often tractable, but fails to incorporate information from the current observation x t . A halfway-house employs distributional approximate inference techniques to approximate p(z t |z 1:t−1 , x 1:t ). Examples include the EKPF and UPF [5]. However, these methods suffer from three main problems. First, the extended and unscented Kalman Filter from which these methods are derived are known to be inaccurate and poorly behaved for many problems outside of the SMC setting [6]. Second, these approximations must be applied on a sample by sample basis, leading to significant additional computational overhead. Third, neither approximation is tuned using an SMC-relevant criterion. In the next section we introduce a new method for adapting the proposal distribution that addresses these three limitations.

Adapting Proposals by Descending the Inclusive KL Divergence
In this work the quality of the proposal distribution will be optimised using the inclusive KL-divergence between the true posterior distribution and the proposal, KL[p θ (z 1:T |x 1:T )||q φ (z 1:T |x 1:T )]. (Parameters are now made explicit since we will shortly be interested in adapting both the proposal φ and the model θ.) This objective is chosen for four main reasons. First, this is a direct measure of the quality of the proposal, unlike those typically used such as effective sample size. Second, if the true posterior lies in the class of distributions attainable by the proposal family then the objective has a global optimum at this point. Third, if the true posterior does not lie within this class, then this KL divergence tends to find proposal distributions that have higher entropy than the original which is advantageous for importance sampling (the exclusive KL would be unsuitable for this reason [7]). Fourth, the derivative of the objective can be approximated efficiently using a sample based approximation that will now be described.
The gradient of the negative KL divergence with respect to the parameters of the proposal distribution takes a simple form, The expectation over the posterior can be approximated using samples from SMC. One option would use the weighted sample trajectories at the final time-step of SMC, but although unbiased such an estimator would have high variance due to the collapse of the trajectories. An alternative, that reduces variance at the cost of introducing some bias, uses the intermediate ancestral trees i.e. a filtering approximation (see the supplementary material for details), The simplicity of the proposed approach brings with it several advantages and opportunities. Online and batch variants. Since the derivatives distribute over time, it is trivial to apply this update in an online way e.g. updating the proposal distribution every time-step. Alternatively, when learning parameters in a batch setting for example, it might be more appropriate to update the proposal parameters after making a full forward pass of SMC. Conveniently, when performing approximate maximum-likelihood learning of the model p θ , the gradient update for the model parameters θ can be efficiently approximated using the same sample particles from SMC (see the supplementary material for derivation and Algorithm 1 for usage). Similar derivation for maximum likelihood learning is also discussed in [4].
Algorithm 1 Stochastic Gradient Proposal Adaption (batch inference and learning variants) Efficiency of the adaptive proposal. In contrast to the EPF and UPF, the new method employs an analytic function for propagation and does not require costly particle-specific distributional approximation as an inner-loop. Similarly, although the method bears similarity to the assumed-density filter (ADF) [8] which minimises a (local) inclusive KL, the new method has the advantage of minimising a global cost and does not require particle-specific moment matching.
Training complex proposal models. The adaptation method described above can be applied to any parametric proposal distribution. Special cases have been previously treated by [9]. In the next section, we describe a rich family of proposal distributions, that go beyond previous work, based upon neural networks. This approach enables adaptive SMC methods to make use of the rich literature and optimisation tools available from supervised learning.
Flexibility of training. One option is to train the proposal distribution using samples from SMC derived from the observed data. However, this is not the only approach. For example, the proposal could be trained using data sampled from the generative model instead, which might mitigate overfitting effects for small datasets. Similarly, the trained proposal does not need to be the one used to generate the SMC samples in the first place -the bootstrap filter or more complex variants can be used.

Flexible and Trainable Proposal Distributions Using Neural Networks
The proposed adaption method can be applied to any parametric proposal distribution. Here we briefly describe how to utilise this flexibility to employ powerful neural network-based parameterisations that have recently shown excellent performance in supervised sequence learning tasks [10,11]. Generally speaking, applications of these techniques to unsupervised sequence modeling settings is an active research area that is still in its infancy [12] and this work opens a new avenue in this wider research effort.
In a nutshell, the goal is to parameterise q φ (z t |z 1:t−1 , x 1:t ) -the proposal's stochastic mapping from all previous hidden states z 1:t−1 and all observations (up to and including the current observation) x 1:t , to the current hidden state, z t -in a flexible, computationally efficient and trainable way. Here we make use of a class of functions called Long Short-Term Memory (LSTM) that define a deterministic mapping from an input sequence to an output sequence using parameter-efficient recurrent dynamics, and alleviate the common vanishing gradient problem in recurrent neural networks [13,10,11]. The distributions q φ (z t |h t ) can be a mixture of Gaussians (a mixture density network (MDN) [14]) in which the mixing proportions, means and covariances are parameterised through another neural network (see the supplementary for details on LSTM, MDN, and neural network architectures).

Experiments
The goal of the experiments is three fold. First to evaluate the performance of the adaptive method for inference on standard benchmarks used by the SMC community where ground truth is known. Second, to evaluate the performance when SMC is used as an inner loop of a learning algorithm. Again we use an example where ground truth is known. Third, to apply SMC learning in complex models that would normally be challenging for SMC comparing to state-ofthe-art approximate inference techniques.
One way of assessing the success of the proposed method would be to evaluate KL[p(z 1:T |x 1:T )||q(z 1:T |x 1:T )]. However, this quantity is hard to accurately compute. Instead we use a number of other metrics. For the experiments where ground truth states z 1:T is known we can evaluate the root mean square error (RMSE) between the approximate posterior mean of the latent variables (z t ) and the true value RMSE(z 1:T ,z 1:T ) = 1 T t (z t −z t ) 2 . More generally, the estimate of the log-marginal likelihood (LML = log p(x 1:T ) = t log p(x t |x 1:t−1 ) = t log( 1 N n w (n) t )) and its variance is also indicative of performance. Finally, we also employ a common metric called the effective sample size (ESS) to measure the effectiveness of our SMC method. ESS of particles at time t is given by If q(z 1:T |x 1:T ) = p(z 1:T |x 1:T ), expected ESS is maximized and equals the number of particles (equivalently, the normalized importance weights are uniform). Note that ESS alone is not a sufficient metric, since it does not measure the absolute quality of samples, but rather the relative quality.

Inference in a Benchmark Nonlinear State-Space Model
In order to evaluate the effectiveness of our adaptive SMC method, we tested our method on a standard nonlinear state-space model often used to benchmark SMC algorithms [2,3]. The model is given by Eq. 3, where θ = (σ v , σ w ). The posterior distribution p θ (z 1:T |x 1:T ) is highly multi-modal due to uncertainty about the signs of the latent states.
The experiments investigated how the new proposal adaptation method performed in comparison to standard methods including the bootstrap filter, EKPF, and UKPF. In particular, we were interested in the following questions: Do rich multi-modal proposals improve inference? For this we compared a Gaussian proposal with a diagonal Gaussian to a mixture density network with three components (-MD-). Does a recurrent parameterisation of the proposal help? For this we compared a non-recurrent neural network with 100 hidden units (-NN-) to a recurrent neural network with 50 LSTM units (-RNN-). Can injecting information about the prior dynamics into the proposal improve performance (similar in spirit to [15] for variational methods)? To asses this, as well as parameterizing proposals for z t that had to learn the dynamics from scratch, we parameterized proposals for v t and let the proposal have access to inputs formed from the prior dynamics f (z t−1 , t) (-f-).
For all experiments, the parameters in the non-linear state-space model were fixed to (σ v , σ w ) = ( √ 10, 1). Adaptation of the proposal was performed on 1000 samples from the generative process at each iteration. Results are summarised in Fig. 1 and Table 1 (see supplementary material for additional results). Average run times for the algorithms over a sequence of length 1000 were: 0.782s bootstrap, 12.1s EKPF, 41.4s UPF, 1.70s NN-NASMC, and 2.67s RNN-NASMC, where EKPF and UPF implementations are provided by [5]. Although these numbers should only be taken as a guide as the implementations had differing levels of acceleration.
The new adaptive proposal methods significantly outperform the bootstrap, EKPF, and UPF methods, in terms of ESS, RMSE and the variance in the LML estimates. The multi-modal proposal outperforms a simple Gaussian proposal (compare RNN-MD-f to RNN-f) indicating multi-modal proposals can improve performance. Moreover, the RNN outperforms the non-recurrent NN (compare RNN to NN). Although the proposal models can effectively learn the transition function, injecting information about the prior dynamics into the proposal does help (compare RNN-f to RNN). Interestingly, there is no clear cut winner between the EKPF and UPF, although the UPF does return LML estimates that have lower variance [5]. All methods converged to similar LMLs that were close to the values computed using large numbers of particles indicating the implementations are correct.

Inference in the Cart and Pole System
As a second and more physically meaningful system we considered a cart-pole system that consists of an inverted pendulum that rests on a movable base [16]. The system was driven by a white noise input. An ODE solver was used to simulate the system from its equations of motion. We considered the problem of inferring the true position of the cart and orientation of the pendulum (along with their derivatives and the input noise) from noisy measurements of the location of the tip of the pole. The results are presented in Fig. 2 (see the supplementary material for more details). The system is significantly more intricate than the model in Sec. 5.1, and does not directly admit the usage of EKPF or UPF. Our RNN-MD proposal model successfully learns good proposals without any direct access to the prior dynamics.

Bayesian learning in a Nonlinear SSM
SMC is often employed as an inner loop of a more complex algorithm. One prominent example is Particle Markov Chain Monte Carlo [3], a class of methods that sample from the joint posterior over model parameters θ and latent state trajectories, p(θ, z 1:T |x 1:T ). Here we consider the Particle Marginal Metropolis-Hasting sampler (PMMH). In this context SMC is used to construct a proposal distribution for a Metropolis-Hasting (MH) accept/reject step. The proposal is formed by sampling a proposed set of parameters e.g. by perturbing the current parameters using a Gaussian random walk, then SMC is used to sample a proposed set of latent state variables, resulting in a joint proposal q(θ * , z * 1:T |θ, z 1:T ) = q(θ * |θ)p θ * (z * 1:T |x 1:T ). The MH step uses the SMC marginal likelihood estimates to determine acceptance. Full details are given in the supplementary material.
In this experiment, we evaluate our method in a PMMH sampler on the same model from Section 5.1 following [3]. 2 A random walk proposal is used to sample θ = (σ v , σ w ), q(θ * |θ) = N (θ * |θ, diag([0.15, 0.08])). The prior over θ is set as IG(0.01, 0.01). θ is initialized as (10,10), and the PMMH is run for 500 iterations. Two of the adaptive models considered section 5.1 are used for comparison (RNN-MD and RNN-MD-f) , where "-pre-" models are pre-trained for 500 iterations using samples from the initial θ = (10,10). The results are shown in Fig. 3 and were typical for a range of parameter settings. Given a sufficient number of particles (N = 100), there is almost no difference between the prior proposal and our method. However, when the number of particles gets smaller (N = 10), NASMC enables significantly faster burn-in to the posterior, particularly on the measurement noise σ w and, for similar reasons, NASMC mixes more quickly. The limitation with the NASMC-PMMH is that the model needs to continuously adapt as the global parameter is sampled, but note this is still not as costly as adapting on a particle-by-particle basis as is the case for the EKPF and UPF.

Polyphonic Music Generation
Finally, the new method is used to train a stochastic recurrent generative model (SRGM) for modelling four polymorphic music datasets of varying complexity [17]. These datasets are often used to benchmark RNN models because of their high dimensionality and the complex temporal dependencies involved at different time scales [17,18,19]. Each dataset contains at least 7 hours of polyphonic music with an average polyphony (number of simultaneous notes) of 3.9 out of 88. The SRGM contains a recurrent neural network with LSTM layers that is driven by i.i.d. stochastic latent variables (z t ) at each time-point and stochastic outputs (x t ) that are fed back into the dynamics (full details in the supplementary material). Both the LSTM layers in the generative and proposal models are set as 1000 units and Adam [20] is used as the optimizer along with the gradient clipping of 0.5 [18]. The bootstrap filter is compared to the new adaptive method (NASMC). 10 particles are used in the training. The hyperparameters are tuned using the validation set [17]. A diagonal Gaussian output is used in the proposal model, with an additional hidden layer of size 200. The log likelihood on the test set, a standard metric for comparison in generative models [18,21,19], is approximated using SMC with 500 particles. The results are reported in Table 2 3 . The adaptive method significantly outperforms the bootstrap filter on three of the four datasets. On the piano dataset the bootstrap method performs marginally better. In general, the NLLs for the new methods are comparable to the state-of-the-art although detailed comparison is difficult as the methods with stochastic latent states require approximate marginalization using importance sampling or SMC.  Table 2: Estimated negative log likelihood on test data. "FD-RNN" and "STORN" are from [19], and "sRNN" and "RNN-NADE" are results from [18].

Comparison of Variational Inference to the NASMC approach
There are several similarities between NASMC and Variational Free-energy methods that employ recognition models. Variational Free-energy methods refine an approximation q φ (z|x) to the posterior distribution p θ (z|x) by optimising the exclusive (or variational) KL-divergence KL[q φ (z|x)||p θ (z|x)]. It is common to approximate this integral using samples from the approximate posterior [21,22,23]. This general approach is similar in spirit to the way that the proposal is adapted in NASMC, except that the inclusive KL-divergence is employed KL[p θ (z|x)||q φ (z|x)] and this entails that sample based approximation requires simulation from the true posterior. Critically, NASMC uses the approximate posterior as a proposal distribution to construct a more accurate posterior approximation. The SMC algorithm therefore can be seen as correcting for the deficiencies in the proposal approximation. We believe that this can lead to significant advantages over variational free-energy methods, especially in the time-series setting where variational methods are known to have severe biases [24]. Moreover, using the inclusive KL avoids having to compute the entropy of the approximating distribution which can prove problematic when using complex approximating distributions (e.g. mixtures and heavy tailed distributions) in the variational framework. There is a close connection between NASMC and the wake-sleep algorithm [25] . The wake-sleep algorithm also employs the inclusive KL divergence to refine a posterior approximation and recent generalisations have shown how to incorporate this idea into importance sampling [26]. In this context, the NASMC algorithm extends this this work to SMC.

Conclusion
This paper developed a powerful method for adapting power proposal distributions within general SMC algorithms. The method parameterises a proposal distribution using a recurrent neural network to model long-range contextual information, allows flexible distributional forms including mixture density networks, and enables efficient training by stochastic gradient descent. The method was found to outperform existing adaptive proposal mechanisms including the EKPF and UPF on a standard SMC benchmark, it improves burn in and mixing of the PMMH sampler, and allows effective training of a deep recurrent generative model using SMC. We hope that the connection between SMC and neural network technologies will inspire further research into adaptive SMC methods. In particular, application of the methods developed in this paper to adaptive particle smoothing, high-dimensional latent models and adaptive PMCMC for probabilistic programming are particular exciting avenues.