- Introduced by
- Kim, Y.
- Year
- 2014
- Type
- Convolutional neural network (deep learning)
What does it do?
TextCNN is a convolutional neural network for text classification, introduced by Yoon Kim in 2014, that applies parallel convolution filters of several window sizes over word embeddings to capture local n-gram patterns.
In Kim's reported experiments, a simple model of this kind trained on pretrained word vectors with little hyperparameter tuning improved on the state of the art on four of seven benchmark tasks, including sentiment analysis and question classification.
The intuition behind the method
Many cues that decide what a sentence is about are short phrases — two, three, or four words like 'not very good' or 'highly recommend'. TextCNN slides small filters across the sequence of word vectors, each filter acting like a detector for a particular kind of short phrase. By using several window sizes in parallel it learns to spot the most telling phrases regardless of where they appear in the text, then uses the strongest signals to decide the label.
History
Yoon Kim introduced TextCNN in 2014 at EMNLP. He showed that a simple CNN trained on top of pretrained word vectors, with little hyperparameter tuning, performed strongly on sentence-level classification, improving on the state of the art on four of seven benchmark tasks, including sentiment analysis and question classification. Fine-tuning the vectors to the task gave further gains, and Kim also proposed a simple modification that allows both static and task-specific vectors to be used.
When to use it
Choose TextCNN for classifying sentences or short documents from labelled text, such as sentiment or topic labels, when you can supply pretrained word embeddings and have enough labelled documents to train on and to hold out for validation and testing. It is best suited to tasks where the label depends on short, local phrases. Do not choose it when labelled data is scarce; a classical TF-IDF pipeline with a linear or tree-based classifier is often a simpler and more robust starting point. Also avoid it for long documents where the label depends on relations across many sentences, because the convolution windows are local.
Limitations and considerations
- Performance depends on a labelled dataset and on pretrained embeddings; Kim's evidence comes from sentence-level benchmarks that used pretrained word vectors.
- With small labelled sets the filters can overfit the training labels. No validated threshold for the required amount of data exists for this method, so the adequacy of a dataset has to be judged on held-out data.
- Max pooling keeps only the strongest activation per filter, so how often a phrase occurs and the order of phrases are discarded.
- The windows are local, so dependencies that span many sentences are not represented directly.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Parallel filters of several window sizes detect short phrases of different lengths in a single pass over the sentence.
- Max-over-time pooling makes each detected feature independent of where the phrase occurs in the sentence.
- Kim reported strong results with little hyperparameter tuning and static pretrained vectors, so a basic configuration is a usable starting point.
- Convolutions over fixed local windows can be computed in parallel across positions, unlike the step-by-step processing of recurrent models.
Applications
- Sentence-level sentiment classification of reviews and comments.
- Question or topic classification of short texts.
- Labelling short survey responses or messages where local phrases carry the signal.
- A convolutional baseline for sentence classification against which heavier sequence models can be compared.
How it works
How it works
Each word in the input is mapped to a dense word embedding, such as Word2Vec or GloVe, so the sentence becomes a matrix in which each row is one word vector. Kim's experiments used pretrained vectors, either kept fixed ('static') or fine-tuned on the task.
Parallel filters of several window sizes (the example on this page uses 3, 4, and 5 consecutive words) slide over the embedding matrix. At each position a filter combines the words it currently spans using its weights w, a bias b, and a nonlinear function f, as in the formula above. Each filter therefore responds to local n-gram patterns wherever they occur in the sentence.
Formula
c_i = f(\mathbf{w} \cdot \mathbf{x}_{i:i+h-1} + b)Max-over-time pooling keeps only the largest activation of each filter across the sentence, so each filter reports its strongest match regardless of position. The pooling formula shown here applies to a single filter. The pooled values of all filters are concatenated and passed to a fully connected output layer with a softmax over the classes, which produces the predicted label.
Formula
\hat{c} = \max_i c_iCommon pitfalls
- Training on a small labelled set and reporting training performance as if it indicated generalisation.
- Learning word vectors from scratch on a small corpus instead of starting from pretrained embeddings.
- Choosing window sizes and filter counts without checking them on a held-out validation set.
- Using TextCNN without comparing it to a TF-IDF plus classical classifier baseline on the same data.
- Reporting the result of a single training run, although results can vary with the random seed.
Frequently asked questions
How much labelled text do I need?
No validated minimum sample size exists for TextCNN, and this page does not give one. Small labelled sets make overfitting more likely. Judge whether your data are sufficient by using a held-out test set or cross-validation, and compare the model against a TF-IDF baseline trained on the same data.
Do I need pretrained word embeddings?
Kim's experiments used pretrained word vectors, and fine-tuning them was reported to add further gains. Using pretrained embeddings such as Word2Vec or GloVe is advisable when the labelled set is modest, since they carry word meaning learned from larger corpora.
What do the different window sizes do?
Each window size lets a filter look at a different length of phrase, for example three, four, or five consecutive words. Running several sizes in parallel captures n-gram patterns of different lengths. Choose the window sizes and number of filters on a held-out validation set rather than fixing them in advance.
When should I pick a different method?
When labelled data is scarce, start with a TF-IDF model and a classical classifier. When the label depends on information spread across long passages, a sequence model that handles long-range dependencies, such as a gated recurrent network or a dilated CNN, may be more suitable.
Related methods
Sources
Kim, Y. (2014). Convolutional Neural Networks for Sentence Classification. EMNLP.
Open DOI referenceZhang, Y. & Wallace, B. (2015). A Sensitivity Analysis of (and Practitioners' Guide to) Convolutional Neural Networks for Sentence Classification. arXiv:1510.03820.
Open publication reference