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

# # Reuters_B - Newswire Topic Classification
# The Reuters dataset contains 11,228 newswires from Reuters, labeled over 46 topics.   As with the IMDB dataset, each wire is encoded as a sequence of numbers.   
#
# Our task is to create a neural network that can classify which topic the piece of text came from. We will use an embedding layer to input the data. 
#
# Approach: Vectorise the sequence-of-words-integers as One-hot word vector

import numpy as np

from keras.datasets import reuters
from keras.preprocessing.sequence import pad_sequences
maxlen=1500 # specify the max number of words of each newswire you want
vocab_size=1000
(x_train, y_train), (x_test, y_test) = reuters.load_data(path="reuters.npz",
                                                         num_words=vocab_size, # use top 1000 frequent words
                                                         skip_top=5, # skip top frequency word
                                                         maxlen=None,
                                                         test_split=0.2,
                                                         seed=113,
                                                         start_char=1,
                                                         oov_char=2,
                                                         index_from=3)

x_train.shape

x_test.shape

x_train[0]

# +
#x_train = pad_sequences(x_train, maxlen=maxlen)
#x_test =  pad_sequences(x_test, maxlen=maxlen)
# -

from keras.preprocessing.text import Tokenizer

tokenizer = Tokenizer(num_words=maxlen)

# * The `sequences_to_matrix` bascially produces a binarised word-count vector, i.e. each column represnets a word and if that word appears in that newswire, it will give 1 to that column, regardless of how many times that word appears.
# * In simple terms we just call it one-hot word vector.

x_train = tokenizer.sequences_to_matrix(x_train, mode='binary')
x_test = tokenizer.sequences_to_matrix(x_test, mode='binary')

x_train.shape

x_test.shape

x_train

y_train

y_train.shape

y_train.max()

from keras.utils import np_utils # one hot encode the y-label

y_train = np_utils.to_categorical(y_train, 46)
y_test = np_utils.to_categorical(y_test, 46)

y_train

y_train.shape

y_test.shape

# ### Building Model: Fully-connected perceptrons layer

from keras.models import Sequential
from keras.layers import Dense, Dropout
model = Sequential()
model.add(Dense(256, input_shape=(maxlen,), activation='relu')) 
model.add(Dropout(0.5)) 
model.add(Dense(46, activation='softmax'))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['acc'])
model.summary()

history = model.fit(x_train, y_train, epochs=5, batch_size=32, validation_split=0.1)

score = model.evaluate(x_test, y_test)

score

print('The test accuracy is:', round(score[1]*100,2))

# ### References:
#
# 1. [keras/examples/reuters_mlp.py](https://github.com/keras-team/keras/blob/master/examples/reuters_mlp.py)

StarcoderdataJupyterScriptsDedupFiltered