Fine-tune a foundation model in the Azure Machine Learning studio

When you want to improve a model that processes natural language, you may try to fine-tune a foundation model. A foundation model is already pre-trained on a large corpus of text, meaning you’ll spend considerably less time fine-tuning the model to accommodate your needs than when you would train a completely new model from scratch.

In this exercise you’ll choose a foundation model from Azure Machine Learning’s model catalog. You’ll use a small dataset to fine-tune the model and you’ll deploy the model to an endpoint. Finally, you can test your deployed model by sending new data to the endpoint.

Before you start

You’ll need an Azure subscription in which you have administrative-level access.

Provision an Azure Machine Learning workspace

An Azure Machine Learning workspace provides a central place for managing all resources and assets you need to train and manage your models. You can interact with the Azure Machine Learning workspace through the studio, Python SDK, and Azure CLI.

You’ll use the Azure CLI to provision the workspace and register a data asset, and you’ll use the studio to explore the model catalog and fine-tune a model.

Create the workspace and upload the dataset

To create the Azure Machine Learning workspace, and upload the dataset to the workspace, you’ll use the Azure CLI. All necessary commands are grouped in a Shell script for you to execute.

  1. In a browser, open the Azure portal at https://portal.azure.com/, signing in with your Microsoft account.
  2. Select the [>_] (Cloud Shell) button at the top of the page to the right of the search box. This opens a Cloud Shell pane at the bottom of the portal.
  3. Select Bash if asked. The first time you open the cloud shell, you will be asked to choose the type of shell you want to use (Bash or PowerShell).
  4. Check that the correct subscription is specified and select Create storage if you are asked to create storage for your cloud shell. Wait for the storage to be created.
  5. In the terminal, enter the following commands to clone this repo:

     rm -r gen-ai-labs -f
     git clone https://github.com/MicrosoftLearning/advanced-gen-ai.git gen-ai-labs
    

    Use SHIFT + INSERT to paste your copied code into the Cloud Shell.

  6. After the repo has been cloned, enter the following commands to change to the folder for this lab and run the setup.sh script it contains:

     cd gen-ai-labs/Labs/setup-script
     ./setup.sh
    

    Ignore any (error) messages that say that the extensions were not installed.

  7. Wait for the script to complete - this typically takes around 5-10 minutes.

Explore the data

While you wait for the setup script to complete, let’s explore the data you’ll use to fine-tune a foundation model.

You can use foundation models for different purposes, one of them is to classify text. The data in this exercise contains reviews of hotels that customers may have posted on a website. You may want to classify the sentiment of the hotel reviews. Though you could classify them as simply positive or negative, you could also create your own labels like terrible, poor, average, very good, excellent.

The dataset provided for this exercise contains two columns:

  • Text: A hotel review (one or two sentences).
  • Label: Manually added labels, either terrible, poor, average, very good, or excellent.

Some examples from the dataset include:

Text Label
I stayed at Contoso Suites and my experience was superb. The hotel had fantastic amenities and the staff was wonderful. Excellent
Alpine Ski House was pleasing. The rooms were satisfying. Very good
Margie’s Travel is a fair hotel. Average
If you’re looking for a lackluster getaway, look no further than Contoso Suites. Poor
Contoso Suites is a dreadful hotel. Terrible

By finetuning a model with the provided dataset, you’ll be able to classify hotel reviews quickly and easily, simply based on the text.

Create a compute cluster

Azure Machine Learning studio is a web-based portal through which you can access the Azure Machine Learning workspace. You can use the Azure Machine Learning studio to manage all assets and resources within your workspace. To fine-tune a language model, you’ll need a GPU cluster. You can create a GPU cluster in the studio.

  1. In the Azure portal, navigate to the Azure Machine Learning workspace that starts with rg-genai-….
  2. Select the Azure Machine Learning workspace, and in its Overview page, select Launch studio. Another tab will open in your browser to open the Azure Machine Learning studio.
  3. Close any pop-ups that appear in the studio.
  4. Navigate to the Compute page, using the menu on the left.
  5. Select the Compute clusters tab.
  6. Create a + New cluster.
  7. Configure the compute cluster with the following settings:
    • Virtual machine tier: Low priority
    • Virtual machine type: GPU
    • Virtual machine size: The cheapest available, for example the `Standard_NC6s_v3
    • Name: gpu-cluster
    • Keep all other settings at the default value.
  8. Create the compute cluster and verify that it shows up in the list of compute clusters.

Fine-tune the model

To fine-tune the model, you need training data, a compute cluster, and a pretrained model. The script registered a training dataset for you with hotel reviews. You created the necessary compute in the previous section. Now, it’s time to choose the pretrained model you want to use and fine-tune the model.

  1. Navigate to the Model catalog, using the menu on the left.
  2. Search for the bert-base-uncased model.
  3. Filter by the Curated by Azure AI collection.
  4. Filter by Text classification under Fine-tuning tasks.
  5. Select the bert-base-uncased model.
  6. Select Fine-tune.
  7. Select text classification for Select task type.
  8. Select dataset for Training data, to open the Select training data pop-up.
  9. Select the Registered datasets tab.
  10. Select the hotel-reviews dataset that was created for you with the setup script.
  11. Select Next and wait for the dataset preview to be loaded. You should have two columns: Text and Label.
  12. Configure the Required data columns:
    • Select the ‘sentence1 key’ column: Text
    • Select the ‘label key’ column: Label
  13. Select Add to close the pop-up and go back to the finetuning configuration.
  14. Leave the validation and test data at the default settings.
  15. Verify that the compute cluster specifies the gpu-cluster that you created during setup.
  16. Select Finish to trigger the finetuning job. The job will first prepare the compute cluster, and then fine-tune the model. The time needed to fine-tune the model will depend on the cluster you use. You may need to refresh the page to get the latest job status.

Test the model

Finally, to test and use the model, you need to register and deploy the model.

Register the model

First, you’ll register the model from the job’s output.

  1. Navigate to the pipeline job overview.
  2. Select + Register model.
  3. Leave the default settings for the Model type (MLflow) and Job output.
  4. Select Next.
  5. In the Model settings tab, name the model hotel-classification-model.
  6. Select Next.
  7. Select Register.

Deploy the model

Now that the model is registered, you can deploy the model.

  1. Navigate to the Models page, using the menu on the left of the workspace.
  2. Select the hotel-classification-model model.
  3. Select Deploy, and select the Real-time endpoint option.
  4. Leave all settings at their defaults.
  5. Select Deploy.

An endpoint will be created first. Then, the registered model will be deployed to the endpoint. The deployment may take some time. Wait until the deployment is complete.

Test the endpoint

When the model is successfully deployed, you can test your model. The quickest way to test your model is directly in the Azure Machine Learning Studio.

  1. Navigate to your endpoint.
  2. Select the Test tab.

    Note: If the Test tab doesn’t appear, the deployment isn’t finalized yet. Wait until the model is deployed to the endpoint and refresh your page to find the Test tab.

  3. Copy and paste the following JSON into the Input data to test endpoint field:

     {
       "input_data": {
         "columns": [
           "input_string"
         ],
         "index": [0, 1, 2, 3, 4],
         "data": [["Contoso Suites exceeded our expectations with their impeccable service and luxurious amenities."], ["The breathtaking mountain views from our room at Alpine Ski House made our stay truly magical."], ["We had a disappointing experience at Margie's Travel due to the lack of cleanliness and outdated facilities."], ["The cozy fireplace in our room at Alpine Ski House created a warm and inviting atmosphere after a day on the slopes."], ["The rooftop pool at Contoso Suites offered a stunning panoramic view of the city, making our stay unforgettable."]]
       }
     }
    
  4. Select Test to send the input data to the endpoint.
  5. The test result should appear on the right.
  6. Review the output and you’ll find that each sentence has been labeled with a classification of the text.

Feel free to experiment and change the text of the hotel reviews to explore the model’s classifications.

Delete Azure resources

When you finish exploring Azure Machine Learning, you should delete the resources you’ve created to avoid unnecessary Azure costs.

  • Close the Azure Machine Learning studio tab and return to the Azure portal.
  • In the Azure portal, on the Home page, select Resource groups.
  • Select the resource group starting with rg-genai….
  • At the top of the Overview page for your resource group, select Delete resource group.
  • Enter the resource group name to confirm you want to delete it, and select Delete.