-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtutorial_03.py
49 lines (42 loc) · 1.38 KB
/
tutorial_03.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
import tensorflow as tf
import matplotlib.pyplot as plt
import numpy as np
# 线性回归模拟
# fetch and feed
node1 = tf.constant(3.0)
node2 = tf.constant(4.0)
node3 = tf.constant(8.0)
add = tf.add(node1, node2)
m1 = tf.multiply(add, node3)
a = tf.placeholder(dtype=tf.float32)
b = tf.placeholder(dtype=tf.float32)
m2 = tf.multiply(a, b)
x_input = np.random.rand(100)
y_input = x_input*0.1+0.2
W = tf.Variable(0.1, dtype=tf.float32)
k = tf.Variable(-0.1, dtype=tf.float32)
y = W*x_input+k
# reduce_sum is bad!!! because iteration is bigger
# loss = tf.reduce_sum(tf.square(y_input - y))
loss = tf.reduce_mean(tf.square(y_input - y))
optimizer = tf.train.GradientDescentOptimizer(0.1)
train = optimizer.minimize(loss)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
result = sess.run([add, m1])
print(result)
print(sess.run(m2, feed_dict={a:[1, 2, 3, 4], b:[2, 4, 6, 8]}))
for i in range(1000):
sess.run(train)
if i % 100 == 0:
print(" step : ", i, sess.run([W, k, loss]))
curr_W, curr_k, curr_loss = sess.run([W, k, loss])
print("W : %s, k : %s, loss : %s", curr_W, curr_k, curr_loss)
plt.plot(x_input, y_input, label="line")
curr_y = []
for i in range(len(x_input)):
curr_y.append(x_input[i]*curr_W+curr_k)
plt.plot(x_input, curr_y, label="fit-line")
plt.legend()
plt.show()