This example demonstrates the use of Convolution1D for text classification.

Gets to 0.89 test accuracy after 2 epochs. 90s/epoch on Intel i5 2.4Ghz CPU. 10s/epoch on Tesla K40 GPU.

  1. from __future__ import print_function
  2. from keras.preprocessing import sequence
  3. from keras.models import Sequential
  4. from keras.layers import Dense, Dropout, Activation
  5. from keras.layers import Embedding
  6. from keras.layers import Conv1D, GlobalMaxPooling1D
  7. from keras.datasets import imdb
  8. # set parameters:
  9. max_features = 5000
  10. maxlen = 400
  11. batch_size = 32
  12. embedding_dims = 50
  13. filters = 250
  14. kernel_size = 3
  15. hidden_dims = 250
  16. epochs = 2
  17. print('Loading data...')
  18. (x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
  19. print(len(x_train), 'train sequences')
  20. print(len(x_test), 'test sequences')
  21. print('Pad sequences (samples x time)')
  22. x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
  23. x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
  24. print('x_train shape:', x_train.shape)
  25. print('x_test shape:', x_test.shape)
  26. print('Build model...')
  27. model = Sequential()
  28. # we start off with an efficient embedding layer which maps
  29. # our vocab indices into embedding_dims dimensions
  30. model.add(Embedding(max_features,
  31. embedding_dims,
  32. input_length=maxlen))
  33. model.add(Dropout(0.2))
  34. # we add a Convolution1D, which will learn filters
  35. # word group filters of size filter_length:
  36. model.add(Conv1D(filters,
  37. kernel_size,
  38. padding='valid',
  39. activation='relu',
  40. strides=1))
  41. # we use max pooling:
  42. model.add(GlobalMaxPooling1D())
  43. # We add a vanilla hidden layer:
  44. model.add(Dense(hidden_dims))
  45. model.add(Dropout(0.2))
  46. model.add(Activation('relu'))
  47. # We project onto a single unit output layer, and squash it with a sigmoid:
  48. model.add(Dense(1))
  49. model.add(Activation('sigmoid'))
  50. model.compile(loss='binary_crossentropy',
  51. optimizer='adam',
  52. metrics=['accuracy'])
  53. model.fit(x_train, y_train,
  54. batch_size=batch_size,
  55. epochs=epochs,
  56. validation_data=(x_test, y_test))