Skip to content

Commit d1461d4

Browse files
qoocrabkkweon
authored andcommitted
Update lab-09-1-xor.py (#236)
* Update lab-09-1-xor.py Removed unnecessary codes. * Update lab-09-1-xor.py Add numpy again. * Update lab-09-1-xor.py Commit suggestion.
1 parent db9125b commit d1461d4

File tree

1 file changed

+13
-22
lines changed

1 file changed

+13
-22
lines changed

lab-09-1-xor.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,33 +3,22 @@
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])
2112

22-
W = tf.Variable(tf.random_normal([2, 1]), name='weight')
23-
b = tf.Variable(tf.random_normal([1]), name='bias')
13+
W = tf.Variable(tf.random_normal([2, 1]), name="weight")
14+
b = tf.Variable(tf.random_normal([1]), name="bias")
2415

2516
# Hypothesis using sigmoid: tf.div(1., 1. + tf.exp(tf.matmul(X, W)))
2617
hypothesis = tf.sigmoid(tf.matmul(X, W) + b)
2718

2819
# cost/loss function
29-
cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) *
30-
tf.log(1 - hypothesis))
31-
32-
train = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)
20+
cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * tf.log(1 - hypothesis))
21+
train = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)
3322

3423
# Accuracy computation
3524
# True if hypothesis>0.5 else False
@@ -42,14 +31,16 @@
4231
sess.run(tf.global_variables_initializer())
4332

4433
for step in range(10001):
45-
sess.run(train, feed_dict={X: x_data, Y: y_data})
34+
_, cost_val, w_val = sess.run(
35+
[train, cost, W], feed_dict={X: x_data, Y: y_data}
36+
)
4637
if step % 100 == 0:
47-
print(step, sess.run(cost, feed_dict={
48-
X: x_data, Y: y_data}), sess.run(W))
38+
print(step, cost_val, w_val)
4939

5040
# Accuracy report
51-
h, c, a = sess.run([hypothesis, predicted, accuracy],
52-
feed_dict={X: x_data, Y: y_data})
41+
h, c, a = sess.run(
42+
[hypothesis, predicted, accuracy], feed_dict={X: x_data, Y: y_data}
43+
)
5344
print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a)
5445

5546
'''

0 commit comments

Comments
 (0)