In [1]:
from keras.utils import plot_model


Using TensorFlow backend.

In [2]:
'''Trains and evaluate a simple MLP
on the Reuters newswire topic classification task.
'''
from __future__ import print_function

import numpy as np
import keras
from keras.datasets import reuters
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.preprocessing.text import Tokenizer

max_words = 1000
batch_size = 32
epochs = 5

print('Loading data...')
(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')

num_classes = np.max(y_train) + 1
print(num_classes, 'classes')

print('Vectorizing sequence data...')
tokenizer = Tokenizer(num_words=max_words)
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)

print('Building model...')
model = Sequential()
model.add(Dense(512, input_shape=(max_words,)))
model.add(Dropout(0.2))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))


Loading data...
8982 train sequences
2246 test sequences
46 classes
Vectorizing sequence data...
x_train shape: (8982, 1000)
x_test shape: (2246, 1000)
Convert class vector to binary class matrix (for use with categorical_crossentropy)
y_train shape: (8982, 46)
y_test shape: (2246, 46)
Building model...

In [3]:
%time

model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

history = model.fit(x_train, y_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
                    validation_split=0.1)
score = model.evaluate(x_test, y_test,
                       batch_size=batch_size, verbose=1)
print('Test score:', score[0])
print('Test accuracy:', score[1])
plot_model(model, to_file='model.png')


CPU times: user 2 µs, sys: 0 ns, total: 2 µs
Wall time: 4.53 µs
Train on 8083 samples, validate on 899 samples
Epoch 1/5
8083/8083 [==============================] - 3s - loss: 1.4582 - acc: 0.6704 - val_loss: 1.2260 - val_acc: 0.7442
Epoch 2/5
8083/8083 [==============================] - 3s - loss: 0.9869 - acc: 0.7872 - val_loss: 1.1579 - val_acc: 0.7642
Epoch 3/5
8083/8083 [==============================] - 3s - loss: 0.8224 - acc: 0.8237 - val_loss: 1.1424 - val_acc: 0.7898
Epoch 4/5
8083/8083 [==============================] - 3s - loss: 0.7168 - acc: 0.8497 - val_loss: 1.1364 - val_acc: 0.7864
Epoch 5/5
8083/8083 [==============================] - 3s - loss: 0.6359 - acc: 0.8689 - val_loss: 1.1486 - val_acc: 0.7964
2240/2246 [============================>.] - ETA: 0sTest score: 1.0966974948
Test accuracy: 0.790739091719

In [ ]:


In [ ]: