""" Starter code for simple linear regression example using placeholders1Created by Chip Huyen ([email protected])2CS20: "TensorFlow for Deep Learning Research"3cs20.stanford.edu4Lecture 035"""6import os7os.environ['TF_CPP_MIN_LOG_LEVEL']='2'8import time910import numpy as np11import matplotlib.pyplot as plt12import tensorflow as tf1314import utils1516DATA_FILE = 'data/birth_life_2010.txt'1718# Step 1: read in data from the .txt file19data, n_samples = utils.read_birth_life_data(DATA_FILE)2021# Step 2: create placeholders for X (birth rate) and Y (life expectancy)22# Remember both X and Y are scalars with type float23X, Y = None, None24#############################25########## TO DO ############26#############################2728# Step 3: create weight and bias, initialized to 0.029# Make sure to use tf.get_variable30w, b = None, None31#############################32########## TO DO ############33#############################3435# Step 4: build model to predict Y36# e.g. how would you derive at Y_predicted given X, w, and b37Y_predicted = None38#############################39########## TO DO ############40#############################4142# Step 5: use the square error as the loss function43loss = None44#############################45########## TO DO ############46#############################4748# Step 6: using gradient descent with learning rate of 0.001 to minimize loss49optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(loss)5051start = time.time()5253# Create a filewriter to write the model's graph to TensorBoard54#############################55########## TO DO ############56#############################5758with tf.Session() as sess:59# Step 7: initialize the necessary variables, in this case, w and b60#############################61########## TO DO ############62#############################6364# Step 8: train the model for 100 epochs65for i in range(100):66total_loss = 067for x, y in data:68# Execute train_op and get the value of loss.69# Don't forget to feed in data for placeholders70_, loss = ########## TO DO ############71total_loss += loss7273print('Epoch {0}: {1}'.format(i, total_loss/n_samples))7475# close the writer when you're done using it76#############################77########## TO DO ############78#############################79writer.close()8081# Step 9: output the values of w and b82w_out, b_out = None, None83#############################84########## TO DO ############85#############################8687print('Took: %f seconds' %(time.time() - start))8889# uncomment the following lines to see the plot90# plt.plot(data[:,0], data[:,1], 'bo', label='Real data')91# plt.plot(data[:,0], data[:,0] * w_out + b_out, 'r', label='Predicted data')92# plt.legend()93# plt.show()9495