1. Multi-layer Perceptron

Train and evaluate a simple MLP on the Reuters newswire topic classification task.

This is a collection of documents that appeared on Reuters newswire in 1987. The documents were assembled and indexed with categories.

Dataset of 11,228 newswires from Reuters, labeled over 46 topics. As with the IMDB dataset, each wire is encoded as a sequence of word indexes (same conventions).

Each wire is encoded as a sequence of word indexes (integers). For convenience, words are indexed by overall frequency in the dataset, so that for instance the integer "3" encodes the 3rd most frequent word in the data. This allows for quick filtering operations such as: "only consider the top 10,000 most common words, but eliminate the top 20 most common words".

As a convention, "0" does not stand for a specific word, but instead is used to encode any unknown word.

Source: https://archive.ics.uci.edu/ml/datasets/Reuters-21578+Text+Categorization+Collection


In [ ]:
# Reuters data

In [ ]:
from __future__ import print_function
import numpy as np
np.random.seed(1337)  # for reproducibility

In [ ]:
#Import keras
from keras.datasets import reuters
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.utils import np_utils
from keras.preprocessing.text import Tokenizer

In [ ]:
max_words = 1000
batch_size = 32
nb_epoch = 5

In [ ]:
import os
path_to_data = os.path.abspath(os.path.join('..', 'data', 'reuters.pkl'))

In [ ]:
print('Loading data...')
(X_train, y_train), (X_test, y_test) = reuters.load_data(path_to_data, nb_words=max_words, test_split=0.2)
print(len(X_train), 'train sequences')
print(len(X_test), 'test sequences')

In [ ]:
nb_classes = np.max(y_train)+1
print(nb_classes, 'classes')

In [ ]:
print('Vectorizing sequence data...')
tokenizer = Tokenizer(nb_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)

In [ ]:
print('Convert class vector to binary class matrix (for use with categorical_crossentropy)')
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
print('Y_train shape:', Y_train.shape)
print('Y_test shape:', Y_test.shape)

In [ ]:
print('Building model...')
model = Sequential()
model.add(Dense(512, input_shape=(max_words,)))
model.add(Activation('relu'))
model.add(Dense(nb_classes))
model.add(Activation('softmax'))

In [ ]:
model.compile(loss='categorical_crossentropy',
              optimizer='sgd',
              metrics=['accuracy'])

In [ ]:
history = model.fit(X_train, Y_train,
                    nb_epoch=nb_epoch, batch_size=batch_size,
                    verbose=1, validation_split=0.1)

In [ ]:
score = model.evaluate(X_test, Y_test,
                       batch_size=batch_size, verbose=1)

In [ ]:
print('Test score:', score[0])
print('Test accuracy:', score[1])

Exercise

  1. Add more dense layers: Try with one more dense layer. Then with two dense layers. Evaluate accuracy

  2. Add dropout using the following code and evaluate accuracy:

model.add(Dropout(0.5))


In [ ]: