# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/keivanipchihagh/Intro_To_MachineLearning/blob/master/Models/Newswires_Classification_with_Reuters.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="riErnUwfLKDw"
# # Newswires Classification with Reuters

# + [markdown] id="s5T_EHZXNShT"
# ##### Imports

# + id="1UdxYS9ALHTE"
import numpy as np                                # Numpy
from matplotlib import pyplot as plt              # Matplotlib
import keras                                      # Keras
import pandas as pd                               # Pandas
from keras.datasets import reuters                # Reuters Dataset
from keras.utils.np_utils import to_categorical   # Categirical Classifier
import random                                     # Random

# + [markdown] id="0ptvrGNGNpoD"
# ##### Load dataset

# + id="QFnLXk66NryE"
(train_data, train_labels), (test_data, test_labels) = reuters.load_data(num_words = 10000)
print('Size:', len(train_data))
print('Training Data:', train_data[0])


# + [markdown] id="eBtaode-Tu1_"
# ##### Get the feel of data

# + id="jAg-wM4ITxA6"
def decode(index):  # Decoding the sequential integers into the corresponding words
  word_index = reuters.get_word_index()
  reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
  decoded_newswire = ' '.join([reverse_word_index.get(i - 3, '?') for i in test_data[0]])
  return decoded_newswire

print("Decoded test data sample [0]: ", decode(0))


# + [markdown] id="XGUDyWwXTpar"
# ##### Data Prep (One-Hot Encoding)

# + id="kl3MVkNxTsU0"
def vectorize_sequences(sequences, dimension = 10000):    # Encoding the integer sequences into a binary matrix
  results = np.zeros((len(sequences), dimension))
  for i, sequence in enumerate(sequences):
    results[i, sequence] = 1.
  return results

train_data = vectorize_sequences(train_data)
test_data = vectorize_sequences(test_data)

train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)

# + [markdown] id="I0AS2vWYT9au"
# ##### Building the model

# + id="6JUWJ32YT_iy"
model = keras.models.Sequential()
model.add(keras.layers.Dense(units = 64, activation = 'relu', input_shape = (10000,)))
model.add(keras.layers.Dense(units = 64, activation = 'relu'))
model.add(keras.layers.Dense(units = 46, activation = 'softmax'))
model.compile( optimizer = 'rmsprop', loss = 'categorical_crossentropy', metrics = ['accuracy'])
model.summary()

# + [markdown] id="o7_1oOMZUeur"
# ##### Training the model

# + id="f8y3-hzfUhQS"
x_val = train_data[:1000]
train_data = train_data[1000:]
y_val = train_labels[:1000]
train_labels = train_labels[1000:]

history = model.fit(train_data, train_labels, batch_size = 512, epochs = 10, validation_data = (x_val, y_val), verbose = False)

# + [markdown] id="jO0xF9RMVILe"
# ##### Evaluating the model

# + id="Hy2nZqQyVKJU"
result = model.evaluate(train_data, train_labels)
print('Loss:', result[0])
print('Accuracy:', result[1] * 100)

# + [markdown] id="Bo60EKVGVZbM"
# ##### Statistics

# + id="CQhxXomBVa3P"
epochs = range(1, len(history.history['loss']) + 1)
plt.plot(epochs, history.history['loss'], 'b', label = 'Training Loss')
plt.plot(epochs, history.history['val_loss'], 'r', label = 'Validation Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()

plt.clf()
plt.plot(epochs, history.history['accuracy'], 'b', label = 'Training Accuracy')
plt.plot(epochs, history.history['val_accuracy'], 'r', label = 'Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()

# + [markdown] id="Uvoc0-o6WQxC"
# ##### Making predictions

# + id="Z360IVlBWSKt"
prediction_index = random.randint(0, len(test_data))
prediction_data = test_data[prediction_index]
decoded_prediction_data = decode(prediction_index)

# Info
print('Random prediction index:', prediction_index)
print('Original prediction Data:', prediction_data)
print('Decoded prediction Data:', decoded_prediction_data)
print('Expected prediction label:', np.argmax(test_labels[prediction_index]))

# Prediction
predictions = model.predict(test_data)
print('Prediction index: ', np.argmax(predictions[prediction_index]))

StarcoderdataJupyterScriptsDedupFiltered