0% found this document useful (0 votes)
29 views4 pages

Multiclass Classification - Ipynb - Colab

Uploaded by

Kavya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
29 views4 pages

Multiclass Classification - Ipynb - Colab

Uploaded by

Kavya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

from keras.

datasets import reuters


(train_data, train_labels), (test_data, test_labels) = reuters.load_data(
num_words=10000)

Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-dataset


2110848/2110848 [==============================] - 0s 0us/step

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
train_data[0]])

Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-dataset


550378/550378 [==============================] - 0s 0us/step

import numpy as np
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)
def to_one_hot(labels, dimension=46):
results = np.zeros((len(labels), dimension))
for i, label in enumerate(labels):
results[i, label] = 1.
return results
one_hot_train_labels = to_one_hot(train_labels)
one_hot_test_labels = to_one_hot(test_labels)
from keras.utils import to_categorical
one_hot_train_labels = to_categorical(train_labels)
one_hot_test_labels = to_categorical(test_labels)

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'])

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))

Epoch 1/20
16/16 [==============================] - 4s 141ms/step - loss: 2.6550 - accuracy
Epoch 2/20
16/16 [==============================] - 2s 108ms/step - loss: 1.4717 - accuracy
Epoch 3/20
16/16 [==============================] - 1s 52ms/step - loss: 1.1324 - accuracy
Epoch 4/20
16/16 [==============================] - 1s 55ms/step - loss: 0.9230 - accuracy
Epoch 5/20
16/16 [==============================] - 1s 51ms/step - loss: 0.7631 - accuracy
Epoch 6/20
16/16 [==============================] - 1s 89ms/step - loss: 0.6354 - accuracy
Epoch 7/20
16/16 [==============================] - 1s 89ms/step - loss: 0.5282 - accuracy
Epoch 8/20
16/16 [==============================] - 1s 47ms/step - loss: 0.4441 - accuracy
Epoch 9/20
16/16 [==============================] - 1s 52ms/step - loss: 0.3734 - accuracy
Epoch 10/20
16/16 [==============================] - 1s 51ms/step - loss: 0.3181 - accuracy
Epoch 11/20
16/16 [==============================] - 1s 53ms/step - loss: 0.2774 - accuracy
Epoch 12/20
16/16 [==============================] - 1s 54ms/step - loss: 0.2387 - accuracy
Epoch 13/20
16/16 [==============================] - 1s 47ms/step - loss: 0.2152 - accuracy
Epoch 14/20
16/16 [==============================] - 1s 54ms/step - loss: 0.1946 - accuracy
Epoch 15/20
16/16 [==============================] - 1s 46ms/step - loss: 0.1812 - accuracy
Epoch 16/20
16/16 [==============================] - 1s 47ms/step - loss: 0.1632 - accuracy
Epoch 17/20
16/16 [==============================] - 1s 54ms/step - loss: 0.1497 - accuracy
Epoch 18/20
16/16 [==============================] - 1s 47ms/step - loss: 0.1444 - accuracy
Epoch 19/20
16/16 [==============================] - 1s 45ms/step - loss: 0.1393 - accuracy
Epoch 20/20
16/16 [==============================] - 1s 62ms/step - loss: 0.1314 - accuracy

import matplotlib.pyplot as plt


loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(1, len(loss) + 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()

You might also like