XOR ๋ฌธ์
๋ ๊ฐ์ ์ธํ์ ๋ํด 1๊ฐ์ ๊ฒฐ๊ณผ๊ฐ์ด ๋์ค๋ ๋ฌธ์ ์ด๋ฏ๋ก ํ ์ํ๋ก์ฐ๋ก ๊ตฌํํด๋ณด์.
๊ฒฐ๊ณผ๊ฐ์ด 0 ๋๋ 1์ด๋ฏ๋ก, logistic regression์ผ๋ก ๋ง๋ค ์ ์๋ค.
# Lab 9 XOR
import tensorflow as tf
import numpy as np
tf.set_random_seed(777) # for reproducibility
x_data = np.array([[0, 0], [0, 1], [1, 0], [1, 1]], dtype=np.float32)
y_data = np.array([[0], [1], [1], [0]], dtype=np.float32)
X = tf.placeholder(tf.float32, [None, 2])
Y = tf.placeholder(tf.float32, [None, 1])
W = tf.Variable(tf.random_normal([2, 1]), name="weight")
b = tf.Variable(tf.random_normal([1]), name="bias")
# Hypothesis using sigmoid: tf.div(1., 1. + tf.exp(tf.matmul(X, W)))
hypothesis = tf.sigmoid(tf.matmul(X, W) + b)
# cost/loss function
cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * tf.log(1 - hypothesis))
train = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost)
# Accuracy computation
# True if hypothesis>0.5 else False
predicted = tf.cast(hypothesis > 0.5, dtype=tf.float32)
accuracy = tf.reduce_mean(tf.cast(tf.equal(predicted, Y), dtype=tf.float32))
# Launch graph
with tf.Session() as sess:
# Initialize TensorFlow variables
sess.run(tf.global_variables_initializer())
for step in range(10001):
_, cost_val, w_val = sess.run(
[train, cost, W], feed_dict={X: x_data, Y: y_data}
)
if step % 100 == 0:
print(step, cost_val, w_val)
# Accuracy report
h, c, a = sess.run(
[hypothesis, predicted, accuracy], feed_dict={X: x_data, Y: y_data}
)
print("\nHypothesis: ", h, "\nCorrect: ", c, "\nAccuracy: ", a)
'''
Hypothesis: [[ 0.5]
[ 0.5]
[ 0.5]
[ 0.5]]
Correct: [[ 0.]
[ 0.]
[ 0.]
[ 0.]]
Accuracy: 0.5
'''
์? ๊ทธ๋ฐ๋ฐ ์ ์ ์๋์ง..?
์ ํ๋๊ฐ 50%๋ฉด ํ๋ฆฐ ๊ฒฐ๊ณผ๋ผ๋ ๊ฒ๋ ๊ฐ์ ๋ง์ด๋ค!
์ด๋ป๊ฒ ํด๊ฒฐํ ์ ์์๊น?
Neural Net
์ด๊ฑธ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ์ด Layer๋ฅผ ์์๋ฒ๋ฆฌ๋ ๊ฒ์ด๋ค.
Layer๋ ์์์ sigmoid ํจ์๋ฅผ ํต๊ณผํ ์ถ๋ ฅ๊ฐ์ ๋ค์ํ๋ฒ
ํ๋ จ์ํค๋ ๊ฒ์ด๋ค.
W1 = tf.Variable(tf.random_normal([2, 2]), name='weight1')
b1 = tf.Variable(tf.random_normal([2]), name='bias1')
layer1 = tf.sigmoid(tf.matmul(X, W1) + b1)
W2 = tf.Variable(tf.random_normal([2, 1]), name='weight2')
b2 = tf.Variable(tf.random_normal([1]), name='bias2')
hypothesis = tf.sigmoid(tf.matmul(layer1, W2) + b2)
์ด ๋, ์ฃผ์ํด์ผ ํ๋ ๊ฒ์, layer1์ ์ถ๋ ฅ ์ฐจ์์ ๋ด๊ฐ ์กฐ์ ํ ์ ์๊ณ ,
๋ํ ์ด ๊ฐ์ด layer2์ ๋ค์ด๊ฐ๊ฒ ๋๋ฏ๋ก, ์ด ์ซ์๋ฅผ ๋ง์ถฐ์ ๋ฃ์ด์ค์ผ ํ๋ค. ์ฆ,
W1 = tf.Variable(tf.random_normal([feature์ ์, output1์ ์ฐจ์]), name='weight1')
b1 = tf.Variable(tf.random_normal([output1์ ์ฐจ์]), name='bias1')
layer1 = tf.sigmoid(tf.matmul(X, W1) + b1)
W2 = tf.Variable(tf.random_normal([output1์ ์ฐจ์, output2์ ์ฐจ์]), name='weight2')
b2 = tf.Variable(tf.random_normal([output2์ ์ฐจ์]), name='bias2')
hypothesis = tf.sigmoid(tf.matmul(layer1, W2) + b2)
์ด๋ฐ์์ผ๋ก ๊ผฌ๋ฆฌ๋ฅผ ๋ฌผ๊ณ ๋ค์ด๊ฐ๊ฒ ๋๋ค.
๋จธ๋ฆฟ์์ผ๋ก ๊ตฌ์ฒดํ๊ฐ ๋์ด์์ด์ผ ํ๋ค!
์ด๋ถ๋ถ๋ง ๋ฐ๊ฟ์ฃผ๊ณ ๋ค์ ๋๋ฆฌ๊ฒ ๋๋ฉด,
'''
Hypothesis:
[[0.01338216]
[0.98166394]
[0.98809403]
[0.01135799]]
Predicted:
[[0.]
[1.]
[1.]
[0.]]
Accuracy:
1.0
'''
์ ์์ธกํ๋ ๊ฒ์ ์ ์ ์๋ค.
Wide NN for XOR
[2, 2] โ [2, 1] ์ผ๋ก ๋ถํฐ, [2, 10] โ [10, 1] ์ผ๋ก ๋ฐ๊ฟ์ ์งํํด๋ณด์.
'''
Hypothesis: [[ 7.80511764e-04]
[ 9.99238133e-01]
[ 9.98379230e-01]
[ 1.55659032e-03]]
Correct: [[ 0.]
[ 1.]
[ 1.]
[ 0.]]
Accuracy: 1.0
'''
๋ ์ ํ์ต๋์๋ค!
Deep NN for XOR
W1 = tf.Variable(tf.random_normal([2, 10]), name='weight1')
b1 = tf.Variable(tf.random_normal([10]), name='bias1')
layer1 = tf.sigmoid(tf.matmul(X, W1) + b1)
W2 = tf.Variable(tf.random_normal([10, 10]), name='weight2')
b2 = tf.Variable(tf.random_normal([10]), name='bias2')
layer2 = tf.sigmoid(tf.matmul(layer1, W2) + b2)
W3 = tf.Variable(tf.random_normal([10, 10]), name='weight3')
b3 = tf.Variable(tf.random_normal([10]), name='bias3')
layer3 = tf.sigmoid(tf.matmul(layer2, W3) + b3)
W4 = tf.Variable(tf.random_normal([10, 1]), name='weight4')
b4 = tf.Variable(tf.random_normal([1]), name='bias4')
hypothesis = tf.sigmoid(tf.matmul(layer3, W4) + b4)
๋ ์ด์ด๊ฐ ๋ง์ ์๋ก ๋ชจ๋ธ์ด ๋ ์ ํ์ต๋๋ค.