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 GRU
    from keras.callbacks import EarlyStopping

    from keras.datasets import imdb


    n_words = 1000
    (X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=n_words)
    print('Train seq: {}'.format(len(X_train)))
    print('Test seq: {}'.format(len(X_train)))

    # Pad sequences with max_len
    max_len = 200
    X_train = sequence.pad_sequences(X_train, maxlen=max_len)
    X_test = sequence.pad_sequences(X_test, maxlen=max_len)

    # Define network architecture and compile
    model = Sequential()
    model.add(Embedding(n_words, 50, input_length=max_len))
    model.add(Dropout(0.2))
    model.add(GRU(100, dropout=0.2, recurrent_dropout=0.2))
    model.add(Dense(250, activation='relu'))
    model.add(Dropout(0.2))
    model.add(Dense(1, activation='sigmoid'))

    model.compile(loss='binary_crossentropy',  optimizer='adam', metrics=['accuracy'])
    model.summary()

    callbacks = [EarlyStopping(monitor='val_acc', patience=3)]


    batch_size = 512
    n_epochs = 100

    model.fit(X_train, y_train, batch_size=batch_size, epochs=n_epochs, validation_split=0.2, callbacks=callbacks)

    print('Accuracy on test set: {}'.format(model.evaluate(X_test, y_test)[1]))

    # Accuracy on test set: 0.83004