text_plain_output_1.png
from keras.datasets import imdb

from keras.layers import SimpleRNN, Dense, Activation

from keras.layers.embeddings import Embedding

from keras.models import Sequential

from keras.preprocessing.sequence import pad_sequences

import matplotlib.pyplot as plt

import seaborn as sns

(x_train, y_train), (x_test, y_test) = imdb.load_data(path='imdb.npz', num_words=None, skip_top=0, maxlen=None, seed=113, start_char=1, oov_char=2, index_from=3)

review_len_train = []
review_len_test = []
for i, ii in zip(x_train, x_test):
    review_len_train.append(len(i))
    review_len_test.append(len(ii))

word_index = imdb.get_word_index()

num_words = 15000
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=num_words)

maxlen = 130
x_train = pad_sequences(x_train, maxlen=maxlen)
x_test = pad_sequences(x_test, maxlen=maxlen)

rnn = Sequential()
rnn.add(Embedding(num_words, 32, input_length=len(x_train[0])))
rnn.add(SimpleRNN(16, input_shape=(num_words, maxlen), return_sequences=False, activation='relu'))
rnn.add(Dense(1))
rnn.add(Activation('sigmoid'))
rnn.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])

history = rnn.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=6, batch_size=128, verbose=1)

plt.figure()
plt.plot(history.history['loss'], label='train')
plt.plot(history.history['val_loss'], label='test')
plt.title('acc')
plt.ylabel('Acc')
plt.xlabel('epochs')
plt.legend()
plt.show()