""" Starter code for a simple regression example using eager execution.1Created by Akshay Agrawal ([email protected])2CS20: "TensorFlow for Deep Learning Research"3cs20.stanford.edu4Lecture 045"""6import time78import tensorflow as tf9import tensorflow.contrib.eager as tfe10import matplotlib.pyplot as plt1112import utils1314DATA_FILE = 'data/birth_life_2010.txt'1516# In order to use eager execution, `tfe.enable_eager_execution()` must be17# called at the very beginning of a TensorFlow program.18#############################19########## TO DO ############20#############################2122# Read the data into a dataset.23data, n_samples = utils.read_birth_life_data(DATA_FILE)24dataset = tf.data.Dataset.from_tensor_slices((data[:,0], data[:,1]))2526# Create weight and bias variables, initialized to 0.0.27#############################28########## TO DO ############29#############################30w = None31b = None3233# Define the linear predictor.34def prediction(x):35#############################36########## TO DO ############37#############################38pass3940# Define loss functions of the form: L(y, y_predicted)41def squared_loss(y, y_predicted):42#############################43########## TO DO ############44#############################45pass4647def huber_loss(y, y_predicted):48"""Huber loss with `m` set to `1.0`."""49#############################50########## TO DO ############51#############################52pass5354def train(loss_fn):55"""Train a regression model evaluated using `loss_fn`."""56print('Training; loss function: ' + loss_fn.__name__)57optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)5859# Define the function through which to differentiate.60#############################61########## TO DO ############62#############################63def loss_for_example(x, y):64pass6566# Obtain a gradients function using `tfe.implicit_value_and_gradients`.67#############################68########## TO DO ############69#############################70grad_fn = None7172start = time.time()73for epoch in range(100):74total_loss = 0.075for x_i, y_i in tfe.Iterator(dataset):76# Compute the loss and gradient, and take an optimization step.77#############################78########## TO DO ############79#############################80optimizer.apply_gradients(gradients)81total_loss += loss82if epoch % 10 == 0:83print('Epoch {0}: {1}'.format(epoch, total_loss / n_samples))84print('Took: %f seconds' % (time.time() - start))85print('Eager execution exhibits significant overhead per operation. '86'As you increase your batch size, the impact of the overhead will '87'become less noticeable. Eager execution is under active development: '88'expect performance to increase substantially in the near future!')8990train(huber_loss)91plt.plot(data[:,0], data[:,1], 'bo')92# The `.numpy()` method of a tensor retrieves the NumPy array backing it.93# In future versions of eager, you won't need to call `.numpy()` and will94# instead be able to, in most cases, pass Tensors wherever NumPy arrays are95# expected.96plt.plot(data[:,0], data[:,0] * w.numpy() + b.numpy(), 'r',97label="huber regression")98plt.legend()99plt.show()100101102