|
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])
|
|
28 | 19 | hypothesis = tf.sigmoid(tf.matmul(layer1, W2) + b2)
|
29 | 20 |
|
30 | 21 | # cost/loss function
|
31 |
| -cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * |
32 |
| - tf.log(1 - hypothesis)) |
33 |
| - |
34 |
| -train = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost) |
| 22 | +cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * tf.log(1 - hypothesis)) |
| 23 | +train = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost) |
35 | 24 |
|
36 | 25 | # Accuracy computation
|
37 | 26 | # True if hypothesis>0.5 else False
|
|
44 | 33 | sess.run(tf.global_variables_initializer())
|
45 | 34 |
|
46 | 35 | for step in range(10001):
|
47 |
| - sess.run(train, feed_dict={X: x_data, Y: y_data}) |
| 36 | + _, cost_val = sess.run([train, cost], feed_dict={X: x_data, Y: y_data}) |
48 | 37 | if step % 100 == 0:
|
49 |
| - print(step, sess.run(cost, feed_dict={ |
50 |
| - X: x_data, Y: y_data}), sess.run([W1, W2])) |
| 38 | + print(step, cost_val) |
51 | 39 |
|
52 | 40 | # Accuracy report
|
53 |
| - h, c, a = sess.run([hypothesis, predicted, accuracy], |
54 |
| - feed_dict={X: x_data, Y: y_data}) |
55 |
| - print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a) |
| 41 | + h, p, a = sess.run( |
| 42 | + [hypothesis, predicted, accuracy], feed_dict={X: x_data, Y: y_data} |
| 43 | + ) |
| 44 | + |
| 45 | + print(f"\nHypothesis:\n{h} \nPredicted:\n{p} \nAccuracy:\n{a}") |
56 | 46 |
|
57 | 47 |
|
58 | 48 | '''
|
59 |
| -Hypothesis: [[ 0.01338218] |
60 |
| - [ 0.98166394] |
61 |
| - [ 0.98809403] |
62 |
| - [ 0.01135799]] |
63 |
| -Correct: [[ 0.] |
64 |
| - [ 1.] |
65 |
| - [ 1.] |
66 |
| - [ 0.]] |
67 |
| -Accuracy: 1.0 |
| 49 | +Hypothesis: |
| 50 | +[[0.01338216] |
| 51 | + [0.98166394] |
| 52 | + [0.98809403] |
| 53 | + [0.01135799]] |
| 54 | +Predicted: |
| 55 | +[[0.] |
| 56 | + [1.] |
| 57 | + [1.] |
| 58 | + [0.]] |
| 59 | +Accuracy: |
| 60 | +1.0 |
68 | 61 | '''
|
0 commit comments