Hire a web Developer and Designer to upgrade and boost your online presence with cutting edge Technologies

Saturday, 21 December 2024

How to Develop a Multichannel CNN Model for Text Classification

 A standard deep learning model for text classification and sentiment analysis uses a word embedding layer and one-dimensional convolutional neural network.

The model can be expanded by using multiple parallel convolutional neural networks that read the source document using different kernel sizes. This, in effect, creates a multichannel convolutional neural network for text that reads text with different n-gram sizes (groups of words).

In this tutorial, you will discover how to develop a multichannel convolutional neural network for sentiment prediction on text movie review data.

After completing this tutorial, you will know:

  • How to prepare movie review text data for modeling.
  • How to develop a multichannel convolutional neural network for text in Keras.
  • How to evaluate a fit model on unseen movie review data.

    Tutorial Overview

    This tutorial is divided into 4 parts; they are:

    1. Movie Review Dataset
    2. Data Preparation
    3. Develop Multichannel Model
    4. Evaluate Model

    Python Environment

    This tutorial assumes you have a Python 3 SciPy environment installed.

    You must have Keras (2.0 or higher) installed with either the TensorFlow or Theano backend.

    The tutorial also assumes you have scikit-learn, Pandas, NumPy, and Matplotlib installed.

    If you need help with your environment, see this post:

    Need help with Deep Learning for Text Data?

    Take my free 7-day email crash course now (with code).

    Click to sign-up and also get a free PDF Ebook version of the course.

    Movie Review Dataset

    The Movie Review Data is a collection of movie reviews retrieved from the imdb.com website in the early 2000s by Bo Pang and Lillian Lee. The reviews were collected and made available as part of their research on natural language processing.

    The reviews were originally released in 2002, but an updated and cleaned up version was released in 2004, referred to as “v2.0”.

    The dataset is comprised of 1,000 positive and 1,000 negative movie reviews drawn from an archive of the rec.arts.movies.reviews newsgroup hosted at imdb.com. The authors refer to this dataset as the “polarity dataset.”

    Our data contains 1000 positive and 1000 negative reviews all written before 2002, with a cap of 20 reviews per author (312 authors total) per category. We refer to this corpus as the polarity dataset.

    — A Sentimental Education: Sentiment Analysis Using Subjectivity Summarization Based on Minimum Cuts, 2004.

    The data has been cleaned up somewhat; for example:

    • The dataset is comprised of only English reviews.
    • All text has been converted to lowercase.
    • There is white space around punctuation like periods, commas, and brackets.
    • Text has been split into one sentence per line.

    The data has been used for a few related natural language processing tasks. For classification, the performance of machine learning models (such as Support Vector Machines) on the data is in the range of high 70% to low 80% (e.g. 78%-82%).

    More sophisticated data preparation may see results as high as 86% with 10-fold cross-validation. This gives us a ballpark of low-to-mid 80s if we were looking to use this dataset in experiments of modern methods.

    … depending on choice of downstream polarity classifier, we can achieve highly statistically significant improvement (from 82.8% to 86.4%)

    — A Sentimental Education: Sentiment Analysis Using Subjectivity Summarization Based on Minimum Cuts, 2004.

    You can download the dataset from here:

    After unzipping the file, you will have a directory called “txt_sentoken” with two sub-directories containing the text “neg” and “pos” for negative and positive reviews. Reviews are stored one per file with a naming convention cv000 to cv999 for each neg and pos.

    Next, let’s look at loading and preparing the text data.

    Data Preparation

    In this section, we will look at 3 things:

    1. Separation of data into training and test sets.
    2. Loading and cleaning the data to remove punctuation and numbers.
    3. Prepare all reviews and save to file.

    Split into Train and Test Sets

    We are pretending that we are developing a system that can predict the sentiment of a textual movie review as either positive or negative.

    This means that after the model is developed, we will need to make predictions on new textual reviews. This will require all of the same data preparation to be performed on those new reviews as is performed on the training data for the model.

    We will ensure that this constraint is built into the evaluation of our models by splitting the training and test datasets prior to any data preparation. This means that any knowledge in the data in the test set that could help us better prepare the data (e.g. the words used) is unavailable in the preparation of data used for training the model.

    That being said, we will use the last 100 positive reviews and the last 100 negative reviews as a test set (100 reviews) and the remaining 1,800 reviews as the training dataset.

    This is a 90% train, 10% split of the data.

    The split can be imposed easily by using the filenames of the reviews where reviews named 000 to 899 are for training data and reviews named 900 onwards are for test.

    Loading and Cleaning Reviews

    The text data is already pretty clean; not much preparation is required.

    Without getting bogged down too much by the details, we will prepare the data in the following way:

    • Split tokens on white space.
    • Remove all punctuation from words.
    • Remove all words that are not purely comprised of alphabetical characters.
    • Remove all words that are known stop words.
    • Remove all words that have a length <= 1 character.

    We can put all of these steps into a function called clean_doc() that takes as an argument the raw text loaded from a file and returns a list of cleaned tokens. We can also define a function load_doc() that loads a document from file ready for use with the clean_doc() function. An example of cleaning the first positive review is listed below.

    Running the example loads and cleans one movie review.

    The tokens from the clean review are printed for review.

    Clean All Reviews and Save

    We can now use the function to clean reviews and apply it to all reviews.

    To do this, we will develop a new function named process_docs() below that will walk through all reviews in a directory, clean them, and return them as a list.

    We will also add an argument to the function to indicate whether the function is processing train or test reviews, that way the filenames can be filtered (as described above) and only those train or test reviews requested will be cleaned and returned.

    The full function is listed below.

    We can call this function with negative training reviews as follows:

    Next, we need labels for the train and test documents. We know that we have 900 training documents and 100 test documents. We can use a Python list comprehension to create the labels for the negative (0) and positive (1) reviews for both train and test sets.

    Finally, we want to save the prepared train and test sets to file so that we can load them later for modeling and model evaluation.

    The function below-named save_dataset() will save a given prepared dataset (X and y elements) to a file using the pickle API.

    Complete Example

    We can tie all of these data preparation steps together.

    The complete example is listed below.

    Running the example cleans the text movie review documents, creates labels, and saves the prepared data for both train and test datasets in train.pkl and test.pkl respectively.

    Now we are ready to develop our model.

    Develop Multichannel Model

    In this section, we will develop a multichannel convolutional neural network for the sentiment analysis prediction problem.

    This section is divided into 3 parts:

    1. Encode Data
    2. Define Model.
    3. Complete Example.

    Encode Data

    The first step is to load the cleaned training dataset.

    The function below-named load_dataset() can be called to load the pickled training dataset.

    Next, we must fit a Keras Tokenizer on the training dataset. We will use this tokenizer to both define the vocabulary for the Embedding layer and encode the review documents as integers.

    The function create_tokenizer() below will create a Tokenizer given a list of documents.

    We also need to know the maximum length of input sequences as input for the model and to pad all sequences to the fixed length.

    The function max_length() below will calculate the maximum length (number of words) for all reviews in the training dataset.

    We also need to know the size of the vocabulary for the Embedding layer.

    This can be calculated from the prepared Tokenizer, as follows:

    Finally, we can integer encode and pad the clean movie review text.

    The function below named encode_text() will both encode and pad text data to the maximum review length.

    Define Model

    A standard model for document classification is to use an Embedding layer as input, followed by a one-dimensional convolutional neural network, pooling layer, and then a prediction output layer.

    The kernel size in the convolutional layer defines the number of words to consider as the convolution is passed across the input text document, providing a grouping parameter.

    A multi-channel convolutional neural network for document classification involves using multiple versions of the standard model with different sized kernels. This allows the document to be processed at different resolutions or different n-grams (groups of words) at a time, whilst the model learns how to best integrate these interpretations.

    This approach was first described by Yoon Kim in his 2014 paper titled “Convolutional Neural Networks for Sentence Classification.”

    In the paper, Kim experimented with static and dynamic (updated) embedding layers, we can simplify the approach and instead focus only on the use of different kernel sizes.

    This approach is best understood with a diagram taken from Kim’s paper:

    Depiction of the multiple-channel convolutional neural network for text

    Depiction of the multiple-channel convolutional neural network for text.
    Taken from “Convolutional Neural Networks for Sentence Classification.”

    In Keras, a multiple-input model can be defined using the functional API.

    We will define a model with three input channels for processing 4-grams, 6-grams, and 8-grams of movie review text.

    Each channel is comprised of the following elements:

    • Input layer that defines the length of input sequences.
    • Embedding layer set to the size of the vocabulary and 100-dimensional real-valued representations.
    • One-dimensional convolutional layer with 32 filters and a kernel size set to the number of words to read at once.
    • Max Pooling layer to consolidate the output from the convolutional layer.
    • Flatten layer to reduce the three-dimensional output to two dimensional for concatenation.

    The output from the three channels are concatenated into a single vector and process by a Dense layer and an output layer.

    The function below defines and returns the model. As part of defining the model, a summary of the defined model is printed and a plot of the model graph is created and saved to file.

    Complete Example

    Pulling all of this together, the complete example is listed below.

    Running the example first prints a summary of the prepared training dataset.

    Next, a summary of the defined model is printed.

    The model is fit relatively quickly and appears to show good skill on the training dataset.

    A plot of the defined model is saved to file, clearly showing the three input channels for the model.

    Plot of the Multichannel Convolutional Neural Network For Text

    Plot of the Multichannel Convolutional Neural Network For Text

    The model is fit for a number of epochs and saved to the file model.h5 for later evaluation.

    Evaluate Model

    In this section, we can evaluate the fit model by predicting the sentiment on all reviews in the unseen test dataset.

    Using the data loading functions developed in the previous section, we can load and encode both the training and test datasets.

    We can load the saved model and evaluate it on both the training and test datasets.

    The complete example is listed below.

    Note: Your results may vary given the stochastic nature of the algorithm or evaluation procedure, or differences in numerical precision. Consider running the example a few times and compare the average outcome.

    Running the example prints the skill of the model on both the training and test datasets.

    We can see that, as expected, the skill on the training dataset is excellent, here at 100% accuracy.

    We can also see that the skill of the model on the unseen test dataset is also very impressive, achieving 87.5%, which is above the skill of the model reported in the 2014 paper (although not a direct apples-to-apples comparison).

    Extensions

    This section lists some ideas for extending the tutorial that you may wish to explore.

    • Different n-grams. Explore the model by changing the kernel size (number of n-grams) used by the channels in the model to see how it impacts model skill.
    • More or Fewer Channels. Explore using more or fewer channels in the model and see how it impacts model skill.
    • Deeper Network. Convolutional neural networks perform better in computer vision when they are deeper. Explore using deeper models here and see how it impacts model skill.

    Further Reading

    This section provides more resources on the topic if you are looking to go deeper.

    Summary

    In this tutorial, you discovered how to develop a multichannel convolutional neural network for sentiment prediction on text movie review data.

    Specifically, you learned:

    • How to prepare movie review text data for modeling.
    • How to develop a multichannel convolutional neural network for text in Keras.
    • How to evaluate a fit model on unseen movie review data.

    Do you have any questions?
    Ask your questions in the comments below and I will do my best to answer.

No comments:

Post a Comment

Connect broadband

AI:Lord Krishna Unventional and DEceptive methodlogies

Lord Krishna's actions during the Kurukshetra War, particularly his role in the deaths of Bhishma, Drona, and Karna, are often seen thro...