Skip to content

Commit be1b26c

Browse files
kakao-jun-ehunkim
authored andcommitted
XOR Tensorboard (hunkim#77)
* Added XOR Tensorboard Updated .gitignore file to ignore logs directory * Renamed lab-09-7 to lab-09-3
1 parent 255fac7 commit be1b26c

2 files changed

Lines changed: 78 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ tb
99
Untitled*.ipynb
1010
.ropeproject
1111
xlab*
12+
logs

lab-09-3-xor_tensorboard.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Lab 9 XOR
2+
# This example does not work
3+
import tensorflow as tf
4+
import numpy as np
5+
tf.set_random_seed(777) # for reproducibility
6+
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)
9+
10+
X = tf.placeholder(tf.float32, [None, 2], name='x-input')
11+
Y = tf.placeholder(tf.float32, [None, 1], name='y-input')
12+
13+
W1 = tf.Variable(tf.random_normal([2, 2]), name='weight1')
14+
b1 = tf.Variable(tf.random_normal([2]), name='bias1')
15+
16+
with tf.name_scope("layer2") as scope:
17+
layer1 = tf.sigmoid(tf.matmul(X, W1) + b1)
18+
19+
W2 = tf.Variable(tf.random_normal([2, 1]), name='weight2')
20+
b2 = tf.Variable(tf.random_normal([1]), name='bias2')
21+
22+
with tf.name_scope("layer3") as scope:
23+
hypothesis = tf.sigmoid(tf.matmul(layer1, W2) + b2)
24+
25+
w1_hist = tf.summary.histogram("weights1", W1)
26+
w2_hist = tf.summary.histogram("weights2", W2)
27+
b1_hist = tf.summary.histogram("biases1", b1)
28+
b2_hist = tf.summary.histogram("biases2", b2)
29+
y_hist = tf.summary.histogram("y", Y)
30+
31+
# cost/loss function
32+
with tf.name_scope("cost") as scope:
33+
cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) *
34+
tf.log(1 - hypothesis))
35+
cost_summ = tf.summary.scalar("cost", cost)
36+
37+
with tf.name_scope("train") as scope:
38+
train = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)
39+
40+
# Accuracy computation
41+
# True if hypothesis>0.5 else False
42+
predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)
43+
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))
44+
45+
# Launch graph
46+
with tf.Session() as sess:
47+
#tensorboard --logdir=./logs/xor_logs
48+
merged = tf.summary.merge_all()
49+
writer = tf.summary.FileWriter("./logs/xor_logs", sess.graph)
50+
51+
# Initialize TensorFlow variables
52+
sess.run(tf.global_variables_initializer())
53+
54+
for step in range(10001):
55+
summary, _ = sess.run([merged, train], feed_dict={X: x_data, Y: y_data})
56+
if step % 100 == 0:
57+
print(step, sess.run(cost, feed_dict={
58+
X: x_data, Y: y_data}), sess.run([W1, W2]))
59+
writer.add_summary(summary, step)
60+
61+
# Accuracy report
62+
h, c, a = sess.run([hypothesis, predicted, accuracy],
63+
feed_dict={X: x_data, Y: y_data})
64+
print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a)
65+
66+
67+
'''
68+
Hypothesis: [[ 0.01338218]
69+
[ 0.98166394]
70+
[ 0.98809403]
71+
[ 0.01135799]]
72+
Correct: [[ 0.]
73+
[ 1.]
74+
[ 1.]
75+
[ 0.]]
76+
Accuracy: 1.0
77+
'''

0 commit comments

Comments
 (0)