Examples: https://github.com/fchollet/keras/tree/master/examples
To consider/compare:
https://github.com/fchollet/keras/blob/master/examples/imdb_fasttext.py
https://github.com/fchollet/keras/blob/master/examples/lstm_benchmark.py
https://github.com/fchollet/keras/blob/master/examples/imdb_bidirectional_lstm.py
In [1]:
import numpy as np
from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers import Dense, Dropout, Embedding, LSTM, Bidirectional
from keras.datasets import imdb
from keras.utils import plot_model
Dataset used here is IMDB Movie reviews sentiment classification dataset available through keras
:
imdb.load_data()
comes with option to consider only the top most frequent words
In [2]:
max_features = 20000
(x_train, y_train), (x_test, y_test) = imdb.load_data(num_words=max_features)
In [3]:
x_train[0][:5]
Out[3]:
In [4]:
y_train[:5]
Out[4]:
In [5]:
print(len(x_train), 'train sequences')
print(len(x_test), 'test sequences')
print('Average train sequence length: {}'.format(np.mean(list(map(len, x_train)), dtype=int)))
print('Average test sequence length: {}'.format(np.mean(list(map(len, x_test)), dtype=int)))
In [6]:
maxlen = 300
# pad sequences shorter than maxlen with 0s; truncate
# longer sequences to maxlen
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)
y_train = np.array(y_train)
y_test = np.array(y_test)
In [7]:
model = Sequential()
model.add(Embedding(max_features, 128, input_length=maxlen))
model.add(LSTM(64))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
# try using different optimizers and different optimizer configs
model.compile('adam', 'binary_crossentropy', metrics=['accuracy'])
In [8]:
plot_model(model, to_file='img/chapter_10_rnn_no_bidir.png', show_shapes=True)
In [9]:
batch_size = 32
epochs = 10
In [10]:
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
validation_data=[x_test, y_test])
Out[10]:
In [11]:
model = Sequential()
model.add(Embedding(max_features, 128, input_length=maxlen))
model.add(Bidirectional(LSTM(64)))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
# try using different optimizers and different optimizer configs
model.compile('adam', 'binary_crossentropy', metrics=['accuracy'])
In [12]:
plot_model(model, to_file='img/chapter_10_rnn.png', show_shapes=True)
In [13]:
batch_size = 32
epochs = 10
In [14]:
model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
validation_data=[x_test, y_test])
Out[14]: