File size: 5,155 Bytes
72adc39
 
7fa01c5
 
 
 
 
 
 
 
 
 
 
72adc39
7fa01c5
 
 
 
 
 
 
 
 
 
 
 
609f7cf
7fa01c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
609f7cf
 
 
7fa01c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
---
license: apache-2.0
base_model: distilbert-base-uncased
tags:
- text-classification
- sentiment-analysis
- fine-tuned
pipeline_tag: text-classification
language:
- en
metrics:
- accuracy
- f1_score
---

# Fine-Tuning DistilBERT for YouTube Comment Sentiment Analysis

Binary sentiment classification (Positive / Negative) of YouTube comments by fully fine-tuning `distilbert-base-uncased`. The video title is fed to the model together with the comment to give it the context needed to interpret ambiguous comments.

**Final test accuracy: 86.97% | Weighted F1: 0.87**

---

## Table of Contents

- [Overview](#overview)
- [Link to Github Repository](#link-to-github-repository)
- [Dataset](#dataset)
- [Preprocessing](#preprocessing)
- [Model](#model)
- [Training Configuration](#training-configuration)
- [Training Results](#training-results)
- [Test Results](#test-results)

---

## Overview

YouTube comments are informal, full of abbreviations and emojis, and often only make sense relative to the video they refer to. For example, "This is so sad!" can be positive (emotion about a moving song) or negative (reaction to bad news), depending on the video.

To address this, each input combines the comment with the video title, and a pre-trained transformer (DistilBERT) is fully fine-tuned on the resulting binary classification task.

## Link to Github Repository
https://github.com/Lberdy/YouTube-Comments-Sentiment-Analysis

## Dataset

English YouTube comments, each paired with the title of its video and a sentiment label. The original data had three classes (Positive, Negative, Neutral). The **Neutral class was removed** because it was ambiguous and noticeably degraded performance in early experiments.

| Property | Value |
|---|---|
| Total size | 60,000 examples (after removing Neutral) |
| Positive | 30,000 (50%) |
| Negative | 30,000 (50%) |
| Train split | 48,600 (81%) |
| Validation split | 5,400 (9%) |
| Test split | 6,000 (10%) |
| Model input | `FullText = comment + " [SEP] " + title` |

## Preprocessing

Cleaning is applied to both comments and titles:

- Convert emojis to descriptive text with the `emoji` library (e.g. 😊 β†’ `:smiling_face:`)
- Remove leftover HTML tags (regex)
- Remove URLs and `@user` mentions
- Normalize hashtags (`#word` β†’ `word`)
- Collapse multiple spaces and line breaks

Tokenization uses HuggingFace's `DistilBertTokenizerFast`, with truncation/padding to a maximum of 512 tokens. `[CLS]` and `[SEP]` special tokens are inserted automatically.

## Model

**DistilBERT** is a distilled version of BERT-base: 40% fewer parameters (110M β†’ 66M) while retaining about 97% of BERT's performance on GLUE.

| Property | Value |
|---|---|
| Base checkpoint | `distilbert-base-uncased` |
| Transformer layers | 6 |
| Attention heads per layer | 12 |
| Hidden size | 768 |
| Vocabulary | 30,522 (WordPiece) |
| Max sequence length | 512 |
| Pre-training data | English Wikipedia + BookCorpus |

**Strategy:** full fine-tuning. All pre-trained weights and a new linear classification head (2 output neurons) on top of the `[CLS]` token are updated during training.

## Training Configuration

| Hyperparameter | Value |
|---|---|
| Learning rate | 3e-5 (linear decay to 0) |
| Batch size (train / eval) | 32 / 32 |
| Epochs | 4 |
| Weight decay | 0.01 |
| Optimizer | AdamW (`adamw_torch_fused`) |
| Max length | 512 tokens (padding / truncation) |
| Precision | BF16 |
| DataLoader workers | 6 (with `pin_memory`) |
| Evaluation | Every epoch (`eval_strategy="epoch"`) |
| Best-model criterion | Weighted F1 (`load_best_model_at_end=True`) |
| Total steps | 6,076 (1,519 per epoch) |

## Training Results

| Epoch | Train Loss | Val Loss | Val Accuracy | Val F1 (weighted) | Steps |
|:---:|:---:|:---:|:---:|:---:|:---:|
| 1 | 0.311 | 0.3115 | 86.11% | 0.8607 | 1,519 |
| **2** β˜… | 0.224 | 0.3171 | 86.70% | **0.8667** | 3,038 |
| 3 | 0.119 | 0.4197 | 86.33% | 0.8633 | 4,557 |
| 4 | 0.076 | 0.5919 | 86.61% | 0.8661 | 6,076 |

β˜… Best checkpoint (step 3038), automatically restored by the HuggingFace `Trainer` for final evaluation.

**Overfitting:** training loss keeps decreasing, while validation loss rises after epoch 2 (0.3171 β†’ 0.4197 β†’ 0.5919). Validation accuracy stays stable around 86–87%, which suggests the model learns robust representations early and the later overfitting only marginally affects predictions. Selecting the best checkpoint by validation F1 mitigates this.

## Test Results

Evaluated on the held-out test set (6,000 examples: 3,000 Positive, 3,000 Negative) using the epoch-2 checkpoint.

| Metric | Value |
|---|---|
| Accuracy | **86.97%** |
| Macro F1 | 0.87 |
| Weighted F1 | 0.87 |

**Per-class metrics**

| Class | Precision | Recall | F1 |
|---|:---:|:---:|:---:|
| Negative (0) | 0.84 | 0.91 | 0.87 |
| Positive (1) | 0.90 | 0.83 | 0.86 |

**Confusion matrix**

| | Predicted Negative | Predicted Positive |
|---|:---:|:---:|
| **Actual Negative** | 2,736 | 264 |
| **Actual Positive** | 518 | 2,482 |

The model is slightly biased toward predicting Negative: high recall on negatives, but 518 positive comments are misclassified as negative.