Engineering PyMC3 models into production ML systems


I’ve put together a tutorial on how I think you could engineer PyMC3 models into production systems - e.g. as web APIs with various endpoints, depending on whether you want a point estimate, highest-density interval or density, etc.

I’ve tried to address what the machine learning lifecycle might look like for a PyMC3 project, how to engineer the service (using FastAPI) and then deploy it to Kubernetes using Bodywork (an open-source ML deployment tool that I contribute to).

The project used in the tutorial is hosted on GitHub,

And you should be able to use this as a template for deploying your own projects.

I’ve been involved in the ML engineering and MLOps side of things for a couple of years now, but I’ve been a PyMC3 fanatic for a lot longer. It struck me, that there’s almost no talk of Bayesian methods and probabilistic programming in these fields, which is odd, given that there aren’t many other pragmatic approaches for tackling uncertainty in prediction (as far as I can see).

Any feedback would be greatly appreciated.




@AlexIoannides thank you for posting here! I had always wondered good API endpoints would be, as they define the language that we use in prod. Looks like you’ve nailed a great starting point:

  1. Point
  2. Interval
  3. Density

These nouns have a natural mapping to the probabilistic output. I think I’ve been blinded by sklearn-style point prediction functions that always return a /predict API.

Thank you for sharing!

1 Like

This is incredible @AlexIoannides and very timely. Thanks for sharing!

In relation to this, we started thinking about better API for saving and loading modules, would be curious to get your thoughts on this: Improve workflow around saving and loading fitted models · Issue #4687 · pymc-devs/pymc3 · GitHub

1 Like

Thanks for the encouragement!

Personally, I didn’t think that pickling and persisting the Model alongside saving the netCDF inference data, was ‘that’ big a deal. It’s certainly no different to the recommended way of persisting PyTorch models (‘models’ and weights persisted separately).

What am I missing?

I’ll consider it further and post my comments on GitHub.

Thanks for taking the time to do this!

1 Like

The pleasure is mine - I :heart: PyMC3 and it was always going to be top of list for a Bodywork demo.

1 Like