File size: 11,633 Bytes
733c188 |
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 |
{
"cells": [
{
"cell_type": "raw",
"metadata": {},
"source": [
"---\n",
"title: 08 Bag of Words Text Classifier\n",
"description: Build a simple bag of words text classifier.\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://colab.research.google.com/drive/19suDts9MNIhx0TeGO26_BIY2Xc0n6DBC?usp=sharing\" target=\"_blank\"><img align=\"left\" alt=\"Colab\" title=\"Open in Colab\" src=\"https://colab.research.google.com/assets/colab-badge.svg\"></a>"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "OP_uXHGK0Q9d"
},
"source": [
"# Bag of Words Text Classifier\n",
"\n",
"The code below implements a simple bag of words text classifier.\n",
"- We tokenize the text, create a vocabulary and encode each piece of text in the dataset\n",
"- The lookup allows for extracting embeddings for each tokenized inputs\n",
"- The embedding vectors are added together with a bias vector\n",
"- The resulting vector is referred to as the scores\n",
"- The score are applied a softmax to generate probabilities which are used for the classification task\n",
"\n",
"The code used in this notebook was inspired by code from the [official repo](https://github.com/neubig/nn4nlp-code) used in the [CMU Neural Networks for NLP class](http://www.phontron.com/class/nn4nlp2021/schedule.html) by [Graham Neubig](http://www.phontron.com/index.php). \n",
"\n",
"![img txt](https://github.com/dair-ai/ML-Notebooks/blob/main/img/bow.png?raw=true)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "rYJ7PiaO2R6Q"
},
"outputs": [],
"source": [
"import torch\n",
"import random\n",
"import torch.nn as nn"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "M3eH6PyS1Ykz"
},
"source": [
"### Download the Data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "F_lDByee1ddU"
},
"outputs": [],
"source": [
"%%capture\n",
"\n",
"# download the files\n",
"!wget https://raw.githubusercontent.com/neubig/nn4nlp-code/master/data/classes/dev.txt\n",
"!wget https://raw.githubusercontent.com/neubig/nn4nlp-code/master/data/classes/test.txt\n",
"!wget https://raw.githubusercontent.com/neubig/nn4nlp-code/master/data/classes/train.txt\n",
"\n",
"# create the data folders\n",
"!mkdir data data/classes\n",
"!cp dev.txt data/classes\n",
"!cp test.txt data/classes\n",
"!cp train.txt data/classes"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "G9gihHeo0dK6"
},
"source": [
"### Read the Data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "YOYzmcLdzD8i"
},
"outputs": [],
"source": [
"# function to read in data, process each line and split columns by \" ||| \"\n",
"def read_data(filename):\n",
" data = []\n",
" with open(filename, 'r') as f:\n",
" for line in f:\n",
" line = line.lower().strip()\n",
" line = line.split(' ||| ')\n",
" data.append(line)\n",
" return data\n",
"\n",
"train_data = read_data('data/classes/train.txt')\n",
"test_data = read_data('data/classes/test.txt')"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "WEIAf06u2kZz"
},
"source": [
"### Contruct the Vocab and Datasets"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "9MJHDqjT2qDu"
},
"outputs": [],
"source": [
"# creating the word and tag indices\n",
"word_to_index = {}\n",
"word_to_index[\"<unk>\"] = len(word_to_index) # adds <UNK> to dictionary\n",
"tag_to_index = {}\n",
"\n",
"# create word to index dictionary and tag to index dictionary from data\n",
"def create_dict(data, check_unk=False):\n",
" for line in data:\n",
" for word in line[1].split(\" \"):\n",
" if check_unk == False:\n",
" if word not in word_to_index:\n",
" word_to_index[word] = len(word_to_index)\n",
" else:\n",
" if word not in word_to_index:\n",
" word_to_index[word] = word_to_index[\"<unk>\"]\n",
"\n",
" if line[0] not in tag_to_index:\n",
" tag_to_index[line[0]] = len(tag_to_index)\n",
"\n",
"create_dict(train_data)\n",
"create_dict(test_data, check_unk=True)\n",
"\n",
"# create word and tag tensors from data\n",
"def create_tensor(data):\n",
" for line in data:\n",
" yield([word_to_index[word] for word in line[1].split(\" \")], tag_to_index[line[0]])\n",
"\n",
"train_data = list(create_tensor(train_data))\n",
"test_data = list(create_tensor(test_data))\n",
"\n",
"number_of_words = len(word_to_index)\n",
"number_of_tags = len(tag_to_index)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "n-4FU9Ab2McP"
},
"source": [
"### Model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "Zt76PIzP0jWn"
},
"outputs": [],
"source": [
"# cpu or gpu\n",
"device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
"\n",
"# create a simple neural network with embedding layer, bias, and xavier initialization\n",
"class BoW(torch.nn.Module):\n",
" def __init__(self, nwords, ntags):\n",
" super(BoW, self).__init__()\n",
" self.embedding = nn.Embedding(nwords, ntags)\n",
" nn.init.xavier_uniform_(self.embedding.weight)\n",
"\n",
" type = torch.cuda.FloatTensor if torch.cuda.is_available() else torch.FloatTensor\n",
" self.bias = torch.zeros(ntags, requires_grad=True).type(type)\n",
"\n",
" def forward(self, x):\n",
" emb = self.embedding(x) # seq_len x ntags (for each seq) \n",
" out = torch.sum(emb, dim=0) + self.bias # ntags\n",
" out = out.view(1, -1) # reshape to (1, ntags)\n",
" return out"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Mi4FNOy02Z1t"
},
"source": [
"### Pretest the Model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "pn_LCZJv2Osz",
"outputId": "2c83bb22-a7e8-40af-cb1b-c04f3de6bd38"
},
"outputs": [
{
"data": {
"text/plain": [
"tensor([[-0.0108, -0.0067, -0.0260, -0.0255, 0.0119]], device='cuda:0',\n",
" grad_fn=<ViewBackward0>)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# function to convert sentence into tensor using word_to_index dictionary\n",
"def sentence_to_tensor(sentence):\n",
" return torch.LongTensor([word_to_index[word] for word in sentence.split(\" \")])\n",
"\n",
"# test the sentence_to_tensor function\n",
"type = torch.cuda.LongTensor if torch.cuda.is_available() else torch.LongTensor\n",
"out = sentence_to_tensor(\"i love dogs\").type(type)\n",
"test_model = BoW(number_of_words, number_of_tags).to(device)\n",
"test_model(out)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "SH5r2Xzs21zB"
},
"source": [
"### Train the Model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "f86xjDAi2bt8",
"outputId": "c329b5b2-6d09-405c-bca9-6066e3415c18"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ITER: 1 | train loss/sent: 1.4746 | train accuracy: 0.3661 | test accuracy: 0.3977\n",
"ITER: 2 | train loss/sent: 1.1221 | train accuracy: 0.6023 | test accuracy: 0.4149\n",
"ITER: 3 | train loss/sent: 0.9114 | train accuracy: 0.7124 | test accuracy: 0.4072\n",
"ITER: 4 | train loss/sent: 0.7681 | train accuracy: 0.7684 | test accuracy: 0.4063\n",
"ITER: 5 | train loss/sent: 0.6629 | train accuracy: 0.8069 | test accuracy: 0.4081\n",
"ITER: 6 | train loss/sent: 0.5802 | train accuracy: 0.8331 | test accuracy: 0.4023\n",
"ITER: 7 | train loss/sent: 0.5167 | train accuracy: 0.8549 | test accuracy: 0.4100\n",
"ITER: 8 | train loss/sent: 0.4632 | train accuracy: 0.8683 | test accuracy: 0.4072\n",
"ITER: 9 | train loss/sent: 0.4187 | train accuracy: 0.8838 | test accuracy: 0.3986\n",
"ITER: 10 | train loss/sent: 0.3802 | train accuracy: 0.8954 | test accuracy: 0.3973\n"
]
}
],
"source": [
"# train and test the BoW model\n",
"model = BoW(number_of_words, number_of_tags).to(device)\n",
"criterion = nn.CrossEntropyLoss()\n",
"optimizer = torch.optim.Adam(model.parameters())\n",
"type = torch.LongTensor\n",
"\n",
"if torch.cuda.is_available():\n",
" model.to(device)\n",
" type = torch.cuda.LongTensor\n",
"\n",
"# perform training of the Bow model\n",
"def train_bow(model, optimizer, criterion, train_data):\n",
" for ITER in range(10):\n",
" # perform training\n",
" model.train()\n",
" random.shuffle(train_data)\n",
" total_loss = 0.0\n",
" train_correct = 0\n",
" for sentence, tag in train_data:\n",
" sentence = torch.tensor(sentence).type(type)\n",
" tag = torch.tensor([tag]).type(type)\n",
" output = model(sentence)\n",
" predicted = torch.argmax(output.data.detach()).item()\n",
" \n",
" loss = criterion(output, tag)\n",
" total_loss += loss.item()\n",
"\n",
" optimizer.zero_grad()\n",
" loss.backward()\n",
" optimizer.step()\n",
"\n",
" if predicted == tag: train_correct+=1\n",
"\n",
" # perform testing of the model\n",
" model.eval()\n",
" test_correct = 0\n",
" for sentence, tag in test_data:\n",
" sentence = torch.tensor(sentence).type(type)\n",
" output = model(sentence)\n",
" predicted = torch.argmax(output.data.detach()).item()\n",
" if predicted == tag: test_correct += 1\n",
" \n",
" # print model performance results\n",
" log = f'ITER: {ITER+1} | ' \\\n",
" f'train loss/sent: {total_loss/len(train_data):.4f} | ' \\\n",
" f'train accuracy: {train_correct/len(train_data):.4f} | ' \\\n",
" f'test accuracy: {test_correct/len(test_data):.4f}'\n",
" print(log)\n",
"\n",
"# call the train_bow function\n",
"train_bow(model, optimizer, criterion, train_data)"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"name": "bow.ipynb",
"provenance": []
},
"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.9.12"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
|