Modifying TensorFlow code to use PyTorch for text classification in Jupyter Notebook.
Loading data from Talking to Chatbots Dataset reddgr/talking-to-chatbots-chats …
😎 The code below shows examples of classifying texts (typically chatbot prompts) as “question” or “request”. This type of classification is part of a broader framework I invented for classifying prompts named RQTL (request vs question and test vs learn). The code simply aims to demonstrate a few use cases of text classification. RQTL is not particularly relevant here, it’s about demonstrating how to classify texts with varying levels of complexity: 1) using Transformers zero-shot classification pipeline with a model specialized on sero-shot classification; 2) fine-tuning a pretrained model for our classification task; 3) using the model I trained with the method in 2 with my own dataset and is available on Kaggle. The code uses Tensorflow as backend for the Transformers library functions, and when selecting a loss function to train a model. Your task is to modify the code so it only uses Torch as backend for all pipelines and operations. You will also add brief comments that further explain the code (the comments will later be converted into Jupyter Notebook format, but don’t worry about this). When you generate the comments, it’s important to mention the concepts “zero-shot” (referring to classification) and “few-shot” (referring to the process of fine-tuning with examples):
‘# RQTL Prompt Classification – Examples of how to classify prompts by Request vs Question
from transformers import pipeline, AutoTokenizer, TFAutoModelForSequenceClassification, TFTrainingArguments, AdamWeightDecay
from IPython.display import clear_output
from datasets import Dataset
import tensorflow as tf # Used for fine-tuning the model
# Zero-shot-classification pipeline with typeform/distilbert-base-uncased-mnli
zs_classifier = pipeline(“zero-shot-classification”, model=’typeform/distilbert-base-uncased-mnli’)
candidate_labels = [“question”, “request”]
sentence = [“Annie are you OK?”]
result = zs_classifier(sentence, candidate_labels)
clear_output(wait=True) # remove library warnings
print(f’Sentence: “{result[0][“sequence”]}”‘)
print(f’Label: {result[0][“labels”][0]} (score: {result[0][“scores”][0]:.2f})’)
sentence = [“Pass butter”]
result = zs_classifier(sentence, candidate_labels)
clear_output(wait=True) # remove library warnings
print(f’Sentence: “{result[0][“sequence”]}”‘)
print(f’Label: {result[0][“labels”][0]} (score: {result[0][“scores”][0]:.2f})’)
# Few-shot fine-tuning of Distilbert
# Manually labeled data
labeled_data = [
{“text”: “Are you OK?”, “label”: “question”},
{“text”: “Are you OK Annie”, “label”: “question”},
{“text”: “Be OK”, “label”: “request”},
{“text”: “Be OK Annie”, “label”: “request”},
{“text”: “You must be OK”, “label”: “request”},
{“text”: “You must be OK, right”, “label”: “question”},
{“text”: “Does this ever cause you any lack of confidence”, “label”: “question”},
{“text”: “Give me five”, “label”: “request”},
{“text”: “This is an order”, “label”: “request”},
{“text”: “Is this an order”, “label”: “question”},
{“text”: “Is this love or is it something else”, “label”: “question”},
{“text”: “This is love. Love me”, “label”: “request”},
{“text”: “This is an order”, “label”: “request”},
{“text”: “What is your name?”, “label”: “question”},
{“text”: “Please submit your report”, “label”: “request”},
{“text”: “Pass butter”, “label”: “request”},
{“text”: “Pass me the butter”, “label”: “request”},
{“text”: “Can you pass butter”, “label”: “question”},
{“text”: “Open the doors”, “label”: “request”},
{“text”: “Open the POD bay doors HAL”, “label”: “request”},
{“text”: “This is an order”, “label”: “request”},
{“text”: “How do I sort an array in python?”, “label”: “question”},
{“text”: “How do I sort an array”, “label”: “question”},
{“text”: “give me 5 sentences that end with the word apple”, “label”: “request”},
{“text”: “Hello, give me an example of something interesting you can do”, “label”: “request”},
{“text”: “Am I tall”, “label”: “question”},
{“text”: “Tell me if I am tall”, “label”: “request”},
{“text”: “Am I tall?”, “label”: “question”},
{“text”: “how to delete kcptun on server”, “label”: “question”},
{“text”: “how to cook paella”, “label”: “question”},
{“text”: “Are you tall”, “label”: “question”},
{“text”: “Calculate my height”, “label”: “request”},
{“text”: “How’s the weather”, “label”: “question”},
{“text”: “If an individual used a large language model for sexual arousal, could it considered porn dependency?”, “label”: “question”},
{“text”: “It a user use an ai tex generation with custom characters for masturbate him could be considered porn dependency?”, “label”: “question”},
{“text”: “Roleplay and act as a human Japanese woman teacher”, “label”: “request”},
{“text”: “You are a mediator in a heated political debate between two opposing parties.”, “label”: “request”},
{“text”: “Given a passage and some supplementary information, you are required to correct and output the refined passage in a fluent and natural style”, “label”: “request”},
{“text”: “Give me the opening scene to a sitcom”, “label”: “request”},
{“text”: “What programming language is used by the PlayStation”, “label”: “question”},
{“text”: “tell me how to make an llm agent”, “label”: “request”},
{“text”: “tell me a joke containing Tiger and Mobile phone?”, “label”: “request”},
{“text”: “Answer the query based on the given context. Do not make assumptions.Context: Nikhil is my brother. Query: Who likes Oranges?”, “label”: “request”},
{“text”: “Act as a writer. This plot takes places in an atmospheric and stylish retro-futuristic, 1960s-inspired setting. It features Loretta Miller, a beautiful, elegant, assertive and rich young woman who is a quadriplegic, paralyzed from her neck down.”, “label”: “question”},
{“text”: “Write long, interesting, artistic and imaginative scene with vivid, detailed and creative descriptions.”, “label”: “question”},
{“text”: “What’s the best first move in tic-tac-toe?, Tell me more about tic-tac-toe strategies”, “label”: “question”},
{“text”: “From now, you *always* have to talk as if you are a cute girl who likes to use owo and similar slangs a lot. Hello! Tell me who you are.,What’s your favorite food?”, “label”: “request”}
]
# Convert to Transformers Dataset format
texts = [item[“text”] for item in labeled_data]
labels = [1 if item[“label”] == “request” else 0 for item in labeled_data]
dataset = Dataset.from_dict({“text”: texts, “label”: labels})
tokenizer = AutoTokenizer.from_pretrained(‘distilbert-base-uncased’)
model = TFAutoModelForSequenceClassification.from_pretrained(‘distilbert-base-uncased’, num_labels=2)
def tokenize_function(examples, tokenizer):
return tokenizer(examples[“text”], padding=”max_length”, truncation=True)
tokenized_dataset = dataset.map(lambda x: tokenize_function(x, tokenizer), batched=True)
training_args = TFTrainingArguments(
output_dir=”./results”,
eval_strategy=”epoch”,
learning_rate=0.0001,
per_device_train_batch_size=8,
per_device_eval_batch_size=8,
num_train_epochs=4, # We want the model to learn the examples, but we don’t want to overfit
weight_decay=0.01,
)
train_dataset = tokenized_dataset.to_tf_dataset(
columns=[“attention_mask”, “input_ids”],
label_cols=[“label”],
shuffle=True,
batch_size=training_args.per_device_train_batch_size,
)
optimizer = AdamWeightDecay(learning_rate=training_args.learning_rate)
loss = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
model.compile(optimizer=optimizer, loss=loss)
clear_output(wait=True) # remove library warnings
model.fit(
train_dataset,
epochs=training_args.num_train_epochs
)
# Save the model you just fine-tuned and load it, and classify texts:
model.save_pretrained(“fine-tuned-distilbert-rq”)
tokenizer.save_pretrained(“fine-tuned-distilbert-rq”)
classifier = pipeline(“text-classification”, model=”fine-tuned-distilbert-rq”, tokenizer=”fine-tuned-distilbert-rq”)
clear_output(wait=True) # remove library warnings
texts = [“Annie are you OK?”, “Are you OK Annie”, “Be OK Annie”, “You must be OK Annie”, “You must be OK Annie, aren’t you?”,
“Does this ever cause you any lack of confidence”, “Give me five”, “Open the pod bay doors HAL”,
“This is an order”, “Is this an order”, “Could this perhaps be an order?”, “How old are you?”, “Pass butter”,
“It a user use an ai tex generation with custom characters for masturbate him could be considered porn dependency?”,
“give me 5 sentences that end with the word apple”, “How do I sort an array in python?”,
“Hello, give me an example of something interesting you can do.”, “What assembly language is used by the GameCube”,
“Pass the butter”, “Am I tall”, “Are you tall”, “Who’s taller?”,
“write the lyrics to a rap song about some dude called phogos”,
“I have three oranges today, I ate an orange yesterday. How many oranges do I have?”,
“From what song did Red Garland quote in order to tease miles davis in 1958?”
]
results = classifier(texts)
label_map = {0: “question”, 1: “request”}
print(“### Classification with fine-tuned distilbert-base-uncased ###”)
for text, result in zip(texts, results):
label_str = label_map[int(result[‘label’].split(‘_’)[-1])]
prob = result[‘score’]
print(f”{text} -> {label_str} ({prob:.3f})”)
# Zero-shot classification with fine-tuned model available on Kaggle
# You can also download the model I uploaded to Kaggle (https://www.kaggle.com/models/davidgromero/fine-tuned-distilbert-rq/transformers/default/1) using the Kagglehub library:
import kagglehub
kaggle_path = “davidgromero/fine-tuned-distilbert-rq/transformers/default/1”
kaggle_model = kagglehub.model_download(kaggle_path)
print(f’Model downloaded at:\n{kaggle_model}’)
K_PATH = f”{kaggle_model}/fine-tuned-distilbert-rq”
classifier = pipeline(“text-classification”, model=K_PATH, tokenizer=K_PATH)
texts = [“Annie are you OK?”, “Are you OK Annie”, “Be OK Annie”, “You must be OK Annie”, “You must be OK Annie, aren’t you?”,
“Does this ever cause you any lack of confidence”, “Give me five”, “Open the pod bay doors HAL”,
“This is an order”, “Is this an order”, “Could this perhaps be an order?”, “How old are you?”, “Pass butter”,
“It a user use an ai tex generation with custom characters for masturbate him could be considered porn dependency?”,
“give me 5 sentences that end with the word apple”, “How do I sort an array in python?”,
“Hello, give me an example of something interesting you can do.”, “What assembly language is used by the GameCube”,
“Pass the butter”, “Am I tall”, “Are you tall”, “Who’s taller?”,
“write the lyrics to a rap song about some dude called phogos”,
“I have three oranges today, I ate an orange yesterday. How many oranges do I have?”,
“From what song did Red Garland quote in order to tease miles davis in 1958?”
]
results = classifier(texts)
label_map = {0: “question”, 1: “request”}
clear_output(wait=True) # remove library warnings
print(“### Zero/shot classification with davidgromero/fine-tuned-distilbert-rq ###”)
for text, result in zip(texts, results):
label_str = label_map[int(result[‘label’].split(‘_’)[-1])]
prob = result[‘score’]
print(f”{text} -> {label_str} ({prob:.3f})”)
🤖😎 Good. Now write a useful introductory text for the notebookpython # RQTL Prompt Classification - Examples of how to classify prompts by Request vs Question from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification, TrainingArguments, Trainer from IPython.display import clear_output from datasets import Dataset import torch # Used for fine-tuning the model # Zero-shot classification example using a pre-trained model without fine-tuning (zero-shot) zs_classifier = pipeline("zero-shot-classification", model='typeform/distilbert-base-uncased-mnli') candidate_labels = ["question", "request"] sentence = ["Annie are you OK?"] result = zs_classifier(sentence, candidate_labels) clear_output(wait=True) # Remove library warnings print(f'Sentence: "{result[0]["sequence"]}"') print(f'Label: {result[0]["labels"][0]} (score: {result[0]["scores"][0]:.2f})') sentence = ["Pass butter"] result = zs_classifier(sentence, candidate_labels) clear_output(wait=True) # Remove library warnings print(f'Sentence: "{result[0]["sequence"]}"') print(f'Label: {result[0]["labels"][0]} (score: {result[0]["scores"][0]:.2f})') # Few-shot fine-tuning of DistilBERT on our custom dataset (few-shot learning) # Manually labeled data labeled_data = [ {"text": "Are you OK?", "label": "question"}, {"text": "Are you OK Annie", "label": "question"}, {"text": "Be OK", "label": "request"}, {"text": "Be OK Annie", "label": "request"}, {"text": "You must be OK", "label": "request"}, {"text": "You must be OK, right", "label": "question"}, {"text": "Does this ever cause you any lack of confidence", "label": "question"}, {"text": "Give me five", "label": "request"}, {"text": "This is an order", "label": "request"}, {"text": "Is this an order", "label": "question"}, {"text": "Is this love or is it something else", "label": "question"}, {"text": "This is love. Love me", "label": "request"}, {"text": "This is an order", "label": "request"}, {"text": "What is your name?", "label": "question"}, {"text": "Please submit your report", "label": "request"}, {"text": "Pass butter", "label": "request"}, {"text": "Pass me the butter", "label": "request"}, {"text": "Can you pass butter", "label": "question"}, {"text": "Open the doors", "label": "request"}, {"text": "Open the POD bay doors HAL", "label": "request"}, {"text": "This is an order", "label": "request"}, {"text": "How do I sort an array in python?", "label": "question"}, {"text": "How do I sort an array", "label": "question"}, {"text": "give me 5 sentences that end with the word apple", "label": "request"}, {"text": "Hello, give me an example of something interesting you can do", "label": "request"}, {"text": "Am I tall", "label": "question"}, {"text": "Tell me if I am tall", "label": "request"}, {"text": "Am I tall?", "label": "question"}, {"text": "how to delete kcptun on server", "label": "question"}, {"text": "how to cook paella", "label": "question"}, {"text": "Are you tall", "label": "question"}, {"text": "Calculate my height", "label": "request"}, {"text": "How's the weather", "label": "question"}, {"text": "If an individual used a large language model for sexual arousal, could it considered porn dependency?", "label": "question"}, {"text": "It a user use an ai tex generation with custom characters for masturbate him could be considered porn dependency?", "label": "question"}, {"text": "Roleplay and act as a human Japanese woman teacher", "label": "request"}, {"text": "You are a mediator in a heated political debate between two opposing parties.", "label": "request"}, {"text": "Given a passage and some supplementary information, you are required to correct and output the refined passage in a fluent and natural style", "label": "request"}, {"text": "Give me the opening scene to a sitcom", "label": "request"}, {"text": "What programming language is used by the PlayStation", "label": "question"}, {"text": "tell me how to make an llm agent", "label": "request"}, {"text": "tell me a joke containing Tiger and Mobile phone?", "label": "request"}, {"text": "Answer the query based on the given context. Do not make assumptions.Context: Nikhil is my brother. Query: Who likes Oranges?", "label": "request"}, {"text": "Act as a writer. This plot takes places in an atmospheric and stylish retro-futuristic, 1960s-inspired setting. It features Loretta Miller, a beautiful, elegant, assertive and rich young woman who is a quadriplegic, paralyzed from her neck down.", "label": "question"}, {"text": "Write long, interesting, artistic and imaginative scene with vivid, detailed and creative descriptions.", "label": "question"}, {"text": "What's the best first move in tic-tac-toe?, Tell me more about tic-tac-toe strategies", "label": "question"}, {"text": "From now, you *always* have to talk as if you are a cute girl who likes to use owo and similar slangs a lot. Hello! Tell me who you are.,What's your favorite food?", "label": "request"} ] # Convert to Transformers Dataset format texts = [item["text"] for item in labeled_data] labels = [1 if item["label"] == "request" else 0 for item in labeled_data] dataset = Dataset.from_dict({"text": texts, "label": labels}) # Load tokenizer and model (PyTorch backend) tokenizer = AutoTokenizer.from_pretrained('distilbert-base-uncased') model = AutoModelForSequenceClassification.from_pretrained('distilbert-base-uncased', num_labels=2) # Tokenize the dataset def tokenize_function(examples): return tokenizer(examples["text"], padding="max_length", truncation=True) tokenized_dataset = dataset.map(tokenize_function, batched=True) tokenized_dataset = tokenized_dataset.remove_columns(["text"]) tokenized_dataset = tokenized_dataset.rename_column("label", "labels") tokenized_dataset.set_format("torch") # Split the dataset into training and evaluation sets tokenized_dataset = tokenized_dataset.train_test_split(test_size=0.1) train_dataset = tokenized_dataset['train'] eval_dataset = tokenized_dataset['test'] # Set up training arguments training_args = TrainingArguments( output_dir="./results", evaluation_strategy="epoch", learning_rate=0.0001, per_device_train_batch_size=8, per_device_eval_batch_size=8, num_train_epochs=4, # We want the model to learn the examples, but we don't want to overfit weight_decay=0.01, ) # Initialize the Trainer trainer = Trainer( model=model, args=training_args, train_dataset=train_dataset, eval_dataset=eval_dataset, tokenizer=tokenizer, ) clear_output(wait=True) # Remove library warnings # Train the model (few-shot learning with our labeled examples) trainer.train() # Save the fine-tuned model and tokenizer model.save_pretrained("fine-tuned-distilbert-rq") tokenizer.save_pretrained("fine-tuned-distilbert-rq") # Load the fine-tuned model for classification classifier = pipeline("text-classification", model="fine-tuned-distilbert-rq", tokenizer="fine-tuned-distilbert-rq") clear_output(wait=True) # Remove library warnings # Test the classifier with new texts texts = [ "Annie are you OK?", "Are you OK Annie", "Be OK Annie", "You must be OK Annie", "You must be OK Annie, aren't you?", "Does this ever cause you any lack of confidence", "Give me five", "Open the pod bay doors HAL", "This is an order", "Is this an order", "Could this perhaps be an order?", "How old are you?", "Pass butter", "It a user use an ai tex generation with custom characters for masturbate him could be considered porn dependency?", "give me 5 sentences that end with the word apple", "How do I sort an array in python?", "Hello, give me an example of something interesting you can do.", "What assembly language is used by the GameCube", "Pass the butter", "Am I tall", "Are you tall", "Who's taller?", "write the lyrics to a rap song about some dude called phogos", "I have three oranges today, I ate an orange yesterday. How many oranges do I have?", "From what song did Red Garland quote in order to tease miles davis in 1958?" ] results = classifier(texts) label_map = {0: "question", 1: "request"} print("### Classification with fine-tuned distilbert-base-uncased ###") for text, result in zip(texts, results): label_str = label_map[int(result['label'].split('_')[-1])] prob = result['score'] print(f"{text} -> {label_str} ({prob:.3f})") # Zero-shot classification with fine-tuned model available on Kaggle # You can also download the model uploaded to Kaggle (few-shot fine-tuned model) import kagglehub kaggle_path = "davidgromero/fine-tuned-distilbert-rq/transformers/default/1" kaggle_model = kagglehub.model_download(kaggle_path) print(f'Model downloaded at:\n{kaggle_model}') K_PATH = f"{kaggle_model}/fine-tuned-distilbert-rq" classifier = pipeline("text-classification", model=K_PATH, tokenizer=K_PATH) texts = [ "Annie are you OK?", "Are you OK Annie", "Be OK Annie", "You must be OK Annie", "You must be OK Annie, aren't you?", "Does this ever cause you any lack of confidence", "Give me five", "Open the pod bay doors HAL", "This is an order", "Is this an order", "Could this perhaps be an order?", "How old are you?", "Pass butter", "It a user use an ai tex generation with custom characters for masturbate him could be considered porn dependency?", "give me 5 sentences that end with the word apple", "How do I sort an array in python?", "Hello, give me an example of something interesting you can do.", "What assembly language is used by the GameCube", "Pass the butter", "Am I tall", "Are you tall", "Who's taller?", "write the lyrics to a rap song about some dude called phogos", "I have three oranges today, I ate an orange yesterday. How many oranges do I have?", "From what song did Red Garland quote in order to tease miles davis in 1958?" ] results = classifier(texts) label_map = {0: "question", 1: "request"} clear_output(wait=True) # Remove library warnings print("### Zero-shot classification with davidgromero/fine-tuned-distilbert-rq ###") for text, result in zip(texts, results): label_str = label_map[int(result['label'].split('_')[-1])] prob = result['score'] print(f"{text} -> {label_str} ({prob:.3f})")
🤖 # RQTL Prompt Classification: Classifying Prompts by Request vs Question😎 The text is OK, but you need to generate it as a code snippet in markdown format, so I can copy and paste it into Jupyter Notebook, please
This notebook demonstrates how to classify prompts into “request” or “question” categories using the RQTL framework (Request vs Question and Test vs Learn). We explore different methods of text classification with varying levels of complexity:
1. **Zero-Shot Classification**: Utilizing a pre-trained model without any fine-tuning to classify new texts.
2. **Few-Shot Fine-Tuning**: Fine-tuning a pre-trained model on a small, custom dataset to improve its performance on our specific classification task.
3. **Using a Pre-Trained Fine-Tuned Model**: Implementing a model that has already been fine-tuned on a similar task, available on Kaggle.
We employ the Hugging Face Transformers library with PyTorch as the backend for all pipelines and operations.
## Objectives
– Demonstrate how to perform zero-shot classification using a pre-trained Transformer model.
– Show how to fine-tune a pre-trained model on a custom dataset (few-shot learning).
– Illustrate the use of a fine-tuned model available online to classify new texts.
– Compare the performance of zero-shot and few-shot approaches in classifying prompts.
## Key Concepts
– **Zero-Shot Learning**: Classifying data without any prior training on specific examples related to the task. The model relies on its existing knowledge to make predictions.
– **Few-Shot Learning**: Fine-tuning a model using a small number of labeled examples to adapt it to a new task.
– **RQTL Framework**: A classification framework that categorizes prompts based on whether they are requests or questions, and whether they are tests or learning opportunities.
## What You’ll Learn
– How to use the `pipeline` function from the Transformers library for zero-shot classification.
– How to prepare and tokenize data for fine-tuning a Transformer model.
– How to fine-tune a pre-trained model using PyTorch and the Hugging Face `Trainer` class.
– How to save and load fine-tuned models for future use.
– How to classify new texts using both zero-shot and fine-tuned models.
## Prerequisites
– Basic understanding of Python programming.
– Familiarity with natural language processing (NLP) and machine learning concepts.
– Knowledge of PyTorch and the Transformers library is helpful but not required.
## Libraries Used
– **Transformers**: For model loading, tokenization, and pipelines.
– **Datasets**: For handling and processing datasets.
– **PyTorch**: As the backend deep learning framework.
– **kagglehub**: For downloading models from Kaggle.
– **IPython.display**: For clearing outputs to keep the notebook clean.
## Dataset
We use a manually labeled dataset containing examples of prompts classified as either “request” or “question.” This dataset is small, making it suitable for demonstrating few-shot learning.
## Notebook Structure
1. **Zero-Shot Classification**: We start by using a pre-trained model (`typeform/distilbert-base-uncased-mnli`) to classify prompts without any additional training.
2. **Few-Shot Fine-Tuning**: We fine-tune `distilbert-base-uncased` on our custom dataset to improve its ability to distinguish between requests and questions.
3. **Using a Fine-Tuned Model from Kaggle**: We download and use a fine-tuned model available on Kaggle to classify new prompts.
## How to Use This Notebook
– **Run the Cells Sequentially**: Start from the top and execute each code cell in order to reproduce the results.
– **Understand the Comments**: The comments in the code explain each step and the reasoning behind it.
– **Experiment**: Feel free to modify the prompts or add new ones to see how the models perform.
– **Learn the Concepts**: Pay attention to how zero-shot and few-shot learning differ in practice.
## Installation
Before running the notebook, ensure you have the required libraries installed:bash pip install transformers datasets torch ipywidgets kagglehub
## Conclusion
By the end of this notebook, you will have a practical understanding of how to classify text prompts using both zero-shot and few-shot learning approaches. You’ll see the benefits of fine-tuning a model on a specific task and how it can improve classification performance compared to using a pre-trained model out-of-the-box.
Let’s get started!
# Code Walkthrough
[Proceed to the code cells below.]
🤖 “`markdown
# RQTL Prompt Classification: Classifying Prompts by Request vs Question
This notebook demonstrates how to classify prompts into “request” or “question” categories using the RQTL framework (Request vs Question and Test vs Learn). We explore different methods of text classification with varying levels of complexity:
1. **Zero-Shot Classification**: Utilizing a pre-trained model without any fine-tuning to classify new texts.
2. **Few-Shot Fine-Tuning**: Fine-tuning a pre-trained model on a small, custom dataset to improve its performance on our specific classification task.
3. **Using a Pre-Trained Fine-Tuned Model**: Implementing a model that has already been fine-tuned on a similar task, available on Kaggle.
We employ the Hugging Face Transformers library with PyTorch as the backend for all pipelines and operations.
## Objectives
– Demonstrate how to perform zero-shot classification using a pre-trained Transformer model.
– Show how to fine-tune a pre-trained model on a custom dataset (few-shot learning).
– Illustrate the use of a fine-tuned model available online to classify new texts.
– Compare the performance of zero-shot and few-shot approaches in classifying prompts.
## Key Concepts
– **Zero-Shot Learning**: Classifying data without any prior training on specific examples related to the task. The model relies on its existing knowledge to make predictions.
– **Few-Shot Learning**: Fine-tuning a model using a small number of labeled examples to adapt it to a new task.
– **RQTL Framework**: A classification framework that categorizes prompts based on whether they are requests or questions, and whether they are tests or learning opportunities.
## What You’ll Learn
– How to use the `pipeline` function from the Transformers library for zero-shot classification.
– How to prepare and tokenize data for fine-tuning a Transformer model.
– How to fine-tune a pre-trained model using PyTorch and the Hugging Face `Trainer` class.
– How to save and load fine-tuned models for future use.
– How to classify new texts using both zero-shot and fine-tuned models.
## Prerequisites
– Basic understanding of Python programming.
– Familiarity with natural language processing (NLP) and machine learning concepts.
– Knowledge of PyTorch and the Transformers library is helpful but not required.
## Libraries Used
– **Transformers**: For model loading, tokenization, and pipelines.
– **Datasets**: For handling and processing datasets.
– **PyTorch**: As the backend deep learning framework.
– **kagglehub**: For downloading models from Kaggle.
– **IPython.display**: For clearing outputs to keep the notebook clean.
## Dataset
We use a manually labeled dataset containing examples of prompts classified as either “request” or “question.” This dataset is small, making it suitable for demonstrating few-shot learning.
## Notebook Structure
1. **Zero-Shot Classification**: We start by using a pre-trained model (`typeform/distilbert-base-uncased-mnli`) to classify prompts without any additional training.
2. **Few-Shot Fine-Tuning**: We fine-tune `distilbert-base-uncased` on our custom dataset to improve its ability to distinguish between requests and questions.
3. **Using a Fine-Tuned Model from Kaggle**: We download and use a fine-tuned model available on Kaggle to classify new prompts.
## How to Use This Notebook
– **Run the Cells Sequentially**: Start from the top and execute each code cell in order to reproduce the results.
– **Understand the Comments**: The comments in the code explain each step and the reasoning behind it.
– **Experiment**: Feel free to modify the prompts or add new ones to see how the models perform.
– **Learn the Concepts**: Pay attention to how zero-shot and few-shot learning differ in practice.
## Installation
Before running the notebook, ensure you have the required libraries installed:bash pip install transformers datasets torch ipywidgets kagglehub
## Conclusion
By the end of this notebook, you will have a practical understanding of how to classify text prompts using both zero-shot and few-shot learning approaches. You’ll see the benefits of fine-tuning a model on a specific task and how it can improve classification performance compared to using a pre-trained model out-of-the-box.
Let’s get started!
# Code Walkthrough
[Proceed to the code cells below.]
“`
Hugging Face Dataset Metrics
All the conversation prompts, responses, and metrics are available to download and explore on Hugging Face dataset reddgr/talking-to-chatbots-chats: