This notebook demonstrates using Cloud TPUs in colab to build a simple regression model using y = sin(x) to predict y for given x.
Advantages:
In [0]:
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,0
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""An Example of a Regression model using Keras for the y = sin(x) dataset."""
import numpy as np
import tensorflow as tf
import tensorflow.keras as keras
import math
import os
import pprint
import matplotlib.pyplot as plt
from sklearn import metrics
In [0]:
print(tf.__version__)
In [0]:
use_tpu = True #@param {type:"boolean"}
if use_tpu:
assert 'COLAB_TPU_ADDR' in os.environ, 'Missing TPU; did you request a TPU in Notebook Settings?'
if 'COLAB_TPU_ADDR' in os.environ:
TF_MASTER = 'grpc://{}'.format(os.environ['COLAB_TPU_ADDR'])
else:
TF_MASTER=''
with tf.Session(TF_MASTER) as session:
print ('List of devices:')
pprint.pprint(session.list_devices())
In [0]:
data_size = 2**18
x = np.linspace(0, 6, data_size)
np.random.shuffle(x)
y = -20 * np.sin(x) + 3 + np.random.normal(0, 1, (data_size,))
x = x.reshape(-1, 1)
y = y.reshape(-1, 1)
train_x, test_x = x[:data_size/2], x[data_size/2:]
train_y, test_y = y[:data_size/2], y[data_size/2:]
plt.plot(x, y, 'bo')
Out[0]:
In [0]:
def get_model():
return keras.Sequential([
keras.layers.Dense(1, input_shape=(1,)),
keras.layers.Dense(200, activation=tf.nn.sigmoid),
keras.layers.Dense(80, activation=tf.nn.sigmoid),
keras.layers.Dense(1)
])
In [0]:
model = get_model()
model.compile(optimizer=tf.train.GradientDescentOptimizer(.01),
loss='mean_squared_error',
metrics=['mean_squared_error'])
In [0]:
tpu_model = tf.contrib.tpu.keras_to_tpu_model(
model,
strategy=tf.contrib.tpu.TPUDistributionStrategy(
tf.contrib.cluster_resolver.TPUClusterResolver(TF_MASTER)))
In [0]:
tpu_model.fit(train_x, train_y, epochs=10, steps_per_epoch=512)
tpu_model.save_weights('/tmp/sine.h5', overwrite=True)
In [0]:
prediction_model = get_model()
prediction_model.load_weights('/tmp/sine.h5')
In [0]:
predictions = prediction_model.predict(test_x)
plt.plot(test_x, predictions, 'ro')
Out[0]:
In [0]:
slow_model = get_model()
slow_model.compile(optimizer=tf.train.GradientDescentOptimizer(.01),
loss='mean_squared_error',
metrics=['mean_squared_error'])
slow_model.fit(train_x, train_y, epochs=10, steps_per_epoch=512)
Out[0]: