|
1 | 1 | {
|
2 | 2 | "cells": [
|
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "## Intent Detection Using CNN & RNN" |
| 8 | + ] |
| 9 | + }, |
3 | 10 | {
|
4 | 11 | "cell_type": "markdown",
|
5 | 12 | "metadata": {
|
6 | 13 | "colab_type": "text",
|
7 | 14 | "id": "Wz0jLNgTnMwF"
|
8 | 15 | },
|
9 | 16 | "source": [
|
10 |
| - "In this notebook we will demonstrate various CNN and RNN models for the task of intent detection on the ATIS dataset. " |
| 17 | + "In this notebook we demonstrate various CNN and RNN architectures for the task of intent detection on the ATIS dataset. The ATIS dataset is a standard benchmark dataset for the tast of intent detection. ATIS Stands for Airline Travel Information System. The dataset can we found in the `Data2` folder under the `Data` folder." |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "markdown", |
| 22 | + "metadata": {}, |
| 23 | + "source": [ |
| 24 | + "## Imports" |
11 | 25 | ]
|
12 | 26 | },
|
13 | 27 | {
|
|
33 | 47 | }
|
34 | 48 | ],
|
35 | 49 | "source": [
|
36 |
| - "\n", |
37 |
| - "#making the necessary imports\n", |
| 50 | + "#general imports\n", |
38 | 51 | "import os\n",
|
39 | 52 | "import sys\n",
|
| 53 | + "import random\n", |
| 54 | + "random.seed(0) #for reproducability of results\n", |
| 55 | + "\n", |
| 56 | + "#basic imports\n", |
40 | 57 | "import numpy as np\n",
|
| 58 | + "import pandas as pd\n", |
| 59 | + "\n", |
| 60 | + "\n", |
| 61 | + "#NN imports\n", |
41 | 62 | "from keras.preprocessing.text import Tokenizer\n",
|
42 | 63 | "from keras.preprocessing.sequence import pad_sequences\n",
|
43 | 64 | "from keras.utils import to_categorical\n",
|
|
46 | 67 | "from keras.models import Model, Sequential\n",
|
47 | 68 | "from keras.initializers import Constant\n",
|
48 | 69 | "\n",
|
| 70 | + "#encoder\n", |
49 | 71 | "from sklearn.preprocessing import LabelEncoder\n",
|
50 | 72 | "\n",
|
51 |
| - "import random\n", |
52 |
| - "random.seed(0)#for reproducability of results\n", |
53 |
| - "\n", |
54 |
| - "import pandas as pd\n", |
55 |
| - "import numpy as np" |
| 73 | + "\n" |
56 | 74 | ]
|
57 | 75 | },
|
58 | 76 | {
|
|
62 | 80 | "id": "LGFhMpnjo4L1"
|
63 | 81 | },
|
64 | 82 | "source": [
|
65 |
| - "Loading the training data" |
| 83 | + "## Data Loading\n", |
| 84 | + "We load the data with the help of a few functions from `utils.py` which is included in this repository's Ch6 folder under folder name \"Data\". \n", |
| 85 | + "### Training Data\n" |
66 | 86 | ]
|
67 | 87 | },
|
68 | 88 | {
|
|
94 | 114 | }
|
95 | 115 | ],
|
96 | 116 | "source": [
|
97 |
| - "#utils is included in this repository'c Ch6 folder under folder name \"Data\"\n", |
98 | 117 | "from Data.utils import fetch_data, read_method\n",
|
99 | 118 | "\n",
|
100 | 119 | "sents,labels,intents = fetch_data('Data/data2/atis.train.w-intent.iob')\n",
|
|
128 | 147 | "id": "qckNEPKRo8_V"
|
129 | 148 | },
|
130 | 149 | "source": [
|
131 |
| - "Loading the test data" |
| 150 | + "### Testing Data" |
132 | 151 | ]
|
133 | 152 | },
|
134 | 153 | {
|
|
200 | 219 | "id": "nUZsI3ZmpBA2"
|
201 | 220 | },
|
202 | 221 | "source": [
|
203 |
| - "Pre-Processing" |
| 222 | + "## Data Preprocessing" |
204 | 223 | ]
|
205 | 224 | },
|
206 | 225 | {
|
|
311 | 330 | "print('Splitting the train data into train and valid is done')"
|
312 | 331 | ]
|
313 | 332 | },
|
| 333 | + { |
| 334 | + "cell_type": "markdown", |
| 335 | + "metadata": {}, |
| 336 | + "source": [ |
| 337 | + "## Modeling\n", |
| 338 | + "### Embedding Matrix\n", |
| 339 | + "We need to prepare our embedding." |
| 340 | + ] |
| 341 | + }, |
314 | 342 | {
|
315 | 343 | "cell_type": "code",
|
316 | 344 | "execution_count": 12,
|
|
377 | 405 | "print(\"Preparing of embedding matrix is done\")"
|
378 | 406 | ]
|
379 | 407 | },
|
| 408 | + { |
| 409 | + "cell_type": "markdown", |
| 410 | + "metadata": {}, |
| 411 | + "source": [ |
| 412 | + "### CNN with Pre-Trained Embeddings" |
| 413 | + ] |
| 414 | + }, |
380 | 415 | {
|
381 | 416 | "cell_type": "code",
|
382 | 417 | "execution_count": 13,
|
|
457 | 492 | "print('Test accuracy with CNN:', acc)"
|
458 | 493 | ]
|
459 | 494 | },
|
| 495 | + { |
| 496 | + "cell_type": "markdown", |
| 497 | + "metadata": {}, |
| 498 | + "source": [ |
| 499 | + "### CNN-Embedding Layer\n", |
| 500 | + "Here, we train a CNN model with an embedding layer which is being trained on the fly instead of using the pre-trained embeddings." |
| 501 | + ] |
| 502 | + }, |
460 | 503 | {
|
461 | 504 | "cell_type": "code",
|
462 | 505 | "execution_count": 14,
|
|
536 | 579 | "print('Test accuracy with CNN:', acc)\n"
|
537 | 580 | ]
|
538 | 581 | },
|
| 582 | + { |
| 583 | + "cell_type": "markdown", |
| 584 | + "metadata": {}, |
| 585 | + "source": [ |
| 586 | + "### RNN-Embedding Layer\n", |
| 587 | + "Here, we train a RNN model with an embedding layer which is being trained on the fly instead of using the pre-trained embeddings." |
| 588 | + ] |
| 589 | + }, |
539 | 590 | {
|
540 | 591 | "cell_type": "code",
|
541 | 592 | "execution_count": 15,
|
|
601 | 652 | "print('Test accuracy with RNN:', acc)\n"
|
602 | 653 | ]
|
603 | 654 | },
|
| 655 | + { |
| 656 | + "cell_type": "markdown", |
| 657 | + "metadata": {}, |
| 658 | + "source": [ |
| 659 | + "### LSTM with Pre-Trained Embeddings" |
| 660 | + ] |
| 661 | + }, |
604 | 662 | {
|
605 | 663 | "cell_type": "code",
|
606 | 664 | "execution_count": 16,
|
|
698 | 756 | "name": "python",
|
699 | 757 | "nbconvert_exporter": "python",
|
700 | 758 | "pygments_lexer": "ipython3",
|
701 |
| - "version": "3.8.3" |
| 759 | + "version": "3.6.12" |
702 | 760 | },
|
703 | 761 | "toc": {
|
704 | 762 | "base_numbering": 1,
|
|
0 commit comments