Intents and classification

Intents and classification

1. Intents and classification

Now that we've learned how to create vector representations of messages, we can use machine learning algorithms to recognize their intents.

2. Supervised learning

Let's introduce some machine learning terminology. Recognizing intents is an example of a classification problem. Given an input (that is, a user message) we want to create an object, a classifier, which can predict a label (that is, the intent of the message). A classifier usually has a number of tunable parameters. We tune these parameters using a training data set. This means that we tweak the parameters until the classifier predicts the training labels correctly. This process is called fitting. We then evaluate the quality of the model using a test data set. These are new messages which the classifier hasn't seen before, and we see if it can correctly predict the test labels. One way to report the quality of the classifier is its accuracy. Accuracy is the fraction of test labels that are predicted correctly

3. ATIS dataset

We will use the ATIS dataset, which contains thousands of messages from an airline booking service, with intent labels like `atis_flight`, `atis_airfare` indicating whether this was a flight search, question about price, etc.

4. ATIS dataset II

The training and test sentences are available in the lists sentences_train and test and their labels as labels_train and test. Let's create an array X_train containing the vector representation of all the test sentences. This should have as many rows as there are sentences in the training set, and as many columns as there are dimensions to our word vectors. We define this shape as a tuple called x train shape We initialize it with zeros, using the np dot zeros function with a single argument providing the shape as an argument. We then iterate over the sentences, and use the document's dot vector method to get a vector for the whole string, this simply takes an average of the vectors of the individual words.

5. Nearest neighbor classification

Now let's use word vectors to recognize intents. We will need some training data, e.g. sentences that we've already labeled with their corresponding intents. The simplest thing we can do to categorize a new sentence is to look for the labeled example that's the most similar and use it's intended as a best guess. We call this nearest-neighbor classification.

 

6. Nearest neighbor classification in sci-kit-learn

Scikit-learn provides a function for calculating the cosine similarity, so we can import it, and then calculate the similarity scores with all the training sentences by iterating over them. We could also use the space similarity function for this, but for later exercises with sci-kit-learn we will need to work with the training data in array format, so we will do that here for consistency. We create a list called scores, comparing each row of X with the vector of the test message. We can use the np dot argmax function to find the index of the largest value in the scores, and the train label with that index is then our guess for the intent of the test message. In this case, it was right, and in the exercise, you'll measure the effectiveness of this super simple approach.

 

7. Support vector machines

Nearest-neighbor classification works well for many simple domains, but you often want something a little more robust. Support Vector Machines are a tried-and-tested machine learning method, and the SV classifier works really well for classifying intents. We have already defined the X_train array, and the y_train array now contains integer labels corresponding to the different intents in the ATIS dataset. First, we import the support vector classifier using sklearn dot svm import SVC then we create a classifier object by calling SVC, and we call clf dot fit, passing it the training inputs X_train and the labels y_train. We can then predict the labels of the test set using the classifier's dot predict function, which takes the input X_test as its sole argument.

 

8. Let's practice!

In the exercises, you'll compare the accuracy of this classifier to the nearest neighbor approach.

Last modified: Friday, 17 February 2023, 12:00 PM