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.
- In a browser, open the Azure portal at
https://portal.azure.com/
, signing in with your Microsoft account. - 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.
- 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).
- 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.
-
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. -
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.
- 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, eitherterrible
,poor
,average
,very good
, orexcellent
.
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.
- In the Azure portal, navigate to the Azure Machine Learning workspace that starts with rg-genai-….
- 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.
- Close any pop-ups that appear in the studio.
- Navigate to the Compute page, using the menu on the left.
- Select the Compute clusters tab.
- Create a + New cluster.
- 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.
- Virtual machine tier:
- 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.
- Navigate to the Model catalog, using the menu on the left.
- Search for the
bert-base-uncased
model. - Filter by the Curated by Azure AI collection.
- Filter by Text classification under Fine-tuning tasks.
- Select the bert-base-uncased model.
- Select Fine-tune.
- Select
text classification
for Select task type. - Select dataset for Training data, to open the Select training data pop-up.
- Select the Registered datasets tab.
- Select the
hotel-reviews
dataset that was created for you with the setup script. - Select Next and wait for the dataset preview to be loaded. You should have two columns:
Text
andLabel
. - Configure the Required data columns:
- Select the ‘sentence1 key’ column:
Text
- Select the ‘label key’ column:
Label
- Select the ‘sentence1 key’ column:
- Select Add to close the pop-up and go back to the finetuning configuration.
- Leave the validation and test data at the default settings.
- Verify that the compute cluster specifies the
gpu-cluster
that you created during setup. - 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.
- Navigate to the pipeline job overview.
- Select + Register model.
- Leave the default settings for the Model type (
MLflow
) and Job output. - Select Next.
- In the Model settings tab, name the model
hotel-classification-model
. - Select Next.
- Select Register.
Deploy the model
Now that the model is registered, you can deploy the model.
- Navigate to the Models page, using the menu on the left of the workspace.
- Select the
hotel-classification-model
model. - Select Deploy, and select the Real-time endpoint option.
- Leave all settings at their defaults.
- 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.
- Navigate to your endpoint.
-
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.
-
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."]] } }
- Select Test to send the input data to the endpoint.
- The test result should appear on the right.
- 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.