old_contents: from __future__ import absolute_import
  from __future__ import print_function
  import numpy as np
  np.random.seed(1337)  # for reproducibility
  
  from keras.preprocessing import sequence
  from keras.optimizers import SGD, RMSprop, Adagrad
  from keras.utils import np_utils
  from keras.models import Sequential
  from keras.layers.core import Dense, Dropout, Activation
  from keras.layers.embeddings import Embedding
  from keras.layers.recurrent import LSTM, GRU, SimpleRNN
  from keras.layers.convolutional import Convolution1D, MaxPooling1D
  from keras.datasets import imdb
  
  '''
      Train a recurrent convolutional network on the IMDB sentiment classification task.
      Reach 84.98% accuracy in 2 epoches
      GPU command:
          THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python imdb_lstm.py
  '''
  
  
  #Parameters
  
  #Embedding
  max_features = 20000
  maxlen = 100 
  embedding_size = 128
  
  #Convolution
  filter_length = 3
  nb_filter = 64
  pool_length = 2
  
  #LSTM
  lstm_output_size = 70
  
  #Training
  batch_size = 30
  nb_epoch = 2
  '''
  Note:
  batch_size is highly sensitive.
  Only 2 epoches are done as the dataset is too small.
  '''
  
  
  print("Loading data...")
  (X_train, y_train), (X_test, y_test) = imdb.load_data(nb_words=max_features, test_split=0.2)
  print(len(X_train), 'train sequences')
  print(len(X_test), 'test sequences')
  
  print("Pad sequences (samples x time)")
  X_train = sequence.pad_sequences(X_train, maxlen=maxlen)
  X_test = sequence.pad_sequences(X_test, maxlen=maxlen)
  print('X_train shape:', X_train.shape)
  print('X_test shape:', X_test.shape)
  
  print('Build model...')
  
  model = Sequential()
  model.add(Embedding(max_features, embedding_size, input_length=maxlen))
  model.add(Dropout(0.25))
  
  model.add(Convolution1D(nb_filter=nb_filter,
                          filter_length=filter_length,
                          border_mode="valid",
                          activation="relu",
                          subsample_length=1))
  
  model.add(MaxPooling1D(pool_length=pool_length))
  
  model.add(LSTM(lstm_output_size)) 
  model.add(Dense(1))
  model.add(Activation('sigmoid'))
  
  model.compile(loss='binary_crossentropy', optimizer='adam', class_mode="binary")
  
  print("Train...")
  model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, validation_data=(X_test, y_test), show_accuracy=True)
  score, acc = model.evaluate(X_test, y_test, batch_size=batch_size, show_accuracy=True)
  print('Test score:', score)
  print('Test accuracy:', acc)
  
new_contents: from __future__ import absolute_import
  from __future__ import print_function
  import numpy as np
  np.random.seed(1337)  # for reproducibility
  
  from keras.preprocessing import sequence
  from keras.optimizers import SGD, RMSprop, Adagrad
  from keras.utils import np_utils
  from keras.models import Sequential
  from keras.layers.core import Dense, Dropout, Activation
  from keras.layers.embeddings import Embedding
  from keras.layers.recurrent import LSTM, GRU, SimpleRNN
  from keras.layers.convolutional import Convolution1D, MaxPooling1D
  from keras.datasets import imdb
  
  '''
      Train a recurrent convolutional network on the IMDB sentiment classification task.
  
      GPU command:
          THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python imdb_lstm.py
          
      Get to 0.8498 test accuracy after 2 epochs. 41s/epoch on K520 GPU.
  '''
  
  
  #Parameters
  
  #Embedding
  max_features = 20000
  maxlen = 100 
  embedding_size = 128
  
  #Convolution
  filter_length = 3
  nb_filter = 64
  pool_length = 2
  
  #LSTM
  lstm_output_size = 70
  
  #Training
  batch_size = 30
  nb_epoch = 2
  '''
  Note:
  batch_size is highly sensitive.
  Only 2 epoches are done as the dataset is too small.
  '''
  
  
  print("Loading data...")
  (X_train, y_train), (X_test, y_test) = imdb.load_data(nb_words=max_features, test_split=0.2)
  print(len(X_train), 'train sequences')
  print(len(X_test), 'test sequences')
  
  print("Pad sequences (samples x time)")
  X_train = sequence.pad_sequences(X_train, maxlen=maxlen)
  X_test = sequence.pad_sequences(X_test, maxlen=maxlen)
  print('X_train shape:', X_train.shape)
  print('X_test shape:', X_test.shape)
  
  print('Build model...')
  
  model = Sequential()
  model.add(Embedding(max_features, embedding_size, input_length=maxlen))
  model.add(Dropout(0.25))
  
  model.add(Convolution1D(nb_filter=nb_filter,
                          filter_length=filter_length,
                          border_mode="valid",
                          activation="relu",
                          subsample_length=1))
  
  model.add(MaxPooling1D(pool_length=pool_length))
  
  model.add(LSTM(lstm_output_size)) 
  model.add(Dense(1))
  model.add(Activation('sigmoid'))
  
  model.compile(loss='binary_crossentropy', optimizer='adam', class_mode="binary")
  
  print("Train...")
  model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, validation_data=(X_test, y_test), show_accuracy=True)
  score, acc = model.evaluate(X_test, y_test, batch_size=batch_size, show_accuracy=True)
  print('Test score:', score)
  print('Test accuracy:', acc)
  
subject: Update imdb_cnn_lstm.py