keras/examples/imdb_cnn.py

78 lines
2.2 KiB
Python
Raw Normal View History

2015-12-09 02:49:14 +00:00
'''This example demonstrates the use of Convolution1D for text classification.
2016-08-01 00:45:32 +00:00
Gets to 0.89 test accuracy after 2 epochs.
90s/epoch on Intel i5 2.4Ghz CPU.
10s/epoch on Tesla K40 GPU.
2015-12-09 02:49:14 +00:00
'''
2015-07-14 20:34:05 +00:00
from __future__ import print_function
import numpy as np
2015-10-05 01:44:49 +00:00
np.random.seed(1337) # for reproducibility
2015-07-14 20:34:05 +00:00
from keras.preprocessing import sequence
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
2016-05-12 01:45:37 +00:00
from keras.layers import Embedding
from keras.layers import Convolution1D, GlobalMaxPooling1D
2015-07-14 20:34:05 +00:00
from keras.datasets import imdb
2015-07-15 03:35:28 +00:00
2015-07-14 20:34:05 +00:00
# set parameters:
max_features = 5000
maxlen = 400
2015-07-15 03:35:28 +00:00
batch_size = 32
embedding_dims = 50
2015-10-05 01:44:49 +00:00
nb_filter = 250
2015-07-14 20:34:05 +00:00
filter_length = 3
hidden_dims = 250
2015-11-29 00:34:52 +00:00
nb_epoch = 2
2015-07-14 20:34:05 +00:00
2015-12-09 02:49:14 +00:00
print('Loading data...')
2016-07-28 00:30:04 +00:00
(X_train, y_train), (X_test, y_test) = imdb.load_data(nb_words=max_features)
2015-07-14 20:34:05 +00:00
print(len(X_train), 'train sequences')
print(len(X_test), 'test sequences')
2015-12-09 02:49:14 +00:00
print('Pad sequences (samples x time)')
2015-07-14 20:34:05 +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)
print('Build model...')
model = Sequential()
# we start off with an efficient embedding layer which maps
# our vocab indices into embedding_dims dimensions
model.add(Embedding(max_features,
embedding_dims,
input_length=maxlen,
dropout=0.2))
2015-07-14 20:34:05 +00:00
2015-10-05 01:44:49 +00:00
# we add a Convolution1D, which will learn nb_filter
2015-07-14 20:34:05 +00:00
# word group filters of size filter_length:
2015-10-05 01:44:49 +00:00
model.add(Convolution1D(nb_filter=nb_filter,
2015-07-14 20:34:05 +00:00
filter_length=filter_length,
2015-12-09 02:49:14 +00:00
border_mode='valid',
activation='relu',
2015-07-14 20:34:05 +00:00
subsample_length=1))
# we use max pooling:
model.add(GlobalMaxPooling1D())
2015-07-14 20:34:05 +00:00
# We add a vanilla hidden layer:
2015-10-05 01:44:49 +00:00
model.add(Dense(hidden_dims))
model.add(Dropout(0.2))
2015-07-14 20:34:05 +00:00
model.add(Activation('relu'))
# We project onto a single unit output layer, and squash it with a sigmoid:
2015-10-05 01:44:49 +00:00
model.add(Dense(1))
2015-07-14 20:34:05 +00:00
model.add(Activation('sigmoid'))
2015-11-29 00:34:52 +00:00
model.compile(loss='binary_crossentropy',
optimizer='adam',
2016-03-19 16:07:15 +00:00
metrics=['accuracy'])
model.fit(X_train, y_train,
batch_size=batch_size,
nb_epoch=nb_epoch,
2015-11-29 00:34:52 +00:00
validation_data=(X_test, y_test))