We show some simple examples of how to use TF 2.0 via the keras interface.
Make sure you select 'GPU' from the 'Runtime' tab at the top of this page.
For more details, see
In [1]:
# Standard Python libraries
from __future__ import absolute_import, division, print_function, unicode_literals
import os
import time
import numpy as np
import glob
import matplotlib.pyplot as plt
import PIL
import imageio
from IPython import display
import sklearn
import seaborn as sns;
sns.set(style="ticks", color_codes=True)
import pandas as pd
pd.set_option('precision', 2) # 2 decimal places
pd.set_option('display.max_rows', 20)
pd.set_option('display.max_columns', 30)
pd.set_option('display.width', 100) # wide windows
# Check we can plot stuff
plt.figure()
plt.plot(range(10))
# We can make some wrappers around random number generation
# so it works even if we switch from numpy to JAX
import numpy as onp # original numpy
def set_seed(seed):
onp.random.seed(seed)
def randn(*args):
return onp.random.randn(*args)
def randperm(args):
return onp.random.permutation(args)
In [2]:
# Tensorflow 2.0
try:
# %tensorflow_version only exists in Colab.
%tensorflow_version 2.x
except Exception:
pass
import tensorflow as tf
from tensorflow import keras
print("tf version {}".format(tf.__version__))
if tf.test.is_gpu_available():
print(tf.test.gpu_device_name())
else:
print("TF cannot find GPU")
In [0]:
# Fashion MNIST has the same size and shape as MNIST, but the images
# are of items of clothing, rather than handwritten digits.
# Fashion-MNIST is slightly harder to than regular MNIST.
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
train_images = train_images / 255.0
test_images = test_images / 255.0
print(np.shape(train_images))
print(np.shape(test_images))
In [0]:
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i], cmap=plt.cm.binary)
plt.xlabel(class_names[train_labels[i]])
#save_fig("fashion-mnist-data.pdf")
plt.show()
In [0]:
from time import time
model = keras.Sequential([
keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation=tf.nn.relu),
keras.layers.Dense(10, activation=tf.nn.softmax)
])
# We use sparse categorical cross entropy since the labels are integers, not dense one-hot vectors.
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
# We just train for 2 epochs because it is faster, and
# and because it produces more errors, which makes for a more interesting plot :)
time_start = time()
model.fit(train_images, train_labels, epochs=2)
print('time spent training {:0.3f}'.format(time() - time_start))
In [0]:
# Overall accuracy
train_loss, train_acc = model.evaluate(train_images, train_labels)
print('Train accuracy:', train_acc)
test_loss, test_acc = model.evaluate(test_images, test_labels)
print('Test accuracy:', test_acc)
In [0]:
predictions = model.predict(test_images)
print(np.shape(predictions))
# To apply prediction to a single image, we need to reshape to an (N,D,D) tensor where N=1
img = test_images[0]
img = (np.expand_dims(img,0))
print(img.shape)
predictions_single = model.predict(img)
print(predictions_single.shape)
In [0]:
def plot_image_and_label(predictions_array, true_label, img):
plt.grid(False)
plt.xticks([])
plt.yticks([])
plt.imshow(img, cmap=plt.cm.binary)
predicted_label = np.argmax(predictions_array)
if predicted_label == true_label:
color = 'blue'
plt.xlabel("{} {:2.0f}%".format(class_names[predicted_label],
100*np.max(predictions_array)),
color=color)
else:
color = 'red'
plt.xlabel("{} {:2.0f}% ({})".format(class_names[predicted_label],
100*np.max(predictions_array),
class_names[true_label]),
color=color)
def plot_label_dist(predictions_array, true_label):
plt.grid(False)
plt.xticks([])
plt.yticks([])
thisplot = plt.bar(range(10), predictions_array, color="#777777")
plt.ylim([0, 1])
predicted_label = np.argmax(predictions_array)
thisplot[predicted_label].set_color('red')
thisplot[true_label].set_color('blue')
# Plot the first 15 test images, their predicted label, and the true label
# Color correct predictions in blue, incorrect predictions in red
num_rows = 5
num_cols = 3
num_images = num_rows*num_cols
plt.figure(figsize=(2*2*num_cols, 2*num_rows))
for i in range(num_images):
plt.subplot(num_rows, 2*num_cols, 2*i+1)
plot_image_and_label(predictions[i], test_labels[i], test_images[i])
plt.subplot(num_rows, 2*num_cols, 2*i+2)
plot_label_dist(predictions[i], test_labels[i])
#save_fig("fashion-mnist-predictions.pdf")
plt.show()
We use the IMDB movie review dataset, where the task is to classify the sentiment of the review as positive or negative. We use the preprocessed version of the dataset from https://www.tensorflow.org/datasets
In [0]:
import tensorflow_datasets as tfds
# TFDS version is slow
if 0:
imdb, info = tfds.load('imdb_reviews', with_info=True, as_supervised=True) # Takes about 1 minute in colab!
#imdb, info = tfds.load('imdb_reviews/subwords32k', with_info=True, as_supervised=True) # Takes forever!!
tokenizer = info.features['text'].encoder
print(info)
train_dataset, test_data = imdb['train'], imdb['test']
prefix = train_dataset.batch(1).take(3)
for data, label in prefix:
print(data)
print(tokenizer.decode(data.numpy()[0]))
In [0]:
# Keras version of IMDB is much faster
imdb = keras.datasets.imdb
vocab_size = 10000
(train_data, train_labels), (test_data, test_labels) = imdb.load_data(num_words=vocab_size)
print(np.shape(train_data)) # (25000)
print(train_data[0])
# [1, 14, 22, 16, 43, 530, 973, 1622, 1385, 65, 458, 4468, 66, 3941...]
word_index = imdb.get_word_index()
word_index = {k:(v+3) for k,v in word_index.items()}
word_index["<PAD>"] = 0
word_index["<START>"] = 1
word_index["<UNK>"] = 2 # unknown
word_index["<UNUSED>"] = 3
reverse_word_index = dict([(value, key) for (key, value) in word_index.items()])
def decode_review(text):
return ' '.join([reverse_word_index.get(i, '?') for i in text])
print(decode_review(train_data[0]))
In [0]:
# Keras padding - every example in the dataset has fixed length
train_data = keras.preprocessing.sequence.pad_sequences(
train_data, value=word_index["<PAD>"], padding='post', maxlen=256)
test_data = keras.preprocessing.sequence.pad_sequences(
test_data, value=word_index["<PAD>"], padding='post', maxlen=256)
print(train_data.shape)
print(train_data[0])
In [0]:
embed_size = 16
def make_model(embed_size):
tf.random.set_seed(42)
np.random.seed(42)
model = keras.Sequential()
model.add(keras.layers.Embedding(vocab_size, embed_size))
model.add(keras.layers.GlobalAveragePooling1D())
model.add(keras.layers.Dense(16, activation=tf.nn.relu))
model.add(keras.layers.Dense(1, activation=tf.nn.sigmoid))
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['acc'])
return model
model = make_model(embed_size)
model.summary()
In [0]:
x_val = train_data[:10000]
x_train = train_data[10000:]
y_val = train_labels[:10000]
y_train = train_labels[10000:]
history = model.fit(x_train,
y_train,
epochs=30,
batch_size=512,
validation_data=(x_val, y_val),
verbose=1)
In [0]:
history_dict = history.history
print(history_dict.keys())
results = model.evaluate(test_data, test_labels)
print(results)
In [0]:
acc = history_dict['acc']
val_acc = history_dict['val_acc']
loss = history_dict['loss']
val_loss = history_dict['val_loss']
epochs = range(1, len(acc) + 1)
fig, ax = plt.subplots()
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'r-', label='Validation loss')
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
#save_fig("imdb-loss.pdf")
plt.show()
In [0]:
fig, ax = plt.subplots()
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'r', label='Validation acc')
plt.title('Training and validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
#save_fig("imdb-acc.pdf")
plt.show()
In [0]:
# Now turn on early stopping
# https://chrisalbon.com/deep_learning/keras/neural_network_early_stopping/
# Also, make the logging per iteration less verbose (show progress dots...)
class PrintDot(keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs):
if epoch % 100 == 0: print('')
print('.', end='')
callbacks = [PrintDot(),
keras.callbacks.EarlyStopping(monitor='val_acc', patience=2),
keras.callbacks.ModelCheckpoint(filepath='imdb_keras_best_model.ckpt',
monitor='val_acc', save_best_only=True)]
In [0]:
# Reset parameters to a new random state
model = make_model(embed_size)
history = model.fit(
x_train, y_train, epochs=50, batch_size=512,
validation_data=(x_val, y_val), verbose=0, callbacks=callbacks)
history_dict = history.history
acc = history_dict['acc']
val_acc = history_dict['val_acc']
loss = history_dict['loss']
val_loss = history_dict['val_loss']
epochs = range(1, len(acc) + 1)
fig, ax = plt.subplots()
plt.plot(epochs, loss, 'bo', label='Training loss')
plt.plot(epochs, val_loss, 'r-', label='Validation loss')
plt.title('Training and validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
#save_fig("imdb-loss-early-stop.pdf")
plt.show()
Tensorflow has an easy way to load data, and convert it into a stream of miniatches, as we show below.
The functionality similar functionality to PyTorch DataLoader, but natively supports infinite streams via the repeat
function. Also, all minibatches have the same size (note how we 'wrap around' the dataset).
In [3]:
N_train = 5
D = 4
set_seed(0)
X = randn(N_train, D)
y = randn(N_train)
batch_size = 2
dataset = tf.data.Dataset.from_tensor_slices({"X": X, "y": y})
batches = dataset.repeat().batch(batch_size)
step = 0
num_minibatches = 4
for batch in batches:
if step >= num_minibatches:
break
x, y = batch["X"].numpy(), batch["y"].numpy()
print(y)
step = step + 1
TF also has preprocessed datasets, available from https://www.tensorflow.org/datasets
In [4]:
import tensorflow_datasets as tfds
dataset = tfds.load(name="mnist", split=tfds.Split.TRAIN)
batches = dataset.repeat().batch(batch_size)
step = 0
for batch in batches:
if step >= num_minibatches:
break
x, y = batch['image'], batch['label']
#print(batch)
print(y)
step = step + 1
In [0]: