So far, we've applied to image data. As in the introduction, CNNs can also be applied to other types of input data. In the following recipe, we will show how you can apply a CNN to textual data. More specifically, we will use the structure of CNNs to classify text. Unlike images, which are 2D, text has 1D input data. Therefore, we will be using 1D convolutional layers in our next recipe. The Keras framework makes it really easy to pre-process the input data.
- Let's start with importing the libraries as follows:
import numpy as np from keras.preprocessing import sequence from keras.models import Sequential from keras.layers import Dense, Dropout, Activation from keras.layers import Embedding from keras.layers import Conv1D, GlobalMaxPooling1D from keras.callbacks import EarlyStopping from keras.datasets import imdb
- We will be using the
imdb
dataset from keras; load the data with the following code:
n_words = 1000 (X_train, y_train), (X_test, y_test)...