You are on page 1of 79

Linear Regression

Gradient Descent Solution

(Iterative Solution)
Linear Regression: Gradient
Descent Solution
Linear Regression: Gradient
Descent Solution
TensorFlow
from Google
Deep Learning
Most Common Frameworks

50,000+ binary installs in 72 hours, 500,000+ since November, 2015


Deep Learning
Most Common Frameworks
Deep Learning
Most Common Frameworks
Mobile App using TensorFlow
(Signs Translation)
Mobile App using TensorFlow
(Language Translation)
Google Cloud
Data Center as a Computer
Google Cloud
Data Center as a Computer
Google Cloud
Data Center as a Computer
TensorFlow architecture
TensorFlow basic concepts
A TensorFlow computation is described by a directed
graph , which is composed of a set of nodes
● Library user construct a computational graph using one
of the supported frontend languages (C++ or Python)
● In a TensorFlow graph, each node has zero or more
inputs and zero or more outputs, and represents the
instantiation of an operation
● Values that flow along normal edges in the graph
(from outputs to inputs) are tensors - arbitrary
dimensionality arrays where the underlying element type is
specified or inferred at graph-construction time
● Special edges, called control dependencies , can also
exist in the graph

Tensorflow Operation types
Hello World
Tensorflow

import tensorflow as tf

input1 = tf.placeholder(tf.float32)
input2 = tf.placeholder(tf.float32)
input3 = tf.constant(5.0)

add_res = tf.add(input2, input3)


mul = tf.mul(input1, add_res)

with tf.Session() as sess:


result = sess.run(mul,feed_dict={input1:[3.0],input2:[2.0]})
print(result)
Linear Regression
Using
TensorFlow
1-D Data Example
Data Preparation
import numpy as np

num_of_points = 100 #Generate 100 Data Points


points = []
for i in range(num_of_points):
x1= np.random.normal(0.0, 0.55)
y1= x1 * 0.1 + 0.3 + np.random.normal(0.0, 0.01)
points.append([x1, y1])
x_data = [v[0] for v in points]
y_data = [v[1] for v in points]
Draw Data

import matplotlib.pyplot as plt

plt.plot(x_data, y_data, 'ro', label='Original data')


plt.legend()
plt.show()
Original Data
Variables and Nodes
Preparation
import tensorflow as tf

#initialize weights "W and bias "b"


W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))

y = W * x_data + b

#Define Loss function as Mean of Squared Error


loss = tf.reduce_mean(tf.square(y - y_data))

#Create Optimizer class to minimize Losses


optimizer = tf.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)

#initialize TensorFlow Variables (always)


init = tf.global_variables_initializer()
Execute TensorFlow Graph
#Start TensorFlow Session and carryout Variable initialization
sess = tf.Session()
sess.run(init)

#Carryout 16 Iterations
for step in range(16):
sess.run(train)

#Draw Original Data


plt.plot(x_data, y_data, 'ro')

#Draw Predicted data (using calculated weight and bias after training
plt.plot(x_data, sess.run(W) * x_data + sess.run(b))
plt.xlabel('x')
plt.xlim(-2, 2)
plt.ylim(0.1, 0.6)
plt.ylabel('y')
plt.legend()
plt.show()

# print updated weights, bias, and Loss value after current training iteration
print(step, sess.run(W), sess.run(b),sess.run(loss))
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
Iteration 6
Iteration 7
Iteration 8
Iteration 9
Iteration 10
Iteration 11
Iteration 12
Iteration 13
Iteration 14
Iteration 15
Iteration 16
Updated Weights and
Calculated Loss
2-D Data Example
Data Preparation
import numpy as np

#Prepare Random Data set (Linear + Random Noise)


num_of_points = 100 #1000
points = []
for i in range(num_of_points):
x1= np.random.normal(0.0, 0.55)
x2= np.random.normal(0.0, 0.55)
y1= x1 * 0.1 + x2 * 0.2 + 0.3 + np.random.normal(0.0, 0.01)
points.append([x1,x2, y1])
x1_data = [v[0] for v in points]
x2_data = [v[1] for v in points]
y_data = [v[2] for v in points]
Draw Data

#Draw Data Set as 3-D


import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.scatter(x1_data, x2_data, y_data, c='r', marker="o")


plt.legend()
plt.show()
Draw Data
Variables and Nodes
Preparation
import tensorflow as tf

#initialize weights "W and bias "b"


W1 = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
W2 = tf.Variable(tf.random_uniform([1], -1.0, 1.0))
b = tf.Variable(tf.zeros([1]))

y = W1 * x1_data + W2 * x2_data + b

#Define Loss function as Mean of Squared Error


loss = tf.reduce_mean(tf.square(y - y_data))

#Create Optimizer to minimize Losses


optimizer = tf.train.GradientDescentOptimizer(0.9)
train = optimizer.minimize(loss)

#initialize TensorFlow Variables (always)


init = tf.global_variables_initializer()
Execute TensorFlow Graph
#Start TensorFlow Session and carryout Variable initialization
sess = tf.Session()
sess.run(init)

#Carryout 24 Iterations
#
#
#
# { Continue in Next Slide }
Execute TensorFlow Graph
#Carryout 24 Iterations
for step in range(24):
sess.run(train)

#Draw Original Data


fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x1_data, x2_data, y_data, c='r', marker="o")

#Draw Mesh Grid (XX,YY) --> claculated regression surface


XX = np.arange(-1, 1, 0.1)
YY = np.arange(-1, 1, 0.1)
XY, YY = np.meshgrid(XX, YY)
ZZ=sess.run(W1) * XX + sess.run(W2) * YY + sess.run(b)
ax.plot_surface(XX, YY, ZZ, cmap=cm.coolwarm,linewidth=0, antialiased=False)

#Draw Calculated Points corresponding to Original Data Points


ax.scatter(x1_data, x2_data, sess.run(W1) * x1_data + sess.run(W2) * x2_data
+ sess.run(b), c='b', marker="^")
plt.legend()
plt.show()

# print updated weights, bias, and Loss value after current training iteration
print(step+1, sess.run(W1), sess.run(W2) , sess.run(b),sess.run(loss))
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
Iteration 6
Iteration 7
Iteration 8
Iteration 9
Iteration 10
Iteration 11
Iteration 12
Iteration 13
Iteration 14
Iteration 15
Iteration 16
Iteration 17
Iteration 18
Iteration 19
Iteration 20
Updated Weights and
Calculated Loss

You might also like