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 the app to see the outputs
Press the run button in the top right corner
# load data
(train_data,train_labels), (test_data, test_labels) = imdb.load_data(num_words=10000)
# 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 the app to see the outputs
Press the run button in the top right corner
print(f"Review:{test_data[0]}\nLabel:{test_labels[0]}")
Run the app to see the outputs
Press the run button in the top right corner
# 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 the app to see the outputs
Press the run button in the top right corner
# 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)
# vectorize labels
y_train = np.asarray(train_labels).astype('float32')
y_test = np.asarray(test_labels).astype('float32')
# 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:]
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 the app to see the outputs
Press the run button in the top right corner
model.compile(optimizer=optimizers.RMSprop(learning_rate=0.001) ,loss=losses.binary_crossentropy, metrics=[metrics.binary_accuracy])
# 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 the app to see the outputs
Press the run button in the top right corner
# Plot the Loss and accuracy of function both training and validation
history_dict = history.history
history_dict.keys()
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)
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()
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()
model.save