Train a recurrent convolutional network on the IMDB sentiment classification task.

Gets to 0.8498 test accuracy after 2 epochs. 41 s/epoch on K520 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 LSTM
  7. from keras.layers import Conv1D, MaxPooling1D
  8. from keras.datasets import imdb
  9. # Embedding
  10. max_features = 20000
  11. maxlen = 100
  12. embedding_size = 128
  13. # Convolution
  14. kernel_size = 5
  15. filters = 64
  16. pool_size = 4
  17. # LSTM
  18. lstm_output_size = 70
  19. # Training
  20. batch_size = 30
  21. epochs = 2
  22. '''
  23. Note:
  24. batch_size is highly sensitive.
  25. Only 2 epochs are needed as the dataset is very small.
  26. '''
  27. print('Loading data...')
  28. (x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
  29. print(len(x_train), 'train sequences')
  30. print(len(x_test), 'test sequences')
  31. print('Pad sequences (samples x time)')
  32. x_train = sequence.pad_sequences(x_train, maxlen=maxlen)
  33. x_test = sequence.pad_sequences(x_test, maxlen=maxlen)
  34. print('x_train shape:', x_train.shape)
  35. print('x_test shape:', x_test.shape)
  36. print('Build model...')
  37. model = Sequential()
  38. model.add(Embedding(max_features, embedding_size, input_length=maxlen))
  39. model.add(Dropout(0.25))
  40. model.add(Conv1D(filters,
  41. kernel_size,
  42. padding='valid',
  43. activation='relu',
  44. strides=1))
  45. model.add(MaxPooling1D(pool_size=pool_size))
  46. model.add(LSTM(lstm_output_size))
  47. model.add(Dense(1))
  48. model.add(Activation('sigmoid'))
  49. model.compile(loss='binary_crossentropy',
  50. optimizer='adam',
  51. metrics=['accuracy'])
  52. print('Train...')
  53. model.fit(x_train, y_train,
  54. batch_size=batch_size,
  55. epochs=epochs,
  56. validation_data=(x_test, y_test))
  57. score, acc = model.evaluate(x_test, y_test, batch_size=batch_size)
  58. print('Test score:', score)
  59. print('Test accuracy:', acc)