import numpy as np

    from keras.preprocessing import sequence
    from keras.models import Sequential
    from keras.layers import Dense, Dropout, Activation, Embedding, LSTM, Bidirectional
    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)))

    print('Train example: \n{}'.format(X_train[0]))
    print('\nTest example: \n{}'.format(X_test[0]))

    # Note: the data is already preprocessed (words are mapped to vectors)

    # 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(Bidirectional(LSTM(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 = 1024
    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, batch_size=batch_size)[1]))

    # Accuracy on test set: 0.8391600004386902