-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathLinear regression.py
106 lines (80 loc) · 2.29 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 15 15:59:19 2016
@author: viky
"""
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
#input data:
x_input=np.linspace(0,10,100)
y_input=5*x_input+2.5
#model parameters
W = tf.Variable(tf.random_normal([1]), name='weight')
#bias
b = tf.Variable(tf.random_normal([1]), name='bias')
#placeholders
with tf.name_scope('input'):
X=tf.placeholder(tf.float32, name='InputX')
Y=tf.placeholder(tf.float32, name='InputY')
#model
with tf.name_scope('model'):
Y_pred=tf.add(tf.mul(X,W),b)
#loss
with tf.name_scope('loss'):
loss = tf.reduce_mean(tf.square(Y_pred -Y ))
#training algorithm
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
#initializing the variables
#init = tf.initialize_all_variables() #for TF version < 1.0
init=tf.global_variables_initializer()
#starting the session session
sess = tf.Session()
sess.run(init)
cost=tf.scalar_summary("loss", loss)
sess.run(init)
epoch=2000
merged_summary_op = tf.merge_all_summaries()
summary_writer = tf.train.SummaryWriter('/Users/Enkay/Documents/Viky/python/tensorflow/lineartensorboard/', graph=tf.get_default_graph())
for step in xrange(epoch):
_, c, summary=sess.run([train, loss, merged_summary_op], feed_dict={X: x_input, Y: y_input})
summary_writer.add_summary(summary,step)
if step%50==0:
print c
print "Model paramters:"
print "Weight:%f" %sess.run(W)
print "bias:%f" %sess.run(b)
'''
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
x_data=np.linspace(0,10,10)
y_data = 2*x_data + 0.3*np.random.rand(*x_data.shape)
#weight
W = tf.Variable(tf.zeros([1]))
#bias
b = tf.Variable(tf.zeros([1]))
#Model-linear regression
y = W * x_data + b
#cost
loss = tf.reduce_mean(tf.square(y - y_data))
#training algorithm
optimizer = tf.train.GradientDescentOptimizer(0.01)
train = optimizer.minimize(loss)
#initializing the variables
init = tf.initialize_all_variables()
#starting the session session
sess = tf.Session()
sess.run(init)
# training the line
for step in xrange(1000):
sess.run(train)
print "Model paramters:"
print "Weight:%f" %sess.run(W)
print "bias:%f" %sess.run(b)
#test
x_test=np.linspace(0,10,10)
y_test=sess.run(W)*x_test+sess.run(b)
plt.plot(x_data,y_data,'o', x_test,y_test,"*")
'''