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)

๋ ˆ์ด์–ด๊ฐ€ ๋งŽ์„ ์ˆ˜๋ก ๋ชจ๋ธ์ด ๋” ์ž˜ ํ•™์Šต๋œ๋‹ค.