In [1]:
from __future__ import absolute_import
from __future__ import print_function
import numpy as np

from keras.datasets import reuters
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation
from keras.layers.normalization import BatchNormalization
from keras.utils import np_utils
from keras.preprocessing.text import Tokenizer

In [2]:
max_words = 1000
batch_size = 32

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


Loading data...
Downloading data from https://s3.amazonaws.com/text-datasets/reuters.pkl
8765440/8761704 [==============================] - 48s    
8982 train sequences
2246 test sequences

In [11]:
print (X_train[0])
print (len(X_train[0]))
print (y_train[0])


[635, 186, 147, 4, 0, 31, 186, 1, 267, 485, 0, 0, 320, 96, 187, 1, 0, 65, 1, 0, 205, 22, 0, 31, 10, 6, 639, 0, 5, 0, 16, 674, 0, 2, 0, 59, 4, 0, 890, 26, 9, 76, 0, 2, 164, 844, 13, 12]
48
1

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


46 classes

In [13]:
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)


Vectorizing sequence data...
X_train shape: (8982, 1000)
X_test shape: (2246, 1000)

In [14]:
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)


Convert class vector to binary class matrix (for use with categorical_crossentropy)
Y_train shape: (8982, 46)
Y_test shape: (2246, 46)

In [15]:
print("Building model...")
model = Sequential()
model.add(Dense(max_words, 256, init='normal'))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(256, nb_classes, init='normal'))
model.add(Activation('softmax'))


Building model...

In [16]:
model.compile(loss='categorical_crossentropy', optimizer='adam')

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


Train on 8083 samples, validate on 899 samples
Epoch 0
8083/8083 [==============================] - 4s - loss: 1.6220 - val. loss: 1.2764
Epoch 1
8083/8083 [==============================] - 4s - loss: 1.1066 - val. loss: 1.1167
Epoch 2
8083/8083 [==============================] - 3s - loss: 0.9484 - val. loss: 1.0230
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-17-9b13ee9d713a> in <module>()
      1 history = model.fit(X_train, Y_train, nb_epoch=3, batch_size=batch_size, verbose=1, show_accuracy=False, validation_split=0.1)
----> 2 print(history.epoch)
      3 print(history.loss)
      4 print(history.accuracy)
      5 print(history.validation_loss)

AttributeError: 'dict' object has no attribute 'epoch'

In [24]:
print (history['epoch'])
print (history['loss'])
print (history['val_loss'])


[0, 1, 2]
[1.6219545829384452, 1.1065756057109553, 0.94837707218051959]
[1.276415767390222, 1.1166925571716353, 1.023049911034189]

In [28]:
score = model.evaluate(X_test, Y_test, batch_size=batch_size, verbose=1, show_accuracy=False)
print('Test score:', score)


2246/2246 [==============================] - 0s - loss: 0.9787     
Test score: 0.981714053701

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