Concepts / Text Classification with Machine Learning

Text Classification with Machine Learning

Check the shape of the desired output first. If the answer is one of a few predefined categories, a classification model fits. If the answer is itself a passage of text, the model must be able to produce text.

  • Programming

When the Answer Is a Passage

Much of the machine learning work you have seen so far ends with a single decision: this email is spam, this image shows a cat, this customer will leave. The model's answer is a label chosen from a fixed set. Many language problems do not fit that pattern. If you want a sentence rewritten in another language, or a long article shortened to a few lines, the answer you need is itself a passage of text, and its length is not fixed in advance.

What do you think happens?

If you want a sentence translated into another language, what kind of output must the model produce?

  • One label chosen from a fixed set of categories

  • A passage of text whose length is not fixed in advance

Reveal answer
Answer:

A passage of text whose length is not fixed in advance

A translation is itself a sequence of words, so the model has to write text rather than pick a category. This is the kind of output recurrent neural networks can produce.

This article looks at a family of neural networks designed for exactly that situation. Before reading on, keep one question in mind: for a given task, does the model need to choose a category, or does it need to write new text?

Two Kinds of Model Output

There are two kinds of model to keep apart. One kind takes an input and returns one of several predefined categories. The other kind takes an input and produces a new piece of text as its output. A classification model belongs to the first kind: it can only assign class labels. A recurrent neural network, usually abbreviated RNN, belongs to the second kind. RNNs are described as a particularly powerful type of neural network, and their defining feature for this topic is the kind of output they can produce: the output can be text again.

EmailinputSpamone fixed categoryEnglish sentenceinputFrench sentencea passage of textassigns a class labelproduces text as output
A classifier maps an input to one category from a fixed set, while an RNN can produce a new piece of text as its output.

The contrast is about the shape of the answer. In the classifier's case the answer is one entry from a list decided in advance. In the RNN's case the answer is text, which means a sequence of words. Because the desired answer is a sequence of words in tasks such as automatic translation and automatic summarization, an RNN is well suited to them.

Key point

Check the shape of the desired output first. If the answer is one of a few predefined categories, a classification model fits. If the answer is itself a passage of text, the model must be able to produce text.

Tracing an Output That Grows

A label-only model returns its whole answer in one decision. A text-producing model returns an answer that is a sequence, and the source notes that the length of that sequence is not fixed in advance. To see what this means, trace a short output one word at a time. The example below is an illustration of the general idea, not a description of any particular trained model.

Thefirst wordcatsecond wordsatthird worddownoutput continuesnext wordnext wordnext word
The output is a sequence of words, and the number of words is not fixed in advance, so the sequence can stop at different points for different inputs.

Read the timeline as a sequence of decisions about what comes next. Each word is a piece of the answer, and the answer keeps growing until the text is complete. A classifier never has this stage: it makes one choice and stops. Keeping this difference in view explains why the RNN, rather than a classifier, is the family the source connects to translation and summarization.

Translation and Summarization as Text-to-Text Tasks

Translation and summarization are the two text-processing tasks the source names as well suited to RNNs. Both take text as input and return text as output. The diagram below shows the flow for each. In both chains the model sits in the middle, and what changes is the meaning of the output: a sentence in another language in the first case, and a shorter version of a longer text in the second.

Input sentencetext inRNNtext-producing modelTranslated texttext outLong articletext inRNNtext-producing modelShort summarytext outinputoutputinputoutput
Translation and summarization both pass input text through a model and return output text; they differ in what the output is meant to be.
Worked example

Choosing a model family for a new task

A team wants to rewrite customer messages in another language. Should they reach for a classification model or a text-producing model such as an RNN?

  1. 1
    Identify the input

    The input is a customer message, which is text.

  2. 2
    Identify the desired output

    The desired answer is a rewritten message in another language. That answer is itself a passage of text, and its length is not fixed in advance.

  3. 3
    Match the output shape

    A classification model can only assign class labels from a fixed set, so it cannot return the rewritten message. Translation is one of the text-processing tasks the source connects to RNNs, because the desired answer is a sequence of words.

  4. 4
    Decide

    The team should choose a text-producing model such as an RNN. This decision was made by checking input and output shapes, before any training code was written.

Result

A text-producing model such as an RNN fits the translation task; a classification model does not.

The worked example did not require knowing how the internal computation of an RNN works. It only required checking the shape of the input and the shape of the desired output. The same check applies to summarization: a long article goes in, and a shorter text comes out, so the task calls for text as output.

Mistakes Beginners Make with Model Choice

Learners new to this topic tend to slip into a few predictable errors. The items below describe each one and how to correct it.

  • Common mistake

    Treating every language problem as a classification problem

    What it looks like

    Setting up a classifier to produce a translated sentence by choosing from a list of fixed sentences.

    Why it fails

    A classifier can only assign one of several predefined categories. A translation is a new passage whose length is not fixed in advance.

    Do this instead

    Ask what the answer looks like. If it is itself text, use a model that can produce text, such as an RNN.

  • Common mistake

    Assuming the output of a text task has a fixed length

    What it looks like

    Expecting a summary to always be exactly three lines because the example shown had three lines.

    Why it fails

    The source states that the length of the output passage is not fixed in advance.

    Do this instead

    Design around a variable-length output, and do not treat one sample output as the required size.

  • Common mistake

    Thinking the model choice requires understanding every internal detail first

    Why it fails

    The source's reasoning about model choice only required checking the shape of the input and the desired output.

    Do this instead

    Start with the input and output shapes. Internal details become important later, when you study RNNs in depth.

Practice Choosing Output Shape

EASY

For each task below, decide whether the model should output a label from a fixed set or output a passage of text: (a) sorting a message as urgent or not urgent, (b) turning a paragraph into a shorter paragraph, (c) deciding whether a photo shows a dog. Then explain your reason for each in terms of the shape of the desired output.

Hints
  • Ask whether the answer could be written as one of a few predefined categories.
  • A task whose answer is a new piece of text, with a length not fixed in advance, calls for text as output.
  • Tasks (b) is a summarization-style task, and the source connects summarization to text-producing RNNs.

Where to Go Next

The source offers two directions for further study. Read the paper for the underlying ideas and the tutorial for hands-on implementation.

  • Paper for the underlying ideas: 'Sequence to Sequence Learning with Neural Networks' by Ilya Sutskever, Oriol Vinyals, and Quoc Le. The source describes it as a relatively technical paper that introduces the topic.
  • Tutorial for hands-on practice: a practical tutorial on using RNNs with the TensorFlow framework, available on the TensorFlow website.

A sensible order is to first confirm the output-shape reasoning from this article, then read the paper for the ideas behind text-producing networks, and then work through the TensorFlow tutorial to build one yourself.

Key Takeaways

  • A classification model can only assign class labels from a predefined set, while an RNN can produce text as its output.
  • Translation and summarization call for text as output, because the desired answer is a sequence of words whose length is not fixed in advance.
  • Choosing a model family can start by checking the shape of the input and the desired output, before any training code is written.
  • For deeper study, read the Sequence to Sequence Learning paper for the ideas and the TensorFlow RNN tutorial for hands-on implementation.

Related concepts

More concepts starting with T