<jupyter_start><jupyter_text>Encoding the integer sequeneces into a binary matrix<jupyter_code>import numpy as np

def vectorize_sequences( sequences, dimension =10000):
    #Create an all-zero matrix of shape (len(sequences), dimensions)
    results = np.zeros((len(sequences), dimension))
    for i,sequence in enumerate(sequences):
        results[i, sequence] = 1. #set specific indices of results[i] to 1s
    return results

#Our vectorized training data
x_train = vectorize_sequences(train_data)
#Our vectorized test data
x_test = vectorize_sequences(test_data)<jupyter_output><empty_output><jupyter_text>One-hot encoding the labels<jupyter_code>from keras.utils.np_utils import to_categorical

#vectorized training labels
one_hot_train_labels = to_categorical(train_labels)
#vectorized test labels
one_hot_test_labels = to_categorical(test_labels)<jupyter_output><empty_output><jupyter_text>Model<jupyter_code>from keras import models
from keras import layers

model = models.Sequential()
model.add(layers.Dense(64, 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'])
# Setting aside validation set
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=9, batch_size= 512,
                   validation_data= (x_val, y_val))<jupyter_output>Train on 7982 samples, validate on 1000 samples
Epoch 1/9
7982/7982 [==============================] - 3s 368us/step - loss: 2.5322 - acc: 0.4955 - val_loss: 1.7208 - val_acc: 0.6120
Epoch 2/9
7982/7982 [==============================] - 1s 135us/step - loss: 1.4451 - acc: 0.6878 - val_loss: 1.3459 - val_acc: 0.7060
Epoch 3/9
7982/7982 [==============================] - 1s 144us/step - loss: 1.0953 - acc: 0.7651 - val_loss: 1.1708 - val_acc: 0.7430
Epoch 4/9
7982/7982 [==============================] - 1s 135us/step - loss: 0.8696 - acc: 0.8161 - val_loss: 1.0787 - val_acc: 0.7590
Epoch 5/9
7982/7982 [==============================] - 1s 123us/step - loss: 0.7033 - acc: 0.8478 - val_loss: 0.9842 - val_acc: 0.7820
Epoch 6/9
7982/7982 [==============================] - 1s 125us/step - loss: 0.5665 - acc: 0.8802 - val_loss: 0.9416 - val_acc: 0.8030
Epoch 7/9
7982/7982 [==============================] - 1s 154us/step - loss: 0.4581 - acc: 0.9045 - val_loss: 0.9070 - val_acc: 0.8020
Epoch 8[...]<jupyter_text>Plotting the training and validation loss<jupyter_code>import matplotlib.pyplot as plt
%matplotlib inline

acc= history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']

epochs = range(1, len(acc) +1)

plt.plot(epochs, loss, 'bo', label='Training Loss')
plt.plot(epochs, val_loss, 'b', label= 'Validation loss')
plt.title('Training and Validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
plt.plot(epochs, acc, 'bo', label='Training Accuracy')
plt.plot(epochs, val_acc, 'b', label= 'Validation Accuracy')
plt.title('Training and Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Results<jupyter_code>results = model.evaluate(x_test, one_hot_test_labels)
results<jupyter_output>2246/2246 [==============================] - 0s 222us/step<jupyter_text>Prediction<jupyter_code>model.predict(x_test)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18995030369961347, 'fraction_numerical': 0.08255107675317504, 'mean_word_length': 5.734200743494424, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '340381', 'license': 'Apache-2.0', 'n_tokens_mistral': 1530, 'n_tokens_neox': 1284, 'n_words': 279, 'path': 'Tensorflow_Keras/Newswires/Reuters.ipynb', 'repo_name': 'SomaKorada07/Deep-Learning-Projects', 'size': '3622'}