Intent Classification using LSTM

Building a Deep Learning Model for Intent Classification

Photo by Jakub Jezovic 🩔 on Dribbble

What is intent?

Intent is the aim behind a specific action or set of actions.

Aim

To develop a Deep Learning Model for Intent Classification using Python programming Language and Keras on Cainvas Platform.

Prerequisites

Before getting started, you should have a good understanding of:

  1. Python programming language
  2. Keras — Deep learning library

Dataset

The CSV file that we are going to use can be directly downloaded through this link: https://cainvas-static.s3.amazonaws.com/media/user_data/vomchaithany/train.csv

Get the data file

https://gist.github.com/omchaithanyav/5e2fc3b1f0c1dadcb63b12391613919d

out:

--2021-07-11 14:09:45--  https://cainvas-static.s3.amazonaws.com/media/user_data/vomchaithany/train.csv
Resolving cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)... 52.219.66.48
Connecting to cainvas-static.s3.amazonaws.com (cainvas-static.s3.amazonaws.com)|52.219.66.48|:443... connected.
HTTP request sent, awaiting response... 304 Not Modified
File ‘train.csv’ not modified on server. Omitting download.

Import required libraries

https://gist.github.com/omchaithanyav/391f1fb96ab9502054f0f7d4a44f1b0e

Load the data

https://gist.github.com/omchaithanyav/ad9b827b33e76031a84f72cac6482bcc

out:

Let’s shuffle the data

https://gist.github.com/omchaithanyav/f40ad0b940344f612ad2e3656ec26118

out:

The rows got shuffled

https://gist.github.com/omchaithanyav/8ae5968bc3e2568c8fb3b2a274744ec6

out:

((7929,), (7929, 4))

https://gist.github.com/omchaithanyav/aeec0717f6b08748bc39f48aa3c9633f

out:

(\'In NH will it be chillier in West York at noon\', array([0, 1, 0, 0]))

Preprocessing

https://gist.github.com/omchaithanyav/c5b648c057208e45e75639bbf4b20115

out:

((7929,), (7929, 4))

https://gist.github.com/omchaithanyav/9bae2682fc053e1a281b8ec83d74e941

out:

((5729, 4000), (2200, 4000))

https://gist.github.com/omchaithanyav/b8c2d75b3a49c7a1814bc3e79bf1e84b

out:

(array([[   0,    0,    0, ...,    5,   11,   32],
[ 0, 0, 0, ..., 350, 977, 614],
[ 0, 0, 0, ..., 9, 4070, 2134],
...,
[ 0, 0, 0, ..., 4, 5446, 493],
[ 0, 0, 0, ..., 503, 562, 281],
[ 0, 0, 0, ..., 29, 4, 5447]], dtype=int32),
array([[ 0, 0, 0, ..., 5, 11, 32],
[ 0, 0, 0, ..., 350, 977, 614],
[ 0, 0, 0, ..., 9, 4070, 2134],
...,
[ 0, 0, 0, ..., 97, 195, 265],
[ 0, 0, 0, ..., 72, 5, 95],
[ 0, 0, 0, ..., 2294, 19, 10]], dtype=int32))

Build and Train the Model

https://gist.github.com/omchaithanyav/db80b4b582000ebc7710577d478a4064

out:

Model: "sequential_9"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding_9 (Embedding) (None, 4000, 32) 240704
_________________________________________________________________
lstm_9 (LSTM) (None, 10) 1720
_________________________________________________________________
dropout_27 (Dropout) (None, 10) 0
_________________________________________________________________
dense_27 (Dense) (None, 800) 8800
_________________________________________________________________
dropout_28 (Dropout) (None, 800) 0
_________________________________________________________________
dense_28 (Dense) (None, 200) 160200
_________________________________________________________________
dropout_29 (Dropout) (None, 200) 0
_________________________________________________________________
dense_29 (Dense) (None, 4) 804
=================================================================
Total params: 412,228
Trainable params: 412,228
Non-trainable params: 0
_________________________________________________________________

https://gist.github.com/omchaithanyav/eb2eaec63cd852643ebd15cd795c2c42

out:

Epoch 1/5
180/180 [==============================] - 19s 106ms/step - loss: 0.3748 - accuracy: 0.8284 - val_loss: 0.0190 - val_accuracy: 0.9945
Epoch 2/5
180/180 [==============================] - 19s 103ms/step - loss: 0.0857 - accuracy: 0.9672 - val_loss: 0.0049 - val_accuracy: 0.9991
Epoch 3/5
180/180 [==============================] - 19s 103ms/step - loss: 0.0665 - accuracy: 0.9738 - val_loss: 0.0033 - val_accuracy: 0.9995
Epoch 4/5
180/180 [==============================] - 19s 103ms/step - loss: 0.0512 - accuracy: 0.9808 - val_loss: 0.0024 - val_accuracy: 0.9991
Epoch 5/5
180/180 [==============================] - 19s 103ms/step - loss: 0.0444 - accuracy: 0.9831 - val_loss: 8.6375e-04 - val_accuracy: 0.9995

Save the model

https://gist.github.com/omchaithanyav/c1c31b3122b442784b17fd1f58a8f602

Plots

Loss vs Validation Loss

https://gist.github.com/omchaithanyav/84bfbdaeccc50809f816b82d8f0bda79

out:

Accuracy vs Validation Accuracy

https://gist.github.com/omchaithanyav/ec287d889468db10a4fb89d01ed0504a

out:

Loss and Accuracy

https://gist.github.com/omchaithanyav/3824d25d853773518ca6583d9c2b856a

out:

69/69 [==============================] - 3s 41ms/step - loss: 8.6375e-04 - accuracy: 0.9995
[0.0008637462160550058, 0.9995454549789429]

Custom Predictions

https://gist.github.com/omchaithanyav/800cf14c8ccc150bb763c8dfee776e11

out:

\'BookRestaurant\'

https://gist.github.com/omchaithanyav/8b57a048f6a5bbc84f018bf5cd332451

out:

\'GetWeather\'

https://gist.github.com/omchaithanyav/815403723d19cfbfa0bd250f26dee2b8

out:

\'RateBook\'

https://gist.github.com/omchaithanyav/5f5fc08a88004623cf4eace4c728bd03

out:

\'PlayMusic\'

Cainvas Gallery: https://cainvas.ai-tech.systems/gallery/

Conclusion

We’ve trained our LSTM model on the Intent Classification dataset and got an accuracy of 99%.

Notebook Link: Here

Credit: Om Chaithanya V