In this tutorial, we will introduce the way to implement linear regression in tensorflow, this is the basic of deep learning.
1. Import libraries
import numpy as np import tensorflow as tf import matplotlib.pyplot as plt
2. Set random seed
np.random.seed(10) tf.set_random_seed(10)
3. Create train data
x = np.linspace(0, 50, 50) y = np.linspace(0, 50, 50) # Adding noise to the random linear data x += np.random.uniform(-4, 4, 50) y += np.random.uniform(-4, 4, 50) n = len(x) # Number of data points
Then we will start to implement linear regression to fit data above.
4. Define tensorflow variables
X = tf.placeholder("float") Y = tf.placeholder("float") W = tf.Variable(np.random.randn(), name = "W") b = tf.Variable(np.random.randn(), name = "b")
5. Set learning rate and epochs
learning_rate = 0.01 training_epochs = 1000
6. Build linear regression to train
# Hypothesis y_pred = tf.add(tf.multiply(X, W), b) # Mean Squared Error Cost Function cost = tf.reduce_sum(tf.pow(y_pred-Y, 2)) / (2 * n) # Gradient Descent Optimizer optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) # Global Variables Initializer init = tf.global_variables_initializer()
7. Start to train model
# Starting the Tensorflow Session with tf.Session() as sess: # Initializing the Variables sess.run(init) # Iterating through all the epochs for epoch in range(training_epochs): # Feeding each data point into the optimizer using Feed Dictionary for (_x, _y) in zip(x, y): sess.run(optimizer, feed_dict = {X : _x, Y : _y}) # Displaying the result after every 50 epochs if (epoch + 1) % 50 == 0: # Calculating the cost a every epoch c = sess.run(cost, feed_dict = {X : x, Y : y}) print("Epoch", (epoch + 1), ": cost =", c, "W =", sess.run(W), "b =", sess.run(b)) # Storing necessary values to be used outside the Session training_cost = sess.run(cost, feed_dict ={X: x, Y: y}) weight = sess.run(W) bias = sess.run(b)
Then you will get the best W and b.