|
3 | 3 | tf.set_random_seed(777) # for reproducibility
|
4 | 4 |
|
5 | 5 | # Try to find values for W and b to compute Y = W * X + b
|
6 |
| -W = tf.Variable(tf.random_normal([1]), name='weight') |
7 |
| -b = tf.Variable(tf.random_normal([1]), name='bias') |
| 6 | +W = tf.Variable(tf.random_normal([1]), name="weight") |
| 7 | +b = tf.Variable(tf.random_normal([1]), name="bias") |
8 | 8 |
|
9 | 9 | # placeholders for a tensor that will be always fed using feed_dict
|
10 | 10 | # See http://stackoverflow.com/questions/36693740/
|
|
17 | 17 | # cost/loss function
|
18 | 18 | cost = tf.reduce_mean(tf.square(hypothesis - Y))
|
19 | 19 |
|
20 |
| -# Minimize |
21 |
| -optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01) |
22 |
| -train = optimizer.minimize(cost) |
| 20 | +# optimizer |
| 21 | +train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost) |
23 | 22 |
|
24 | 23 | # Launch the graph in a session.
|
25 |
| -sess = tf.Session() |
26 |
| -# Initializes global variables in the graph. |
27 |
| -sess.run(tf.global_variables_initializer()) |
| 24 | +with tf.Session() as sess: |
| 25 | + # Initializes global variables in the graph. |
| 26 | + sess.run(tf.global_variables_initializer()) |
28 | 27 |
|
29 |
| -# Fit the line |
30 |
| -for step in range(2001): |
31 |
| - cost_val, W_val, b_val, _ = \ |
32 |
| - sess.run([cost, W, b, train], |
33 |
| - feed_dict={X: [1, 2, 3], Y: [1, 2, 3]}) |
34 |
| - if step % 20 == 0: |
35 |
| - print(step, cost_val, W_val, b_val) |
| 28 | + # Fit the line |
| 29 | + for step in range(2001): |
| 30 | + _, cost_val, W_val, b_val = sess.run( |
| 31 | + [train, cost, W, b], feed_dict={X: [1, 2, 3], Y: [1, 2, 3]} |
| 32 | + ) |
| 33 | + if step % 20 == 0: |
| 34 | + print(step, cost_val, W_val, b_val) |
36 | 35 |
|
37 |
| -# Learns best fit W:[ 1.], b:[ 0] |
38 |
| -''' |
39 |
| -... |
40 |
| -1980 1.32962e-05 [ 1.00423515] [-0.00962736] |
41 |
| -2000 1.20761e-05 [ 1.00403607] [-0.00917497] |
42 |
| -''' |
| 36 | + # Testing our model |
| 37 | + print(sess.run(hypothesis, feed_dict={X: [5]})) |
| 38 | + print(sess.run(hypothesis, feed_dict={X: [2.5]})) |
| 39 | + print(sess.run(hypothesis, feed_dict={X: [1.5, 3.5]})) |
43 | 40 |
|
44 |
| -# Testing our model |
45 |
| -print(sess.run(hypothesis, feed_dict={X: [5]})) |
46 |
| -print(sess.run(hypothesis, feed_dict={X: [2.5]})) |
47 |
| -print(sess.run(hypothesis, feed_dict={X: [1.5, 3.5]})) |
| 41 | + # Learns best fit W:[ 1.], b:[ 0] |
| 42 | + """ |
| 43 | + 0 3.5240757 [2.2086694] [-0.8204183] |
| 44 | + 20 0.19749963 [1.5425726] [-1.0498911] |
| 45 | + ... |
| 46 | + 1980 1.3360998e-05 [1.0042454] [-0.00965055] |
| 47 | + 2000 1.21343355e-05 [1.0040458] [-0.00919707] |
| 48 | + [5.0110054] |
| 49 | + [2.500915] |
| 50 | + [1.4968792 3.5049512] |
| 51 | + """ |
48 | 52 |
|
49 |
| -''' |
50 |
| -[ 5.0110054] |
51 |
| -[ 2.50091505] |
52 |
| -[ 1.49687922 3.50495124] |
53 |
| -''' |
| 53 | + # Fit the line with new training data |
| 54 | + for step in range(2001): |
| 55 | + _, cost_val, W_val, b_val = sess.run( |
| 56 | + [train, cost, W, b], |
| 57 | + feed_dict={X: [1, 2, 3, 4, 5], Y: [2.1, 3.1, 4.1, 5.1, 6.1]}, |
| 58 | + ) |
| 59 | + if step % 20 == 0: |
| 60 | + print(step, cost_val, W_val, b_val) |
54 | 61 |
|
| 62 | + # Testing our model |
| 63 | + print(sess.run(hypothesis, feed_dict={X: [5]})) |
| 64 | + print(sess.run(hypothesis, feed_dict={X: [2.5]})) |
| 65 | + print(sess.run(hypothesis, feed_dict={X: [1.5, 3.5]})) |
55 | 66 |
|
56 |
| -# Fit the line with new training data |
57 |
| -for step in range(2001): |
58 |
| - cost_val, W_val, b_val, _ = \ |
59 |
| - sess.run([cost, W, b, train], |
60 |
| - feed_dict={X: [1, 2, 3, 4, 5], |
61 |
| - Y: [2.1, 3.1, 4.1, 5.1, 6.1]}) |
62 |
| - if step % 20 == 0: |
63 |
| - print(step, cost_val, W_val, b_val) |
64 |
| - |
65 |
| -# Learns best fit W:[ 1.], b:[ 1.1] |
66 |
| -''' |
67 |
| -1980 2.90429e-07 [ 1.00034881] [ 1.09874094] |
68 |
| -2000 2.5373e-07 [ 1.00032604] [ 1.09882331] |
69 |
| -''' |
70 |
| - |
71 |
| -# Testing our model |
72 |
| -print(sess.run(hypothesis, feed_dict={X: [5]})) |
73 |
| -print(sess.run(hypothesis, feed_dict={X: [2.5]})) |
74 |
| -print(sess.run(hypothesis, feed_dict={X: [1.5, 3.5]})) |
75 |
| - |
76 |
| -''' |
77 |
| -[ 6.10045338] |
78 |
| -[ 3.59963846] |
79 |
| -[ 2.59931231 4.59996414] |
80 |
| -''' |
| 67 | + # Learns best fit W:[ 1.], b:[ 1.1] |
| 68 | + """ |
| 69 | + 0 1.2035878 [1.0040361] [-0.00917497] |
| 70 | + 20 0.16904518 [1.2656431] [0.13599995] |
| 71 | + ... |
| 72 | + 1980 2.9042917e-07 [1.00035] [1.0987366] |
| 73 | + 2000 2.5372992e-07 [1.0003271] [1.0988194] |
| 74 | + [6.1004534] |
| 75 | + [3.5996385] |
| 76 | + [2.5993123 4.599964 ] |
| 77 | + """ |
0 commit comments