DEV Community

Cover image for Running Machine Learning Model in Azure Function
Wildan Aziz
Wildan Aziz

Posted on • Updated on

Running Machine Learning Model in Azure Function

☀️☀️☀️

Hi! How's your weekend? I hope it's great. I've been taking a long break since I moved into a new place. Anyway, I'd like to show you how I managed to utilize Azure Function to run amachine learning model. Hopefully, by the end of this post, you're able to understand how it works.


What's Azure Function?

Azure Function is just another service among other good services offered by Microsoft Azure. It's known as a serverless solution. That is, it allow us to focus on writing code instead of worrying about server infrastructure.

So what are the differences between running application on Azure Function and Virtual Machine? This is a good question. Firstly, Azure Function is more cost-effective. In fact, our application will only be charged depending on its running time. Secondly, it's also easier to manage. No need to configure extra settings whatsoever to run application.

Requirements

  1. Python 3.7
  2. Azure Functions Core (Not a hard requirement, but will make life easier)
  3. Visual Studio Code + Azure Function Extension (Again, not a hard requirement)
  4. Azure Function Service
  5. Azure Storage Account
  6. A machine learning model

Steps

In this article, we're making use of Graph Convolutional Network (GCN) machine learning model. To give more detail, it's a specific type of neural network model that accepts a graph data as an input. It then outputs a visualization of the graph's node embeddings. If you're interested to learn more about GCN, feel free to check out this blog first. Otherwise, go ahead clone the repository here.

Open the __init__.py and read the code carefully.

Notice there are HTTP request and response in the code. That is because our function is making use of a HTTP trigger. If you don't know what HTTP trigger is, it's simply a method to let us invoke our function using HTTP request. There are several triggers available to use in Azure Function such as HTTP, timer, and blob.

Open the local.settings.json.

Add your storage account connection string. So what's storage account? storage account is an object storage solution offered by the Azure cloud. We're using this storage to store our function's result.

Use the Azure Functions Core tool to initialize a Python function app.

$ func init

Run the following commands to create and activate a virtual environment named .venv.

$ py -3.7 -m venv .venv
$ .venv\scripts\activate

Install the dependencies by running the following command. Installation may take a few minutes.

$ pip install --no-cache-dir -r requirements.txt

Start the function by starting the local Azure Function runtime host.

$ func start

Note:

If encounter any errors, make sure the correct version of python is already installed. Otherwise, try upgrading the python pip and install the dependencies again. If the errors still persist, Google is the best place to ask questions.

Extra steps (Nice to follow!)

We may now test our function. Download graph dataset here. What is this dataset? It's a preprocessed dataset for our model.

Open the request.py and change the files location to your dataset file location.

Send a POST request to the runtime host on http://localhost:7071/api/nn by executing the code below. The process may take several minutes to complete.

$ py -3.7 request.py

In order to get the function's public endpoint, we need to deploy our function on cloud. Check out the complete documentation on how to here.

Conclusion

We've learned how to run a machine learning model with Azure Function by following the steps above. I hope you understand how Azure Function works in general by now. It's also important to note that it doesn't necessarily have to be a complex application, such as machine learning model, to run with Azure Function. We can use it for as simple as APIs that update a single row or column in database.

Top comments (0)