Learning The TensorFlow Way of Linear Regression
Getting ready
In this recipe, we will loop through batches of data points and let TensorFlow update the slope and y-intercept. Instead of generated data, we will us the iris dataset that is built in to the Scikit Learn. Specifically, we will find an optimal line through data points where the x-value is the petal width and the y-value is the sepal length. We choose these two because there appears to be a linear relationship between them, as we will see in the graphs at the end. We will also talk more about the effects of different loss
functions in the next section, but for this recipe we will use the L2 loss
function.
How to do it…
We start by loading the necessary libraries, creating a graph, and loading the data:
import matplotlib.pyplot as plt import numpy as np import tensorflow as tf from sklearn import datasets from tensorflow.python.framework import ops ops.reset_default_graph() sess = tf.Session() iris = datasets.load_iris()
x_vals = np...