So, you want to wrap your machine learning model into an API. Flask used to be the best tool for that, but lately, FastAPI has become my favorite.
Here are my five main reasons why! 👇
1️⃣ Simple yet brilliant interface.
You define the request body model in Pydantic, write the endpoint function to process it, and finally register the route to the app.
That's it.
You can launch the app right away with uvicorn, ready to receive requests!
Icing on the cake: FastAPI automatically generates the OpenAPI specification and a Swagger UI, which you can use to try out your app.
2️⃣ Async support.
You only have to append `async` before the function definition, and you can use async functions inside the endpoint function.
3️⃣ Dependency injections.
This is my favorite. You can inject complex dependencies into endpoints easily, which not only makes testing a breeze but can manage difficult tasks like authentication - in one line.
In the example below, I inject a "database" with minimal effort.
4️⃣ Easy integration of databases.
Working with databases can be a real pain in other Python frameworks. If you ever tried to make Django work with MongoDB, you know what I am talking about.
With FastAPI, it is simple and pleasant, like a cool summer breeze.
5️⃣ GraphQL support.
We all know GraphQL is awesome. You can add it to your app with Graphene + FastAPI easily.
➕1️⃣ Great documentation.
FastAPI's documentation is on par with Django and Flask. As someone coming from the machine learning side, it also taught me a bunch of new things about web development.
Did I convince you? :) Have you tried FastAPI yet? What are your experiences?
• • •
Missing some Tweet in this thread? You can try to
force a refresh
Have you ever implemented a dynamic function dispatcher in Python, where you can register functions at runtime using the decorator syntax? (Like the routers for FastAPI.)
I did this recently, and I am going to teach you how to do it! I'll walk you through it in the thread below.
We will build an event handler to catch arbitrary events and dynamically execute a function to handle the event.
(A good example is catching events in a webhook listener.)
Time to use some decorator magic!
The usage is straightforward: 1) instantiate the EventHandler, 2) register handler functions for specific events, 3) pass the event to the EventHandler instance when caught.
Neural networks are getting HUGE. In their @stateofaireport 2020, @NathanBenaich and @soundboy visualized how the number of parameters grew for breakthrough architectures. The result below is staggering.
What can you do to compress neural networks?
👇A thread.
1⃣ Neural network pruning: iteratively removing connections after training. Turns out that in some cases, 90%+ of the weights can be removed without noticeable performance loss.
A few selected milestone papers:
📰Optimal Brain Damage by @ylecun, John S. Denker, and @SaraASolla. As far as I know, this was the one where the idea was introduced.
1️⃣ If you struggle to understand determinants, stop what you are doing and check out this video by @3blue1brown, it will make your brain explode.
2⃣ Sometimes, it is hard to figure out what a concept represents by looking at how it is calculated. The determinant of a matrix is calculated by a sum, iterating through all permutations of a row.
3⃣ However, this definition doesn't reveal anything about what the determinant means. In fact, it is quite simple: it describes how the volume scales under the corresponding linear transformation.