티스토리 뷰
Does not work
xor을 단일 퍼셉트론으로 구현하는 것은 불가능하다. 왜냐하면 위 그림과 같이 직선으로 1과 0을 나눌 수 없기 때문이다.
그럼 한번 텐서플로우를 이용하여 단층 퍼셉트론을 구현해 보자07train.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | import tensorflow as tf import numpy as np # 07train.txt # # x1 x2 y # 0 0 0 # 0 1 1 # 1 0 1 # 1 1 0 xy = np.loadtxt('07train.txt', unpack=True) x_data = xy[:-1] y_data = xy[-1] X = tf.placeholder(tf.float32) Y = tf.placeholder(tf.float32) W = tf.Variable(tf.random_uniform([1, len(x_data)], -1.0, 1.0)) h = tf.matmul(W, X) hypothesis = tf.div(1., 1. + tf.exp(-h)) cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * tf.log(1 - hypothesis)) rate = tf.Variable(0.1) optimizer = tf.train.GradientDescentOptimizer(rate) train = optimizer.minimize(cost) init = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init) for step in range(1000): sess.run(train, feed_dict={X: x_data, Y: y_data}) if step % 200 == 0: print(step, sess.run(cost, feed_dict={X: x_data, Y: y_data}), sess.run(W)) print('-'*50) # Test model correct_prediction = tf.equal(tf.floor(hypothesis+0.5), Y) #Calculate accuraty accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) param = [hypothesis, tf.floor(hypothesis+0.5), correct_prediction, accuracy] result = sess.run(param, feed_dict={X:x_data, Y:y_data}) for i in result: print(i) print('Accuracy :', accuracy.eval({X:x_data, Y:y_data}) | cs |
출력 결과를 보면 Accuray가 1이 나오지 않는 것을 볼 수 있다.
NN for XOR
코드로 확인해 보겠다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import tensorflow as tf import numpy as np xy = np.loadtxt('07train.txt', unpack=True) x_data = np.transpose(xy[:-1]) y_data = np.reshape(xy[-1], (4, 1)) X = tf.placeholder(tf.float32) Y = tf.placeholder(tf.float32) W1 = tf.Variable(tf.random_uniform([2, 2], -1.0, 1.0)) W2 = tf.Variable(tf.random_uniform([2, 1], -1.0, 1.0)) b1 = tf.Variable(tf.zeros([2])) b2 = tf.Variable(tf.zeros([1])) L2 = tf.sigmoid(tf.matmul(X, W1) + b1) hypothesis = tf.sigmoid(tf.matmul(L2, W2) + b2) cost = -tf.reduce_mean(Y * tf.log(hypothesis) + (1 - Y) * tf.log(1 - hypothesis)) rate = tf.Variable(0.1) optimizer = tf.train.GradientDescentOptimizer(rate) train = optimizer.minimize(cost) init = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init) for step in range(10000): sess.run(train, feed_dict={X: x_data, Y: y_data}) if step % 1000 == 999: # b1과 b2는 출력 생략. 한 줄에 출력하기 위해 reshape 사용 r1, (r2, r3) = sess.run(cost, feed_dict={X: x_data, Y: y_data}), sess.run([W1, W2]) print('{:5} {:10.8f} {} {}'.format(step+1, r1, np.reshape(r2, (1,4)), np.reshape(r3, (1,2)))) print('-'*50) # Test model correct_prediction = tf.equal(tf.floor(hypothesis+0.5), Y) #Calculate accuraty accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float')) param = [hypothesis, tf.floor(hypothesis+0.5), correct_prediction, accuracy] result = sess.run(param, feed_dict={X:x_data, Y:y_data}) print(*result[0]) print(*result[1]) print(*result[2]) print( result[-1]) print('Accuracy :', accuracy.eval({X:x_data, Y:y_data})) | cs |
두개로 연결하니 xor이 풀리는 것을 볼 수 있다.
--------------------------------------------
이 글은 모두를 위한 딥러닝을 보고 정리한 글입니다.
'AI > 딥러닝(sung kim)' 카테고리의 다른 글
딥네트웍 학습 시키기(backpropagation) (0) | 2018.08.19 |
---|---|
XOR 문제 딥러닝으로 풀기 (0) | 2018.07.23 |
mnist data set (0) | 2018.07.23 |
training/test data set, learning rate, normalization (0) | 2018.07.23 |
learning rate, standardization, normalization (0) | 2018.07.12 |
댓글