Skip to content

Commit c644f04

Browse files
qoocrabkkweon
authored andcommitted
Update lab-09-2-xor-nn.py (#237)
* Update lab-09-2-xor-nn.py 1. Removed unnecessary codes. * Update lab-09-2-xor-nn.py Add numpy again. * Update lab-09-2-xor-nn.py Add numpy again. * Update lab-09-2-xor-nn.py Commit suggstion. Co-Authored-By: qoocrab <[email protected]> * Update lab-09-2-xor-nn.py Commit Suggestion. Co-Authored-By: qoocrab <[email protected]> * Update lab-09-2-xor-nn.py Commit Suggestion. Co-Authored-By: qoocrab <[email protected]> * Update lab-09-2-xor-nn.py Add f-string output. * Update lab-09-2-xor-nn.py
1 parent d1461d4 commit c644f04

File tree

1 file changed

+23
-30
lines changed

1 file changed

+23
-30
lines changed

lab-09-2-xor-nn.py

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,9 @@
33
import numpy as np
44

55
tf.set_random_seed(777) # for reproducibility
6-
learning_rate = 0.1
76

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)
189

1910
X = tf.placeholder(tf.float32, [None, 2])
2011
Y = tf.placeholder(tf.float32, [None, 1])
@@ -28,10 +19,8 @@
2819
hypothesis = tf.sigmoid(tf.matmul(layer1, W2) + b2)
2920

3021
# 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)
3524

3625
# Accuracy computation
3726
# True if hypothesis>0.5 else False
@@ -44,25 +33,29 @@
4433
sess.run(tf.global_variables_initializer())
4534

4635
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})
4837
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)
5139

5240
# 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}")
5646

5747

5848
'''
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
6861
'''

0 commit comments

Comments
 (0)