How can you fix the DISE-TUNE DISELBER OF EMOVERS

Customer support groups were hypocritical about a large volume of customer questions throughout the company I worked for. Have you ever had the same experience?
What if I told you you can use AI automatically point, Separate yourself, besides separate Most common problems?
By planning a transformer model such as the Bert, you can create a default program that includes tickets in the type of problems and submits them to the correct group.
In this lesson, I will show you how to fix the transformer model of emotional separation in five steps:
- Set your nature: Prepare your data and enter the required libraries.
- The load and the data for priority: Collect text files and edit your data.
- Fine-Tune Dise Dincilbert: Railing model to distinguish feelings of your data.
- Check performance: Use mathemakes such as accuracy, F1-Score, and the matriculants of confusion measuring model performance.
- Translate the prediction: Add to imagination and understand the prediction using shapes (additional Shapley definitions).
At the end, you will have a well-organized model that separates the text from the accuracy of the accuracy, and you will also learn how these predictions use the predictions using Shape.
The same approach can be included in the Real-World World Social Cases of Emotional Separation, such as Automation of Customer Support, Audio-based Automation, Content, and more.
Let's get in!
To select the correct transformer model
When selecting the transformer model of Division of textHere's a quick decrease in ordinary models:
- Church: Good for Normal NLP tasks, but it is very expensive training for both.
- Distilbert: 60% Soon there is a beert while keeping 97% of its abilities, making it good for real-time applications.
- Roberta: The most solid nature of the Bert, but requires additional resources.
- Xlm-Robertter: Multiple languages by Roberta is trained 100 languages. Ready for many languages, but it is very powerful.
In this study, I chose to dissolve a DIME-TUNE DISTILBER because it provides the best balance between working and efficiency.
Step 1: Setting and installing dependence
Make sure you have the required libraries installed:
!pip install datasets transformers torch scikit-learn shap
Step 2: Upload and Setory Data
I have used NLNOTS of NLCP by the Praveen Govi, which is GGLI, a license for commercial use. It contains text-based text. Details comes in three .txt Files: Train, verification, including try.
Each line contains a sentence and its corresponding labeling tournament with the semicolon:
text; emotion
"i didnt feel humiliated"; "sadness"
"i am feeling grouchy"; "anger"
"im updating my blog because i feel shitty"; "sadness"
Integrating the dataset in the data of pandas
Let's download dataset:
def parse_emotion_file(file_path):
"""
Parses a text file with each line in the format: {text; emotion}
and returns a pandas DataFrame with 'text' and 'emotion' columns.
Args:
- file_path (str): Path to the .txt file to be parsed
Returns:
- df (pd.DataFrame): DataFrame containing 'text' and 'emotion' columns
"""
texts = []
emotions = []
with open(file_path, 'r', encoding='utf-8') as file:
for line in file:
try:
# Split each line by the semicolon separator
text, emotion = line.strip().split(';')
# append text and emotion to separate lists
texts.append(text)
emotions.append(emotion)
except ValueError:
continue
return pd.DataFrame({'text': texts, 'emotion': emotions})
# Parse text files and store as Pandas DataFrames
train_df = parse_emotion_file("train.txt")
val_df = parse_emotion_file("val.txt")
test_df = parse_emotion_file("test.txt")
Understanding the Distribution of Label
This data contains Examples of 16k training including 2k Examples with verification and examination. Here is the breaking of the label distribution:
The bar chart above shows that the dataset is Zimaled, Via many labels samples as a pleasure and sadness.
To correctly edit the production model.
Step 3: Tokenization and Basic Data
Next, I loaded to Diskizert's Tokozer:
from transformers import AutoTokenizer
# Define the model path for DistilBERT
model_name = "distilbert-base-uncased"
# Load the tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_name)
Then, I used the immersion to the text and convert the labels into small IDs:
# Tokenize data
def preprocess_function(df, label2id):
"""
Tokenizes text data and transforms labels into numerical IDs.
Args:
df (dict or pandas.Series): A dictionary-like object containing "text" and "emotion" fields.
label2id (dict): A mapping from emotion labels to numerical IDs.
Returns:
dict: A dictionary containing:
- "input_ids": Encoded token sequences
- "attention_mask": Mask to indicate padding tokens
- "label": Numerical labels for classification
Example usage:
train_dataset = train_dataset.map(lambda x: preprocess_function(x, tokenizer, label2id), batched=True)
"""
tokenized_inputs = tokenizer(
df["text"],
padding="longest",
truncation=True,
max_length=512,
return_tensors="pt"
)
tokenized_inputs["label"] = [label2id.get(emotion, -1) for emotion in df["emotion"]]
return tokenized_inputs
# Convert the DataFrames to HuggingFace Dataset format
train_dataset = Dataset.from_pandas(train_df)
# Apply the 'preprocess_function' to tokenize text data and transform labels
train_dataset = train_dataset.map(lambda x: preprocess_function(x, label2id), batched=True)
Step 4: Model to redeem well
Next, I loaded the model of the Distilbert-trained Distilbert model for the separation of our text separation. I also explained that labels of this Dana looks like:
# Get the unique emotion labels from the 'emotion' column in the training DataFrame
labels = train_df["emotion"].unique()
# Create label-to-id and id-to-label mappings
label2id = {label: idx for idx, label in enumerate(labels)}
id2label = {idx: label for idx, label in enumerate(labels)}
# Initialize model
model = AutoModelForSequenceClassification.from_pretrained(
model_name,
num_labels=len(labels),
id2label=id2label,
label2id=label2id
)
The model of the front-trained Distilbert contains Five layers and partial headache.
To prevent excessive overdoing, I Enter the first four layerspreserves the information read during the previous training. This allows the model to keep the standard understanding of the language while organized only Fifth Arler and the head of separating to fit my data. Here's how I did this:
# freeze base model parameters
for name, param in model.base_model.named_parameters():
param.requires_grad = False
# keep classifier trainable
for name, param in model.base_model.named_parameters():
if "transformer.layer.5" in name or "classifier" in name:
param.requires_grad = True
Explaining Matterts
Since I was given the label uneven, I thought the accuracy may not be the correct metric, so I chose to replace the division of problems with accuracy, to remember, F1-Score, and AUC points.
I also used the “weight” in the F1-Score degree, accuracy, and remember to deal with an inequal class problem. The parameter ensures that all classes contribute accordingly in the metric and protect any one paragraph in regulating the results:
def compute_metrics(p):
"""
Computes accuracy, F1 score, precision, and recall metrics for multiclass classification.
Args:
p (tuple): Tuple containing predictions and labels.
Returns:
dict: Dictionary with accuracy, F1 score, precision, and recall metrics, using weighted averaging
to account for class imbalance in multiclass classification tasks.
"""
logits, labels = p
# Convert logits to probabilities using softmax (PyTorch)
softmax = torch.nn.Softmax(dim=1)
probs = softmax(torch.tensor(logits))
# Convert logits to predicted class labels
preds = probs.argmax(axis=1)
return {
"accuracy": accuracy_score(labels, preds), # Accuracy metric
"f1_score": f1_score(labels, preds, average="weighted"), # F1 score with weighted average for imbalanced data
"precision": precision_score(labels, preds, average="weighted"), # Precision score with weighted average
"recall": recall_score(labels, preds, average="weighted"), # Recall score with weighted average
"auc_score": roc_auc_score(labels, probs, average="macro", multi_class="ovr")
}
Let's set the training process:
# Define hyperparameters
lr = 2e-5
batch_size = 16
num_epochs = 3
weight_decay = 0.01
# Set up training arguments for fine-tuning models
training_args = TrainingArguments(
output_dir="./results",
evaluation_strategy="steps",
eval_steps=500,
learning_rate=lr,
per_device_train_batch_size=batch_size,
per_device_eval_batch_size=batch_size,
num_train_epochs=num_epochs,
weight_decay=weight_decay,
logging_dir="./logs",
logging_steps=500,
load_best_model_at_end=True,
metric_for_best_model="eval_f1_score",
greater_is_better=True,
)
# Initialize the Trainer with the model, arguments, and datasets
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=val_dataset,
tokenizer=tokenizer,
compute_metrics=compute_metrics,
)
# Train the model
print(f"Training {model_name}...")
trainer.train()
Step 5: Checking for a model performance
After training, I tested the performance of the model to the test set:
# Generate predictions on the test dataset with fine-tuned model
predictions_finetuned_model = trainer.predict(test_dataset)
preds_finetuned = predictions_finetuned_model.predictions.argmax(axis=1)
# Compute evaluation metrics (accuracy, precision, recall, and F1 score)
eval_results_finetuned_model = compute_metrics((predictions_finetuned_model.predictions, test_dataset["label"]))
This method of the correct Distilbert model is made from the test set compared to the previously trained model:
Before good repairs, the previously trained model is mistreated in our database, because they did not see labels in a certain soul before. It actually guessed randomly, as shown in AUC 0.5 points show that there is no better than the opportunity.
After good order, the model is very Upgrading to all MetricFinding 83% accuracy in sensitivity. This shows that the model has successfully learned purpose patterns in the data, even 16k training samples only.
It's amazing!
Step 6: Transacistration of predictions in state
I tested a well organized model in three sentences and the feelings predicted:
- “The thought of speaking in front of a great crowd did my heart race, and I start feeling anxious about worrying. “→ Fear 😱
- “I can't believe how disrespectfully failed! I worked hard to this project, and they just dismissed you without listening. It's angry!” → Anger 😡
- “I love the new phone! The camera quality is amazing, the battery lasts all day, and very fast. → DOY 😀
Impressively, is that?!
I wanted to understand how the model made its predictions, I used how to use Shaples (shaples that could not be explained) to see the importance of the feature.
I started to create an actor:
# Build a pipeline object for predictions
preds = pipeline(
"text-classification",
model=model_finetuned,
tokenizer=tokenizer,
return_all_scores=True,
)
# Create an explainer
explainer = shap.Explainer(preds)
After that, I included painful prices using the actor:
# Compute SHAP values using explainer
shap_values = explainer(example_texts)
# Make SHAP text plot
shap.plots.text(shap_values)
The plan below recognizes that each word for the text-in which the text affects the removal of the model using stated prices:
In this case, the plan shows that “anxiety” is the most important factor in predicting “fear” as compassionate.
The shaped text is a good, accurate way, and connects to understand the forecasts by breaking down how each word is the last prediction.
Summary
You have successfully learned to do the separation of emotions from the text data! (You can check the model in kissing face here).
TransformMer models may be well organized by many actual world programs, including:
- Customer service ticket tickets (as discussed in importance),
- To evoke the risk of mental health in conversations designed for the text,
- Finding feelings in product reviews.
Fine-Tuning is an efficient and effective way to synchronize trained models with job training potential services in small dataset.
What will you do well?
Want to build your AI skills?
👉🏻 I run AI WEKENDER including Write a weekly blog post on data science, AI projects for AI weekend, professional tips in the data.
Resources
- JYSTER NETEBOOK [HERE]
- Model card in face of face [HERE]



