keras/examples/imdb_lstm.py

68 lines
2.2 KiB
Python
Raw Normal View History

2015-12-09 02:49:14 +00:00
'''Train a LSTM on the IMDB sentiment classification task.
The dataset is actually too small for LSTM to be of any advantage
compared to simpler, much faster methods such as TF-IDF+LogReg.
Notes:
- RNNs are tricky. Choice of batch size is important,
choice of loss and optimizer is critical, etc.
Some configurations won't converge.
- LSTM loss decrease patterns during training can be quite different
from what you see with CNNs/MLPs/etc.
GPU command:
THEANO_FLAGS=mode=FAST_RUN,device=gpu,floatX=float32 python imdb_lstm.py
'''
from __future__ import print_function
2015-03-28 00:59:42 +00:00
import numpy as np
2015-07-26 08:00:18 +00:00
np.random.seed(1337) # for reproducibility
2015-03-28 00:59:42 +00:00
from keras.preprocessing import sequence
from keras.utils import np_utils
from keras.models import Sequential
2015-04-10 22:14:01 +00:00
from keras.layers.core import Dense, Dropout, Activation
from keras.layers.embeddings import Embedding
2015-11-29 00:34:52 +00:00
from keras.layers.recurrent import LSTM
2015-03-28 00:59:42 +00:00
from keras.datasets import imdb
2015-06-26 22:21:10 +00:00
max_features = 20000
2015-07-26 08:00:18 +00:00
maxlen = 100 # cut texts after this number of words (among top max_features most common words)
batch_size = 32
2015-06-26 22:21:10 +00:00
2015-12-09 02:49:14 +00:00
print('Loading data...')
2015-11-29 00:34:52 +00:00
(X_train, y_train), (X_test, y_test) = imdb.load_data(nb_words=max_features,
test_split=0.2)
print(len(X_train), 'train sequences')
print(len(X_test), 'test sequences')
2015-03-28 00:59:42 +00:00
print("Pad sequences (samples x time)")
2015-03-28 00:59:42 +00:00
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)
2015-03-28 00:59:42 +00:00
print('Build model...')
2015-03-28 00:59:42 +00:00
model = Sequential()
model.add(Embedding(max_features, 128, input_length=maxlen))
2015-10-05 01:44:49 +00:00
model.add(LSTM(128)) # try using a GRU instead, for fun
2015-03-28 00:59:42 +00:00
model.add(Dropout(0.5))
2015-10-05 01:44:49 +00:00
model.add(Dense(1))
2015-03-28 00:59:42 +00:00
model.add(Activation('sigmoid'))
# try using different optimizers and different optimizer configs
2015-11-29 00:34:52 +00:00
model.compile(loss='binary_crossentropy',
optimizer='adam',
class_mode="binary")
2015-03-28 00:59:42 +00:00
print("Train...")
2015-11-29 00:34:52 +00:00
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=3,
validation_data=(X_test, y_test), show_accuracy=True)
score, acc = model.evaluate(X_test, y_test,
batch_size=batch_size,
show_accuracy=True)
print('Test score:', score)
print('Test accuracy:', acc)