from keras.datasets import imdb
from keras.preprocessing import sequence

max_features = 10000
max_len = 500

print('Loading data...')
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words = max_features)

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 = max_len)
x_test = sequence.pad_sequences(x_test, maxlen = max_len)

print('x_train shape:', x_train.shape)
print('x_test shape:', x_test.shape)

"""
The structure of a 1D convnet is the same than a 2D, we also use max pooling layers
to reduce the number of features. However as there is only one dimension we can afford
a bigger window (bigger than 2 x 2 or 3 x 3).
"""
from keras.models import Sequential
from keras import layers
from keras.optimizers import RMSprop

model = Sequential()
model.add(layers.Embedding(max_features, 128, input_length = max_len))
model.add(layers.Conv1D(32, 7, activation = 'relu'))
model.add(layers.MaxPooling1D(5))
model.add(layers.Conv1D(32, 7, activation = 'relu'))
model.add(layers.GlobalMaxPooling1D())
model.add(layers.Dense(1))
model.summary()

model.compile(
  optimizer = RMSprop(lr = 1e-4),
  loss = 'binary_crossentropy',
  metrics = ['acc'],
)
history = model.fit(
  x_train,
  y_train,
  epochs = 10,
  batch_size = 128,
  validation_split = 0.2,
)

acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(1, len(loss) + 1)

from matplotlib import pyplot as plt

plt.plot(epochs, acc, 'bo', label = 'Training acc')
plt.plot(epochs, val_acc, 'b', label = 'Validation acc')
plt.title('Training and validation accuracy')
plt.legend()
plt.figure()

plt.plot(epochs, loss, 'bo', label = 'Training loss')
plt.plot(epochs, val_loss, 'b', label = 'Validation loss')
plt.title('Training and validation loss')
plt.legend()
plt.show()

"""
The results are less good than with an RNN but it runs a lot faster.
84% accuracy vs 89% with the RNN LSTM.
"""