-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtenser 2.py
42 lines (29 loc) · 1.28 KB
/
tenser 2.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
# TODO: Create variables
import tensorflow as tf
import matplotlib.pyplot as plt
tf.reset_default_graph()
input_data = tf.placeholder(dtype=tf.float32, shape=None)
output_data = tf.placeholder(dtype=tf.float32, shape=None)
slope = tf.Variable(0.5, dtype=tf.float32)
intercept = tf.Variable(0.1, dtype=tf.float32)
model_operation = slope * input_data + intercept
error = model_operation - output_data
squared_error = tf.square(error)
loss = tf.reduce_mean(squared_error)
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.005)
train = optimizer.minimize(loss)
# TODO: Run a session
init = tf.global_variables_initializer()
x_values = [0, 1, 2, 3, 4]
y_values = [1, 3, 5, 7, 9]
with tf.Session() as sess:
sess.run(init)
for i in range(2000):
sess.run(train, feed_dict={input_data: x_values, output_data: y_values})
if i % 100 == 0:
print(sess.run([slope, intercept]))
plt.plot(x_values, sess.run(model_operation, feed_dict={input_data: x_values}))
print(sess.run(loss, feed_dict={input_data: x_values, output_data: y_values}))
plt.plot(x_values, y_values, 'ro', 'Training Data')
plt.plot(x_values, sess.run(model_operation, feed_dict={input_data: x_values}))
plt.show()