The Classify Endpoint
In this chapter you'll learn how to classify a small dataset of sentences by their sentiment (positive, negative, or neutral), using Cohere's Classify endpoint
For example, a human can easily tell you that “Hello, World! What a beautiful day” conveys a positive sentiment, but let’s see if our models can do that too. And while we’re at it, let’s try classifying other phrases that you might find on social media.
Colab Notebook
This chapter comes with a corresponding notebook, and we encourage you to follow it along as you read the chapter.
For the setup, please refer to the Setting Up chapter at the beginning of this module.
Prepare Input
A typical machine learning model requires many training examples to perform text classification, but with the Classify endpoint, you can get started with as few as five examples per class. With the Classify endpoint, the input you need to prepare is as follows:
Examples
- These are the training examples we give the model to show the output we want it to generate.
- Each example contains the text itself and the corresponding label, or class.
- The minimum number of examples required is two per class.
- You can have as many classes as possible. If you are classifying text into two classes, that means you need a minimum of four examples, and if you have three, that means you need six examples, and so on.
Inputs
- These are the list of text pieces you’d like to classify.
Our sentiment analysis classifier has three classes with five examples each: “Positive” for a positive sentiment, “Negative” for a negative sentiment, and “Neutral” for a neutral sentiment. The code looks as follows.
The examples:
The inputs (we have twelve in this example):
Get output
With the Classify endpoint, setting up the model is quite straightforward. The main thing to do is to define the model type. For the Classify endpoint, we need to use an embedding model, which we’ll useembed-english-v3.0
(we’ll learn more about embedding models in the next chapter).
Putting everything together with the Classify endpoint looks like the following:
Together with the predicted class, the endpoint also returns the confidence value of the prediction (between 0 and 1). These confidence values are split among the classes, in this case three, in which the values add up to a total of 1. The classifier then selects the class with the highest confidence value as the “predicted class.” A high confidence value for the predicted class therefore indicates that the model is very confident of its prediction, and vice versa.
Here’s a sample output returned:
The model returned a Positive sentiment for “Hello, world! What a beautiful day,” which is what we would expect! And the predictions for all the rest look spot on too.
That was one example, but you can classify any kind of text into any number of possible classes according to your needs.
As your task gets more complex, you will likely need to bring in additional training data and finetune a model. This will ensure that the model can capture the nuances specific to your task and realize performance gains. You can read more about finetuning representation models in one of the following chapters.
If you’d like to learn more about text classification, here are some additional resources:
- Intuition and use case examples
- An example application: toxicity detection
- Evaluating a classifier’s performance
- Classify endpoint API reference
Conclusion
In this chapter, you’ve learned to classify text based on mood using Cohere’s Classify
endpoint. Follow along the next chapters to learn other endpoints and how to use them for many other tasks!
Original Source
This material comes from the post Hello, World! Meet Language AI: Part 2