File size: 11,210 Bytes
0b6b733
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "80f816c1-0839-41cb-847b-c79a62ca1465",
   "metadata": {},
   "source": [
    "### Load all required modules for loading data, model setup, training, and metric evaluation"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2554d05b-f08a-4c21-953f-4f507407e426",
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import os\n",
    "sys.path.append(os.path.abspath(os.path.join(os.getcwd(), \"..\", \"src\")))\n",
    "from data_loader import load_and_prepare_data         \n",
    "from model import get_model, get_tokenizer            \n",
    "from train import get_training_args, train_model      \n",
    "from evaluate import compute_metrics                  \n",
    "from torch.utils.data import Dataset                      \n",
    "import torch"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3bfbb706-4b0b-43de-a95a-884d46343668",
   "metadata": {},
   "source": [
    "### Define a class that wraps tokenized data and labels for Hugging Face’s Trainer to use"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c814c354-7962-4a2d-b7bd-5c498f1d004e",
   "metadata": {},
   "outputs": [],
   "source": [
    "class EmotionDataset(Dataset):\n",
    "    def __init__(self, encodings, labels):\n",
    "        self.encodings = encodings  # BERT tokenized inputs (input_ids, attention_mask)\n",
    "        self.labels = labels        # Encoded labels (integers)\n",
    "\n",
    "    def __len__(self):\n",
    "        return len(self.labels)     # Total number of samples\n",
    "\n",
    "    def __getitem__(self, idx):\n",
    "        # Return dictionary of input tensors + label tensor for a single sample\n",
    "        return {\n",
    "            key: torch.tensor(val[idx]) for key, val in self.encodings.items()\n",
    "        } | {\"labels\": torch.tensor(self.labels[idx])}"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9b87257-f0c0-4532-9eee-939d8747ef79",
   "metadata": {},
   "source": [
    "### Load the dataset from Hugging Face, clean and encode it, then tokenize it using the BERT tokenizer."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "18e312be-5863-4e24-900a-843e42e145cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load train/test splits and label encoder\n",
    "train_texts, test_texts, train_labels, test_labels, label_encoder = load_and_prepare_data()\n",
    "\n",
    "# Load BERT tokenizer\n",
    "tokenizer = get_tokenizer()\n",
    "\n",
    "# Tokenize training and testing texts with truncation and padding\n",
    "train_encodings = tokenizer(train_texts, truncation=True, padding=True, max_length=128)\n",
    "test_encodings = tokenizer(test_texts, truncation=True, padding=True, max_length=128)\n",
    "\n",
    "# Wrap the tokenized data into EmotionDataset objects\n",
    "train_dataset = EmotionDataset(train_encodings, train_labels)\n",
    "test_dataset = EmotionDataset(test_encodings, test_labels)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "66b99b4e-5297-4bc0-8cfb-20dbe22526c0",
   "metadata": {},
   "source": [
    "### Samples from the dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "35db4426-db21-4438-ba0e-ebb51d52edfb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Sample 1\n",
      "Text: i'd just feel less out of place, i guess. my sa makes me feel like i'm so behind my peers in terms of a social life\n",
      "Label (encoded): 9\n",
      "\n",
      "Sample 2\n",
      "Text: i love the lady in the green jacket chasing after the second car looking back at the first car like \"look what you did\"\n",
      "Label (encoded): 18\n",
      "\n",
      "Sample 3\n",
      "Text: man. really bad last possession there. bummer.\n",
      "Label (encoded): 10\n",
      "\n",
      "Sample 4\n",
      "Text: never would’ve guessed that one.\n",
      "Label (encoded): 20\n",
      "\n",
      "Sample 5\n",
      "Text: i wasn’t even expecting the reply that’s why i’m literally bamboozled.\n",
      "Label (encoded): 27\n",
      "\n"
     ]
    }
   ],
   "source": [
    "for i in range(5):\n",
    "    print(f\"Sample {i+1}\")\n",
    "    print(f\"Text: {train_texts[i]}\")\n",
    "    print(f\"Label (encoded): {train_labels[i]}\")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0883760a-a449-42ca-ba69-fa01d874e50b",
   "metadata": {},
   "source": [
    "### Set up the BERT model for sequence classification and define training parameters."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "3176ccf4-d20d-460c-b620-c73a1ab9cb6d",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Some weights of BertForSequenceClassification were not initialized from the model checkpoint at bert-base-uncased and are newly initialized: ['classifier.bias', 'classifier.weight']\n",
      "You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n",
      "/opt/anaconda3/lib/python3.12/site-packages/transformers/training_args.py:1545: FutureWarning: `evaluation_strategy` is deprecated and will be removed in version 4.46 of 🤗 Transformers. Use `eval_strategy` instead\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "# Load pre-trained BERT model with classification head for number of emotion classes\n",
    "model = get_model(num_labels=len(label_encoder.classes_))\n",
    "\n",
    "# Set training configuration: batch size, epochs, logging, saving, evaluation\n",
    "training_args = get_training_args()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "874a4e6a-80dd-470d-9283-e1c88e731b8e",
   "metadata": {},
   "source": [
    "### Train the Model "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "4c312e56-52bf-417d-82c0-8a1f47b82670",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "    <div>\n",
       "      \n",
       "      <progress value='5448' max='5448' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
       "      [5448/5448 1:46:28, Epoch 3/3]\n",
       "    </div>\n",
       "    <table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       " <tr style=\"text-align: left;\">\n",
       "      <th>Epoch</th>\n",
       "      <th>Training Loss</th>\n",
       "      <th>Validation Loss</th>\n",
       "      <th>Accuracy</th>\n",
       "      <th>F1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>1.358900</td>\n",
       "      <td>1.335635</td>\n",
       "      <td>0.613467</td>\n",
       "      <td>0.579882</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>0.947100</td>\n",
       "      <td>1.284574</td>\n",
       "      <td>0.615671</td>\n",
       "      <td>0.601428</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>0.970400</td>\n",
       "      <td>1.297894</td>\n",
       "      <td>0.617048</td>\n",
       "      <td>0.606042</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table><p>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "\n",
       "    <div>\n",
       "      \n",
       "      <progress value='5448' max='5448' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
       "      [5448/5448 1:35:20, Epoch 3/3]\n",
       "    </div>\n",
       "    <table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       " <tr style=\"text-align: left;\">\n",
       "      <th>Epoch</th>\n",
       "      <th>Training Loss</th>\n",
       "      <th>Validation Loss</th>\n",
       "      <th>Accuracy</th>\n",
       "      <th>F1</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <td>1</td>\n",
       "      <td>0.907200</td>\n",
       "      <td>1.365916</td>\n",
       "      <td>0.602313</td>\n",
       "      <td>0.595804</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>2</td>\n",
       "      <td>0.549100</td>\n",
       "      <td>1.488130</td>\n",
       "      <td>0.595566</td>\n",
       "      <td>0.591464</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <td>3</td>\n",
       "      <td>0.514400</td>\n",
       "      <td>1.593286</td>\n",
       "      <td>0.591297</td>\n",
       "      <td>0.589066</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table><p>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "TrainOutput(global_step=5448, training_loss=0.7054264770818002, metrics={'train_runtime': 5721.3012, 'train_samples_per_second': 15.23, 'train_steps_per_second': 0.952, 'total_flos': 5733080823638016.0, 'train_loss': 0.7054264770818002, 'epoch': 3.0})"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trainer = train_model(\n",
    "    model=model,\n",
    "    args=training_args,\n",
    "    train_dataset=train_dataset,\n",
    "    val_dataset=test_dataset,\n",
    "    compute_metrics=compute_metrics\n",
    ")\n",
    "\n",
    "# Begin training\n",
    "trainer.train()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "020729b6-c545-42ba-bd2c-00ee5f9bbb80",
   "metadata": {},
   "source": [
    "### Save both model weights and tokenizer files for future inference or deployment."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "5f12aedb-b3f8-4a1b-8e1f-6a68eb29933f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('../outputs/model/tokenizer_config.json',\n",
       " '../outputs/model/special_tokens_map.json',\n",
       " '../outputs/model/vocab.txt',\n",
       " '../outputs/model/added_tokens.json')"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "model_path = Path(\"..\") / \"outputs\" / \"model\"\n",
    "model.save_pretrained(model_path)\n",
    "tokenizer.save_pretrained(model_path)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}