2024-11-11 Weekly Update - Python, Inverse Machine Learning, Set Cardinality
I took the last few weeks off of blogging to focus on some other projects. The highlight was a demo at the local AI Tinkerers meetup of the AI medical document summarization tool I’ve been helping to build. This week, I’m back to blogging and I’ve got a blend of programming, machine learning, notes to share.
This is a longer post, so if you need some music settle into anything by Linenoise.io.
Python Structural Pattern Matching
When I first learned about structural pattern matching in Python (i.e. match
and case
keywords) I glossed over it as equivalent to if/else statements. After reading this great article from Real Python (more than once) I see there’s a lot more depth here. In particular I learned about deconstruction in pattern matching (similar to *args
), conditional pattern execution, and applications to complex nested data structures. I’m not sure how often I would reach for this in production but it’s definitely something I want to experiment with in the future.
Logging for Python Packages
The Python logging module is not the easiest API to grok, but it does have a lot of useful properties once you understand how to hold the tool. It’s also one of those tools where I’ve inherited a logging setup more than I’ve had to set it up myself.
So this week I spent some time digging best practices are for instrumenting logging. The Python standard library docs are pretty rich in examples, patterns, and anti-patterns for logging. I learned a lot of things but my biggest “ah-ha moment” was understanding that logs inherit configurations (e.g. a handler) on import from whatever configuration is executed first. That means that you need to align your logger configurations with the entry point(s) to your code. So for example maybe you define a logger on your main pipeline entry and another in the module’s if __name__ == ”__main__”:
block if it can be run as a stand-alone script.
UV in Docker
Like most people in the Python ecosystem uv is my new go-to tool for Python dependency and environment management. First it replaced Poetry for me, and then pyenv, and so now I want to understand how to effectively use it in a Docker container. Astral’s example project as well as this article by Hynek Schlawack are a great place to get started. But knowing my interest in fundamentals I can tell this is going to lead me to a deeper dive on Docker.
While you’re at it you should also check out this uv cheatsheet which translate commands from other tools (pip, vent, poetry) to their equivalent in uv. This framing helped some of the new commands “click” for me.
Inverse Reinforcement learning
I was talking to a local founder and they mentioned “inverse reinforcement learning” (IRL). I had spent some time this year working (unsuccessfully) on reproducing an experiment for Sutton’s “Reinforcement Learning” textbook so I felt (barely) confident enough to ask them to explain IRL. Fortunately, I was able to get it right away — studying the fundamentals, even briefly, always pays off!
In traditional reinforcement learning you are trying to optimize agent behavior to optimize a known reward function, for example in a multi-armed bandit problem. In IRL it’s the inverse, you are given agent behaviors and instead have to find the reward function that best explains the agent behaviors. This article has a good summary as well as citations to foundational papers in the area.
Cantor’s Theory of Cardinality
I’m enjoying this second lecture from the MIT open courseware on analysis. This lecture is about using bijections to compare the cardinality of sets, specifically infinite sets. It concluded with Cantor’s proof that there are more real numbers than integers. This builds nicely on the proof work I’ve been doing all year out of “The Book of Proof”, enough so that I was able to follow along while cooking dinner.
Not sure other folks would enjoy this lecture, if you already know the material it’s probably too slow and if you’re not familiar with it (or planning to put in some serious study) it’s probably too fast. But it’s a well-done resource worth highlighting. Free MIT math content online — what a time to be alive!
Odds and Ends
Updates on some books and audiobooks:
- I finished “The MANIAC” audiobook which I thought was fantastic. I especially loved the dark overtones and the bridge to AlphaGo in the last section of the book. It makes a great compliment to the more neutral biography “The Man from the Future: The Visionary Life of John von Neumann” which I listened to earlier this year.
- I’m now listening to “A Mind At Play: How Claude Shannon Invented The Information Age”, which is more, maybe I’d call it folksy in tone than ”The MANIC”. It gives a lot of insight into the transition in Electrical Engineering from essentially tinkerers and craftsman to the modern practice which is so intertwined with physics and mathematics.
- I’m trying to read “The Missing Billionaires” which is essentially a book-long study of the application of the Kelley Criterion as a risk management tool. I’m making glacial progress (a few pages as I nod off at night) but the book is great if you’re interested in something partway between quantitative finance and personal investing advice.