Spaces:
Runtime error
Runtime error
File size: 8,539 Bytes
de99c92 |
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 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### **r/place 2023 Sentiment Analysis Model**\n",
"This Jupyter notebook will fine-tune the DistilBERT model to perform sentiment analysis on Reddit comments in July 2023. Feel free to tweak the variables and code here. Credits are included at the end of the notebook."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Install Dependencies**<br>\n",
"This notebook has been tested on Python 3.11.2 and uses Pytorch."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import csv\n",
"import datasets\n",
"import pandas as pd\n",
"import sklearn\n",
"import torch"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Load the Data**<br>\n",
"The target CSV file has Reddit comments in Column 0 and a score in Column 1. The scores correspond to the following sentiments: -1 = negative, 0 = neutral, 1 = positive. We will tweak the range from [-1, 1] to [0, 2] to match the model's labels."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# define the data path and store the comments in a list\n",
"data_path = \"data/Reddit_Data.csv\"\n",
"comments_and_scores = []\n",
"\n",
"# read the csv and store each comment with its respective score\n",
"with open(data_path, \"r\", encoding=\"utf8\") as f:\n",
" csv_reader = csv.reader(f)\n",
" next(csv_reader)\n",
" for row in csv_reader:\n",
" comment, score = row\n",
" comments_and_scores.append((comment, int(score)+1))\n",
"\n",
"print(comments_and_scores[0])\n",
"print(len(comments_and_scores))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Separate Training and Testing Datasets**<br>\n",
"We need to separate these comments into training and testing datasets."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.model_selection import train_test_split\n",
"train_set, test_set = train_test_split(comments_and_scores,\n",
" test_size=0.2,\n",
" random_state=24)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(len(train_set))\n",
"print(len(test_set))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(train_set[0])\n",
"print(test_set[0])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# extract the training comments and scores\n",
"train_comments = [group[0] for group in train_set]\n",
"train_scores = [group[1] for group in train_set]\n",
"\n",
"# extract the testing comments and scores\n",
"test_comments = [group[0] for group in test_set]\n",
"test_scores = [group[1] for group in test_set]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(train_comments[0], train_scores[0])\n",
"print(test_comments[0], test_scores[0])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now that we have the training and testing datasets, we will convert them into Pandas DataFrame objects."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# extract the train set from the list\n",
"train_set = {\"text\": train_comments, \"labels\": train_scores}\n",
"train_set = pd.DataFrame(train_set)\n",
"train_set = datasets.Dataset.from_pandas(train_set)\n",
"print(train_set)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# extract the test set from the list\n",
"test_set = {\"text\": test_comments, \"labels\": test_scores}\n",
"test_set = pd.DataFrame(test_set)\n",
"test_set = datasets.Dataset.from_pandas(test_set)\n",
"print(test_set)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Tokenize the Data**<br>\n",
"Prior to training the model, we will tokenize the Reddit comments into small pieces to make it easier for the model to identify the comment's sentiment. Note: I disabled the warning for the fast tokenizer request as it will prevent the trainer from running the .train() function later in the notebook."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from transformers import DistilBertTokenizer\n",
"tokenizer = DistilBertTokenizer.from_pretrained(\"distilbert-base-uncased\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# prepare the text inputs for the model\n",
"def preprocess_function(examples):\n",
" return tokenizer(examples[\"text\"], truncation=True, max_length=128)\n",
"\n",
"tokenized_train = train_set.map(preprocess_function, batched=True)\n",
"tokenized_test = test_set.map(preprocess_function, batched=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Use data_collector to convert our samples to PyTorch tensors and concatenate them with the correct amount of padding\n",
"from transformers import DataCollatorWithPadding\n",
"data_collator = DataCollatorWithPadding(tokenizer=tokenizer)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Define DistilBERT as our base model:\n",
"from transformers import DistilBertForSequenceClassification\n",
"model = DistilBertForSequenceClassification.from_pretrained('distilbert-base-uncased', num_labels=3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.metrics import precision_recall_fscore_support\n",
"def compute_metrics(p):\n",
" preds = p.predictions.argmax(axis=1)\n",
" return {\n",
" 'precision': precision_recall_fscore_support(p.label_ids, preds, average='weighted')[0],\n",
" 'recall': precision_recall_fscore_support(p.label_ids, preds, average='weighted')[1],\n",
" 'f1': precision_recall_fscore_support(p.label_ids, preds, average='weighted')[2],\n",
" }"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Define a new Trainer with all the objects we constructed so far\n",
"from transformers import TrainingArguments, Trainer\n",
"\n",
"training_args = TrainingArguments(\n",
" output_dir='args/',\n",
" evaluation_strategy='epoch',\n",
" save_total_limit=2,\n",
" learning_rate=2e-5,\n",
" per_device_train_batch_size=16,\n",
" per_device_eval_batch_size=16,\n",
" num_train_epochs=3,\n",
" weight_decay=0.01,\n",
" logging_dir='logs/',\n",
")\n",
"\n",
"trainer = Trainer(\n",
" model=model,\n",
" args=training_args,\n",
" train_dataset=tokenized_train,\n",
" eval_dataset=tokenized_test,\n",
" tokenizer=tokenizer,\n",
" data_collator=data_collator,\n",
" compute_metrics=compute_metrics,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"trainer.train()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model.save_pretrained('saved_model/')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.11.2"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
|