Neural Networks
Hello world example: Classification of IMDB movie reviews
# load libraries
from keras.datasets import imdb
import numpy as np
from keras import models, layers, optimizers, losses, metrics
import matplotlib.pyplot as plt
Run to view results
# load data
(train_data,train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
Run to view results
# take a look of the one row of traning data to have an understanding of how it looks like.
print(f"review: {train_data[0]}\nlabel:{train_labels[0]}")
Run to view results
print(f"Review:{test_data[0]}\nLabel:{test_labels[0]}")
Run to view results
# here first we need to get the word indicies and then reverse/decode indicies back to english
word_index = imdb.get_word_index()
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
decode_review_train = ' '.join(reverse_word_index.get(i - 3, '?') for i in train_data[0])
decode_review_test = ' '.join(reverse_word_index.get(i - 3, '?') for i in test_data[0])
print(f"Train review:{decode_review_train}\nTest review:{decode_review_test}")
Run to view results
# function to covert sequence into vector of same size in this case 10,000
def vector_sequence(sequence, dimension = 10000):
results = np.zeros((len(sequence), dimension))
for i, seq in enumerate(sequence):
results[i, seq] = 1
return results
x_train = vector_sequence(train_data)
x_test = vector_sequence(test_data)
Run to view results
# vectorize labels
y_train = np.asarray(train_labels).astype('float32')
y_test = np.asarray(test_labels).astype('float32')
Run to view results
# Now we build our network
# no of layers: 2 with 16 hidden units and 1 output layer using sequential api
# Activation relu and sigmoid; optomizer = RMSprop and loss binary_crossentropy and metric: accuracy
# get a validation set to validate the training.
x_val = x_train[:10000]
partial_x_train = x_train[10000:]
y_val = y_train[:10000]
partial_y_train = y_train[10000:]
Run to view results
model = models.Sequential()
model.add(layers.Dense(16, activation='relu', input_shape=(10000, )))
model.add(layers.Dense(16, activation='relu'))
model.add(layers.Dense(1, activation='sigmoid'))
Run to view results
model.compile(optimizer=optimizers.RMSprop(learning_rate=0.001) ,loss=losses.binary_crossentropy, metrics=[metrics.binary_accuracy])
Run to view results
# Training the modle and saving the state
history = model.fit(x=partial_x_train, y=partial_y_train,
batch_size=512, epochs=10, validation_data=(x_val, y_val))
Run to view results
# Plot the Loss and accuracy of function both training and validation
history_dict = history.history
history_dict.keys()
Run to view results
loss_values = history_dict['loss']
val_loss_values = history_dict['val_loss']
acc_values = history_dict['acc']
val_acc_values = history_dict['val_acc']
epochs = range(1, len(acc) + 1)
Run to view results
plt.clf()
plt.plot(epochs, loss_values, 'bo', label='Train loss')
plt.plot(epochs, val_loss_values, 'b', label='Vlidation loss')
plt.title('Training and validation loss curves.')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
Run to view results
plt.plot(epochs, acc_values, 'bo', label='Train acc')
plt.plot(epochs, val_acc_values, 'b', label='Vlidation acc')
plt.title('Training and validation accuracy curves.')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
Run to view results
model.save
Run to view results