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 which are predicted correctly3. 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 which we've already labelled 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 intent as a best guess. We call this nearest-neighbor classification.6. Nearest neighbor classification in scikit-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 spacy similarity function for this, but for later exercises with scikit-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 from 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.Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.