-
Notifications
You must be signed in to change notification settings - Fork 0
/
linear_regression.py
58 lines (46 loc) · 1.63 KB
/
linear_regression.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import xlrd
import utils
DATA_FILE = "data/fire_theft.xls"
#read data
book = xlrd.open_workbook(DATA_FILE, encoding_override="utf-8")
sheet = book.sheet_by_index(0)
data = np.asarray([sheet.row_values(i) for i in range(1, sheet.nrows)])
n_samples = sheet.nrows-1
#create placeholders (X) (Y)
X = tf.placeholder(tf.float32, name="X")
Y = tf.placeholder(tf.float32, name="Y")
#create weight, bias, initialized to 0
w = tf.Variable(0.0, name="weight", dtype=tf.float32)
b = tf.Variable(0.0, name="bias", dtype=tf.float32)
#predict Y from X
Y_predicted = X*w + b
#use square root error as a loss function
loss = tf.square(Y - Y_predicted, name="loss")
#loss = utils.hubber_loss(Y, Y_predicted)
#use gradient descent + learn_rate 0.01 to minimize loss
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(loss)
#train model
with tf.Session() as sess:
#initialize all variables
sess.run(tf.global_variables_initializer())
writer = tf.summary.FileWriter("./graphs/linearReg", sess.graph)
#train model
for i in range(50):
total_loss = 0
for x,y in data:
#session run optimizer to minimize loss + fetch data
_, l = sess.run([optimizer, loss], feed_dict={X: x, Y: y})
total_loss += l
print("Epoca {0}: {1}".format(i, total_loss/n_samples))
writer.close()
w, b = sess.run([w, b])
X, Y = data.T[0], data.T[1]
plt.plot(X, Y, "bo", label="Real data")
plt.plot(X, X*w+b, "r", label="Predicted data")
plt.legend()
plt.show()