PyDPF: A Python Package for Differentiable Particle Filtering

John-Joseph Brady, Benjamin Cox, Yunpeng Li, Víctor ElviraarXiv 2025

Here is a blog-style summary of the paper introducing PyDPF.

TL;DR

PyDPF is an open-source Python package built on PyTorch that unifies several Differentiable Particle Filter (DPF) algorithms under a single, cohesive API. By making the traditionally non-differentiable particle resampling step trackable via gradients, PyDPF allows researchers to seamlessly integrate sequential Monte Carlo methods with deep learning pipelines for end-to-end parameter estimation.

What Problem Does This Paper Address?

State-space models (SSMs) are essential tools for tracking hidden states over time based on noisy observations—think target tracking, financial forecasting, or robotics. When these systems are non-linear or non-Gaussian, standard analytical tools like the Kalman Filter fail. Instead, researchers turn to Particle Filters (PFs), which use a swarm of Monte Carlo samples ("particles") to approximate the true state.

However, traditional particle filters hit a major roadblock when paired with deep learning: they are not differentiable. The crucial "resampling" step requires drawing discrete samples from a categorical distribution based on particle weights. Because you cannot take the derivative of a discrete random sample, you cannot use standard backpropagation (like Adam) to learn unknown system parameters or optimize neural network-based proposal distributions.

While several "Differentiable Particle Filters" (DPFs) have been proposed recently to circumvent this issue, they have historically existed in isolated, fragmented codebases. PyDPF solves this by providing a unified, modular framework to implement, benchmark, and extend these algorithms.

Methods

PyDPF is designed rigidly around object-oriented PyTorch conventions (torch.nn.Module), allowing users to easily swap out transition rules, observation models, or resampling schemes.

The package implements five distinct DPF methodologies that handle the resampling gradient in different ways:

  1. Non-differentiable resampling: Truncates gradients at each time step. It acts like truncated backpropagation through time—low variance but high bias.
  2. Soft resampling: Differentiably scales particle weights using a hyperparameter ($\xi$) to balance statistically efficient sampling against unbiased gradient estimation.
  3. Optimal transport resampling: Replaces stochastic resampling entirely with a deterministic transport map solved via the Sinkhorn algorithm.
  4. Stop-gradient & Marginal stop-gradient resampling: Uses the REINFORCE policy gradient estimator to bypass the non-differentiable step without altering the forward-pass particle estimates.
  5. Kernel mixture resampling: Smooths the particle field by drawing new particles from a kernel density estimator.

To make this architecture work seamlessly, PyDPF introduces a standardized data schema that governs how tensors flow between components:

Standardized data categories in PyDPF used to enable a plug-and-play API design.

Additionally, PyDPF includes custom property decorators like @pydpf.constrained_parameter and @pydpf.cached_property to enforce statistical constraints (e.g., ensuring a variance parameter remains positive) without breaking PyTorch's computational graph.

Datasets

The authors validated PyDPF across three distinct environments:

  • Synthetic Linear-Gaussian SSM: A controlled environment used to benchmark the package's particle filters against the mathematically exact Kalman Filter and test CPU vs. GPU scaling.
  • Stochastic Volatility Model: A non-linear financial time-series model used to evaluate how well the different DPFs can infer single or multiple unknown parameters ($\alpha, \beta, \sigma$).
  • DeepMind Lab Maze: A complex visual localization task where a simulated robot must estimate its 3D position inside a maze using 32x32 RGB camera images and deterministic control actions.

Key Insights and Ideas

  • The Bias-Variance Trade-off: Mathematically "unbiased" gradient methods (like stop-gradient or optimal transport) often suffer from extreme gradient variance over long time horizons. Surprisingly, for simpler models, biased or truncated methods (like non-differentiable resampling) actually yield better parameter convergence because their gradient variance is dramatically lower.
  • GPU Acceleration is Mandatory for Scale: Particle filters are highly parallelizable. PyDPF demonstrates that while a CPU bottlenecks heavily as particle counts scale up to $K=10^4$, a GPU handles them efficiently via tensor chunking, making large-scale sequential Monte Carlo viable for deep learning.

Key Results

  • Algorithm Validation: When tested against a linear-Gaussian model, PyDPF's particle filter successfully converged to the exact Kalman Filter baseline as the particle count increased, dropping the state error ($\epsilon_x$) from 3.8 ($K=25$) down to 0.012 ($K=10^4$).
  • Visual Localization Performance: On the challenging DeepMind Lab maze task, Soft Resampling ($\xi=0.7$) achieved the best performance with a root mean squared error ($\sqrt{\text{Test MSE}}$) of 302, outperforming standard stop-gradient (346) and kernel-mixture resampling (526).
  • The Cost of Optimal Transport: Optimal Transport resampling proved to be computationally prohibitive for complex tasks. In the visual localization experiment, it failed to stably converge and took over 2.5 hours to run, compared to just 13–17 minutes for the other DPF methods. This is due to its $O(K^2)$ complexity and heavy reliance on PyTorch's costly logsumexp operations.

Limitations and Future Work

While PyDPF successfully unifies gradient-based DPFs, the authors note a few limitations:

  • Computational Bottlenecks: $O(K^2)$ methods like Optimal Transport and Kernel Mixture resampling scale poorly to large particle sizes.
  • Scope Constraints: PyDPF is strictly focused on gradient-based parameter optimization. It does not currently support gradient-free optimization, Bayesian parameter estimation (like Particle MCMC), or state smoothing algorithms.
  • CUDA Non-determinism: Certain underlying PyTorch operations (like torch.cumsum) are inherently non-deterministic on GPUs, requiring specific context managers that can slow down execution if absolute reproducibility is required.

Summary updated

BibTeX
@misc{brady2025pydpf,
  author = {John-Joseph Brady and Benjamin Cox and Yunpeng Li and Víctor Elvira},
  title = {{PyDPF: A Python Package for Differentiable Particle Filtering}},
  year = {2025},
  eprint = {2510.25693},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2510.25693},
}
Figure