|
3 | 3 | import numpy as np
|
4 | 4 |
|
5 | 5 | tf.set_random_seed(777) # for reproducibility
|
6 |
| -learning_rate = 0.1 |
7 | 6 |
|
8 |
| -x_data = [[0, 0], |
9 |
| - [0, 1], |
10 |
| - [1, 0], |
11 |
| - [1, 1]] |
12 |
| -y_data = [[0], |
13 |
| - [1], |
14 |
| - [1], |
15 |
| - [0]] |
16 |
| -x_data = np.array(x_data, dtype=np.float32) |
17 |
| -y_data = np.array(y_data, dtype=np.float32) |
| 7 | +x_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=np.float32) |
| 8 | +y_data = np.array([[0], [1], [1], [0]], dtype=np.float32) |
18 | 9 |
|
19 | 10 | X = tf.placeholder(tf.float32, [None, 2])
|
20 | 11 | Y = tf.placeholder(tf.float32, [None, 1])
|
|
36 | 27 | hypothesis = tf.sigmoid(tf.matmul(layer3, W4) + b4)
|
37 | 28 |
|
38 | 29 | # cost/loss function
|
39 |
| -cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * |
40 |
| - tf.log(1 - hypothesis)) |
41 |
| - |
42 |
| -train = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost) |
| 30 | +cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * tf.log(1 - hypothesis)) |
| 31 | +train = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost) |
43 | 32 |
|
44 | 33 | # Accuracy computation
|
45 | 34 | # True if hypothesis>0.5 else False
|
|
52 | 41 | sess.run(tf.global_variables_initializer())
|
53 | 42 |
|
54 | 43 | for step in range(10001):
|
55 |
| - sess.run(train, feed_dict={X: x_data, Y: y_data}) |
| 44 | + _, cost_val = sess.run([train, cost], feed_dict={X: x_data, Y: y_data}) |
56 | 45 | if step % 100 == 0:
|
57 |
| - print(step, sess.run(cost, feed_dict={ |
58 |
| - X: x_data, Y: y_data}), sess.run([W1, W2])) |
| 46 | + print(step, cost_val) |
59 | 47 |
|
60 | 48 | # Accuracy report
|
61 |
| - h, c, a = sess.run([hypothesis, predicted, accuracy], |
62 |
| - feed_dict={X: x_data, Y: y_data}) |
| 49 | + h, c, a = sess.run( |
| 50 | + [hypothesis, predicted, accuracy], feed_dict={X: x_data, Y: y_data} |
| 51 | + ) |
63 | 52 | print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a)
|
64 | 53 |
|
65 | 54 |
|
|
0 commit comments