In [1]:
import numpy as np
import keras
import os
from keras.datasets import reuters
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
from keras.preprocessing.text import Tokenizer
from keras.callbacks import TensorBoard
In [2]:
max_words = 1000
batch_size = 32
epochs = 5
In [3]:
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')
In [4]:
data = np.load('/Users/alorozco53/.keras/datasets/reuters.npz')
data['x'].shape, data['y'].shape
Out[4]:
In [5]:
num_classes = np.max(y_train) + 1
print(num_classes, 'classes')
In [6]:
# Vectorize 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)
In [7]:
# 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)
In [8]:
model = Sequential()
model.add(Dense(512, input_shape=(max_words,)))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes))
model.add(Activation('softmax'))
In [9]:
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['mae', 'acc'])
model.summary()
In [10]:
# callback config
logdir = 'mlp-logs'
if not os.path.exists(logdir):
os.makedirs(logdir)
tb = TensorBoard(log_dir=logdir, write_graph=True, histogram_freq=0, write_images=True)
In [11]:
# Finally, train
history = model.fit(x_train, y_train,
batch_size=batch_size,
epochs=epochs,
callbacks=[tb],
validation_split=0.1)
In [12]:
# Evaluation
score = model.evaluate(x_test, y_test,
batch_size=batch_size, verbose=1)
print('Test score:', score[0])
print('Test accuracy:', score[1])
In [13]:
# Save the model
model_path = os.path.join(logdir, 'model.h5')
model.save(model_path)
print('model saved in:', model_path)