2016-03-19 16:07:15 +00:00
|
|
|
'''Trains and evaluate a simple MLP
|
|
|
|
on the Reuters newswire topic classification task.
|
2015-12-09 02:49:14 +00:00
|
|
|
'''
|
2015-04-16 03:18:40 +00:00
|
|
|
from __future__ import print_function
|
2015-03-28 00:59:42 +00:00
|
|
|
|
2017-02-28 22:41:30 +00:00
|
|
|
import numpy as np
|
|
|
|
import keras
|
2015-03-28 00:59:42 +00:00
|
|
|
from keras.datasets import reuters
|
|
|
|
from keras.models import Sequential
|
2016-05-12 01:45:37 +00:00
|
|
|
from keras.layers import Dense, Dropout, Activation
|
2015-03-28 00:59:42 +00:00
|
|
|
from keras.preprocessing.text import Tokenizer
|
|
|
|
|
2015-06-02 03:11:44 +00:00
|
|
|
max_words = 1000
|
2015-05-31 01:46:00 +00:00
|
|
|
batch_size = 32
|
2017-02-15 00:08:30 +00:00
|
|
|
epochs = 5
|
2015-03-28 00:59:42 +00:00
|
|
|
|
2015-12-09 02:49:14 +00:00
|
|
|
print('Loading data...')
|
2017-02-28 22:41:30 +00:00
|
|
|
(x_train, y_train), (x_test, y_test) = reuters.load_data(num_words=max_words,
|
|
|
|
test_split=0.2)
|
|
|
|
print(len(x_train), 'train sequences')
|
|
|
|
print(len(x_test), 'test sequences')
|
2015-03-28 00:59:42 +00:00
|
|
|
|
2017-02-15 00:08:30 +00:00
|
|
|
num_classes = np.max(y_train) + 1
|
|
|
|
print(num_classes, 'classes')
|
2015-03-28 00:59:42 +00:00
|
|
|
|
2015-12-09 02:49:14 +00:00
|
|
|
print('Vectorizing sequence data...')
|
2017-02-15 00:08:30 +00:00
|
|
|
tokenizer = Tokenizer(num_words=max_words)
|
2017-02-28 22:41:30 +00:00
|
|
|
x_train = tokenizer.sequences_to_matrix(x_train, mode='binary')
|
|
|
|
x_test = tokenizer.sequences_to_matrix(x_test, mode='binary')
|
|
|
|
print('x_train shape:', x_train.shape)
|
|
|
|
print('x_test shape:', x_test.shape)
|
|
|
|
|
|
|
|
print('Convert class vector to binary class matrix '
|
|
|
|
'(for use with categorical_crossentropy)')
|
|
|
|
y_train = keras.utils.to_categorical(y_train, num_classes)
|
|
|
|
y_test = keras.utils.to_categorical(y_test, num_classes)
|
|
|
|
print('y_train shape:', y_train.shape)
|
|
|
|
print('y_test shape:', y_test.shape)
|
2015-03-28 00:59:42 +00:00
|
|
|
|
2015-12-09 02:49:14 +00:00
|
|
|
print('Building model...')
|
2015-03-28 00:59:42 +00:00
|
|
|
model = Sequential()
|
2015-10-05 01:44:49 +00:00
|
|
|
model.add(Dense(512, input_shape=(max_words,)))
|
2015-03-28 00:59:42 +00:00
|
|
|
model.add(Activation('relu'))
|
|
|
|
model.add(Dropout(0.5))
|
2017-02-15 00:08:30 +00:00
|
|
|
model.add(Dense(num_classes))
|
2015-03-28 00:59:42 +00:00
|
|
|
model.add(Activation('softmax'))
|
|
|
|
|
2016-03-19 16:07:15 +00:00
|
|
|
model.compile(loss='categorical_crossentropy',
|
|
|
|
optimizer='adam',
|
|
|
|
metrics=['accuracy'])
|
2015-04-16 03:18:40 +00:00
|
|
|
|
2017-02-28 22:41:30 +00:00
|
|
|
history = model.fit(x_train, y_train,
|
2017-03-26 14:27:49 +00:00
|
|
|
batch_size=batch_size,
|
|
|
|
epochs=epochs,
|
|
|
|
verbose=1,
|
|
|
|
validation_split=0.1)
|
2017-02-28 22:41:30 +00:00
|
|
|
score = model.evaluate(x_test, y_test,
|
2016-03-19 16:07:15 +00:00
|
|
|
batch_size=batch_size, verbose=1)
|
2015-04-27 21:18:20 +00:00
|
|
|
print('Test score:', score[0])
|
|
|
|
print('Test accuracy:', score[1])
|