<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 <a href="https
Step1:  A simple regression model using Keras with Cloud TPUs
Overview
This notebook demonstrates using Cloud TPUs in colab to build a simple regression model using y = sin(x) to predict y for given x.
This model generates huge amounts of data that and demonstrates the training performance advantage of Cloud TPU.
The model trains for 10 epochs with 512 steps per epoch on TPU and completes in approximately 2 minutes.
This notebook is hosted on GitHub. To view it in its original repository, after opening the notebook, select File > View on GitHub.
Learning objectives
In this Colab, you will learn how to
Step2:  Resolve TPU Address
Step3:  Creating data for y = sin(x).
Sine wave data is created using numpy. And to make it more difficult, random noise is added to the sine wave.
Step4:  Define model
Step5:  Compiling the model with a distribution strategy
To make the model usable by a TPU, we first must create and compile it using a distribution strategy.
Step6:  Training of the model on TPU
Step7:  Prediction
For predictions, same model weights are being used.
<ASSISTANT_TASK:>
Python Code:
# Copyright 2018 The TensorFlow Hub 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,
# 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.
# ==============================================================================
Explanation: <a href="https://colab.research.google.com/github/tensorflow/tpu/blob/0e3cfbdfbcf321681c1ac1c387baf7a1a41d8d21/tools/colab/regression_sine_data_with_keras.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>
Copyright 2018 The TensorFlow Hub Authors.
Licensed under the Apache License, Version 2.0 (the "License");
End of explanation
import math
import os
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
print(tf.__version__)
import distutils
if distutils.version.LooseVersion(tf.__version__) < '2.0':
    raise Exception('This notebook is compatible with TensorFlow 2.0 or higher.')
Explanation: A simple regression model using Keras with Cloud TPUs
Overview
This notebook demonstrates using Cloud TPUs in colab to build a simple regression model using y = sin(x) to predict y for given x.
This model generates huge amounts of data that and demonstrates the training performance advantage of Cloud TPU.
The model trains for 10 epochs with 512 steps per epoch on TPU and completes in approximately 2 minutes.
This notebook is hosted on GitHub. To view it in its original repository, after opening the notebook, select File > View on GitHub.
Learning objectives
In this Colab, you will learn how to:
*   Create and compile a Keras model on TPU with a distribution strategy.
*   Train, evaluate, and and generate predictions on Cloud TPU.
*   Compare the performance of a TPU versus a CPU.
Instructions
<h3>  &nbsp;&nbsp;Train on TPU&nbsp;&nbsp; <a href="https://cloud.google.com/tpu/"><img valign="middle" src="https://raw.githubusercontent.com/GoogleCloudPlatform/tensorflow-without-a-phd/master/tensorflow-rl-pong/images/tpu-hexagon.png" width="50"></a></h3>
On the main menu, click Runtime and select Change runtime type. Set "TPU" as the hardware accelerator.
Click Runtime again and select Runtime > Run All. You can also run the cells manually with Shift-ENTER.
Imports
End of explanation
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:
  TPU_ADDRESS = 'grpc://{}'.format(os.environ['COLAB_TPU_ADDR'])
else:
  TPU_ADDRESS = ''
resolver = tf.distribute.cluster_resolver.TPUClusterResolver(tpu=TPU_ADDRESS)
tf.config.experimental_connect_to_cluster(resolver)
tf.tpu.experimental.initialize_tpu_system(resolver)
print("All devices: ", tf.config.list_logical_devices('TPU'))
Explanation: Resolve TPU Address
End of explanation
data_size = 2**18
x = np.linspace(0, 6, data_size, dtype=np.float32)
np.random.shuffle(x)
y = -20 * np.sin(x, dtype=np.float32) + 3 + np.random.normal(0, 1, (data_size,)).astype(np.float32)
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')
Explanation: Creating data for y = sin(x).
Sine wave data is created using numpy. And to make it more difficult, random noise is added to the sine wave.
End of explanation
def get_model():
  return tf.keras.models.Sequential([
      tf.keras.layers.Dense(1, input_shape=(1,)),
      tf.keras.layers.Dense(200, activation='sigmoid'),
      tf.keras.layers.Dense(80, activation='sigmoid'),
      tf.keras.layers.Dense(1)
  ])
Explanation: Define model:
Model will have an input layer where it takes in the x coordinate, two densely connected layers with 200 and 80 nodes, and an output layer where it returns the predicted y value.
End of explanation
strategy = tf.distribute.experimental.TPUStrategy(resolver)
with strategy.scope():
  model = get_model()
  model.compile(optimizer=tf.keras.optimizers.SGD(.01),
                loss='mean_squared_error',
                metrics=['mean_squared_error'])
Explanation: Compiling the model with a distribution strategy
To make the model usable by a TPU, we first must create and compile it using a distribution strategy.
End of explanation
model.fit(train_x, train_y, epochs=10, steps_per_epoch=512)
Explanation: Training of the model on TPU
End of explanation
predictions = model.predict(test_x)
plt.plot(test_x, predictions, 'ro')
Explanation: Prediction
For predictions, same model weights are being used.
End of explanation
<END_TASK>