<jupyter_start><jupyter_text>Doing single-label multiclass classification on the Reuters dataset<jupyter_code>from keras import models
from keras import layers
import numpy as np
import matplotlib.pyplot as plt
# We keep the 10000 most frequently occurring words
from keras.datasets import reuters
(train_data, train_labels), (test_data, test_labels) = reuters.load_data(num_words=10000)
# Number of training and test samples
print len(train_data)
print len(test_data)
# Example of data
print train_data[10]
# Example of label (between 0 and 45)
print train_labels[10]
# Vectorize the data
def vectorize_sequences(sequences, dimension=10000):
    results = np.zeros((len(sequences), dimension))
    for i, sequence in enumerate(sequences):
        results[i, sequence] = 1
    return results

x_train = vectorize_sequences(train_data)
x_test = vectorize_sequences(test_data)

print x_train[10]
# Vectorize the labels using categorical encoding
from keras.utils.np_utils import to_categorical

one_hot_train_labels = to_categorical(train_labels)
one_hot_test_labels = to_categorical(test_labels)

print one_hot_train_labels[10]
# Build the network
model = models.Sequential()
model.add(layers.Dense(32, activation='relu', input_shape=(10000,)))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(46, activation='softmax'))

model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy'])
# Create a validation dataset
x_val = x_train[:1000]
partial_x_train = x_train[1000:]
y_val = one_hot_train_labels[:1000]
partial_y_train = one_hot_train_labels[1000:]
history = model.fit(partial_x_train,partial_y_train,epochs=20,batch_size=512,validation_data=(x_val, y_val))
# Plot the loss curves

history_dict = history.history
loss_values = history_dict['loss']
val_loss_values = history_dict['val_loss']
epochs = range(1, len(loss_values) + 1)

plt.plot(epochs, loss_values, 'b', label='Training loss')
plt.plot(epochs, val_loss_values, 'r', label='Validation loss')
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
# Retrain for 10 epochs (to avoid overfitting)

model2 = models.Sequential()
model2.add(layers.Dense(32, activation='relu', input_shape=(10000,)))
model2.add(layers.Dense(64, activation='relu'))
model2.add(layers.Dense(46, activation='softmax'))

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

history = model2.fit(partial_x_train,partial_y_train,epochs=10,batch_size=512,validation_data=(x_val, y_val))
# Evaluate on the test set (78% accuracy)
results = model2.evaluate(x_test, one_hot_test_labels)
print results
# Generate predictions for the test set and print an example for data #10
predictions = model.predict(x_test)
print predictions[10]
print np.sum(predictions[10])
# To identify the predicted class of a sample, take the highest probability
np.argmax(predictions[10])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11189753960229187, 'fraction_numerical': 0.032692955847657565, 'mean_word_length': 7.966767371601208, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '165429', 'license': 'MIT', 'n_tokens_mistral': 1056, 'n_tokens_neox': 971, 'n_words': 229, 'path': '05_Multiclass_Classification_Reuters_Newswires.ipynb', 'repo_name': 'alzaia/keras_projects', 'size': '2967'}