티스토리 뷰
Training and Test data set으로 분류
- Training data set : 학습에만 사용
- Test data set : 모델이 한번도 본 적 없기에 새로운 데이터
Tensorflow
Training data set과 Test data set분류 예제
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 | import tensorflow as tf # 학습 데이터 x_data = [[1, 2, 1], [1, 3, 2], [1, 3, 4], [1, 5, 5], [1, 7, 5], [1, 2, 5], [1, 6, 6], [1, 7, 7]] y_data = [[0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 1, 0], [0, 1, 0], [0, 1, 0], [1, 0, 0], [1, 0, 0]] # Test 데이터 x_test = [[2, 1, 1], [3, 1, 2], [3, 3, 4]] y_test = [[0, 0, 1], [0, 0, 1], [0, 0, 1]] X = tf.placeholder("float", [None, 3]) Y = tf.placeholder("float", [None, 3]) W = tf.Variable(tf.random_normal([3, 3])) b = tf.Variable(tf.random_normal([3])) # hypothesis hypothesis = tf.nn.softmax(tf.matmul(X, W)+b) cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1)) optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.1).minimize(cost) # Correct prediction Test model prediction = tf.arg_max(hypothesis, 1) is_correct = tf.equal(prediction, tf.arg_max(Y, 1)) accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32)) # Launch graph with tf.Session() as sess: # Initialize TensorFlow variables sess.run(tf.global_variables_initializer()) for step in range(201): # 학습에는 학습 데이터 이용 cost_val, W_val, _ = sess.run([cost, W, optimizer], feed_dict={X: x_data, Y: y_data}) print(step, cost_val, W_val) # 200 0.582307 # [[-0.97919977 - 0.42496243 2.13029099] # [-0.84562796 - 1.03543162 - 1.07710946] # [0.66253269 0.44326913 - 0.41879809]] # predict : 테스트에는 테스트 데이터 이용 print("Prediction:", sess.run(prediction, feed_dict={X: x_test})) # Prediction: [2 2 2] # Calculate the accuracy print("Accuracy: ", sess.run(accuracy, feed_dict={X: x_test, Y: y_test})) # Accuracy: 1.0 | cs |
learning rate를 1.5로 준 예제
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 | import tensorflow as tf # 학습 데이터 x_data = [[1, 2, 1], [1, 3, 2], [1, 3, 4], [1, 5, 5], [1, 7, 5], [1, 2, 5], [1, 6, 6], [1, 7, 7]] y_data = [[0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 1, 0], [0, 1, 0], [0, 1, 0], [1, 0, 0], [1, 0, 0]] # Test 데이터 x_test = [[2, 1, 1], [3, 1, 2], [3, 3, 4]] y_test = [[0, 0, 1], [0, 0, 1], [0, 0, 1]] X = tf.placeholder("float", [None, 3]) Y = tf.placeholder("float", [None, 3]) W = tf.Variable(tf.random_normal([3, 3])) b = tf.Variable(tf.random_normal([3])) # hypothesis hypothesis = tf.nn.softmax(tf.matmul(X, W)+b) cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1)) optimizer = tf.train.GradientDescentOptimizer(learning_rate=1.5).minimize(cost) # Correct prediction Test model prediction = tf.arg_max(hypothesis, 1) is_correct = tf.equal(prediction, tf.arg_max(Y, 1)) accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32)) # Launch graph with tf.Session() as sess: # Initialize TensorFlow variables sess.run(tf.global_variables_initializer()) for step in range(201): # 학습에는 학습 데이터 이용 cost_val, W_val, _ = sess.run([cost, W, optimizer], feed_dict={X: x_data, Y: y_data}) print(step, cost_val, W_val) # 200 0.582307 # [[-0.97919977 - 0.42496243 2.13029099] # [-0.84562796 - 1.03543162 - 1.07710946] # [0.66253269 0.44326913 - 0.41879809]] # predict : 테스트에는 테스트 데이터 이용 print("Prediction:", sess.run(prediction, feed_dict={X: x_test})) # Prediction: [2 2 2] # Calculate the accuracy print("Accuracy: ", sess.run(accuracy, feed_dict={X: x_test, Y: y_test})) # Accuracy: 1.0 | cs |
learning rate를 1e-10으로 작게 준 예제
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 | import tensorflow as tf # 학습 데이터 x_data = [[1, 2, 1], [1, 3, 2], [1, 3, 4], [1, 5, 5], [1, 7, 5], [1, 2, 5], [1, 6, 6], [1, 7, 7]] y_data = [[0, 0, 1], [0, 0, 1], [0, 0, 1], [0, 1, 0], [0, 1, 0], [0, 1, 0], [1, 0, 0], [1, 0, 0]] # Test 데이터 x_test = [[2, 1, 1], [3, 1, 2], [3, 3, 4]] y_test = [[0, 0, 1], [0, 0, 1], [0, 0, 1]] X = tf.placeholder("float", [None, 3]) Y = tf.placeholder("float", [None, 3]) W = tf.Variable(tf.random_normal([3, 3])) b = tf.Variable(tf.random_normal([3])) # hypothesis hypothesis = tf.nn.softmax(tf.matmul(X, W)+b) cost = tf.reduce_mean(-tf.reduce_sum(Y * tf.log(hypothesis), axis=1)) optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost) # Correct prediction Test model prediction = tf.arg_max(hypothesis, 1) is_correct = tf.equal(prediction, tf.arg_max(Y, 1)) accuracy = tf.reduce_mean(tf.cast(is_correct, tf.float32)) # Launch graph with tf.Session() as sess: # Initialize TensorFlow variables sess.run(tf.global_variables_initializer()) for step in range(201): # 학습에는 학습 데이터 이용 cost_val, W_val, _ = sess.run([cost, W, optimizer], feed_dict={X: x_data, Y: y_data}) print(step, cost_val, W_val) # 200 0.582307 # [[-0.97919977 - 0.42496243 2.13029099] # [-0.84562796 - 1.03543162 - 1.07710946] # [0.66253269 0.44326913 - 0.41879809]] # predict : 테스트에는 테스트 데이터 이용 print("Prediction:", sess.run(prediction, feed_dict={X: x_test})) # Prediction: [2 2 2] # Calculate the accuracy print("Accuracy: ", sess.run(accuracy, feed_dict={X: x_test, Y: y_test})) # Accuracy: 1.0 | cs |
Non-normalized inputs data의 학습 에제
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 | import tensorflow as tf import numpy as np xy = np.array([[828.659973, 833.450012, 908100, 828.349976, 831.659973], [823.02002, 828.070007, 1828100, 821.655029, 828.070007], [819.929993, 824.400024, 1438100, 818.97998, 824.159973], [816, 820.958984, 1008100, 815.48999, 819.23999], [819.359985, 823, 1188100, 818.469971, 818.97998], [819, 823, 1198100, 816, 820.450012], [811.700012, 815.25, 1098100, 809.780029, 813.669983], [809.51001, 816.659973, 1398100, 804.539978, 809.559998]]) # 0 ~ 3 : x_data # 4 : y_data x_data = xy[:, 0:-1] y_data = xy[:, [-1]] # placeholder X = tf.placeholder(tf.float32, shape=[None, 4]) Y = tf.placeholder(tf.float32, shape=[None, 1]) W = tf.Variable(tf.random_normal([4, 1]), name='weight') b = tf.Variable(tf.random_normal([1]), name='bias') hypothesis = tf.matmul(X, W) + b cost = tf.reduce_mean(tf.square(hypothesis - Y)) # Minimize optimizer = tf.train.GradientDescentOptimizer(learning_rate=1e-5) train = optimizer.minimize(cost) sess = tf.Session() sess.run(tf.global_variables_initializer()) for step in range(2001): cost_val, hy_val, _ = sess.run([cost, hypothesis, train], feed_dict={X: x_data, Y: y_data}) print(step, "Cost: ", cost_val, "\nPrediction:\n", hy_val) # learning_rate를 이것저것 바꿔봐도 input data의 값들간의 차이가 너무 커서 학습이 이루어지지 않음 # 2000 # Cost: nan # Prediction: # [[nan] # [nan] # [nan] # [nan] # [nan] # [nan] # [nan] # [nan]] | cs |
-min-max scale을 이용한 normalize 학습 예제
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.array([[828.659973, 833.450012, 908100, 828.349976, 831.659973], [823.02002, 828.070007, 1828100, 821.655029, 828.070007], [819.929993, 824.400024, 1438100, 818.97998, 824.159973], [816, 820.958984, 1008100, 815.48999, 819.23999], [819.359985, 823, 1188100, 818.469971, 818.97998], [819, 823, 1198100, 816, 820.450012], [811.700012, 815.25, 1098100, 809.780029, 813.669983], [809.51001, 816.659973, 1398100, 804.539978, 809.559998]]) # Min-Max Scale을 이용한 normalize def MinMaxScaler(data): numerator = data - np.min(data, 0) denominator = np.max(data, 0) - np.min(data, 0) # noise term prevents the zero division return numerator / (denominator + 1e-7) xy = MinMaxScaler(xy) # 0 ~ 3 : x_data # 4 : y_data x_data = xy[:, 0:-1] y_data = xy[:, [-1]] # placeholder X = tf.placeholder(tf.float32, shape=[None, 4]) Y = tf.placeholder(tf.float32, shape=[None, 1]) W = tf.Variable(tf.random_normal([4, 1]), name='weight') b = tf.Variable(tf.random_normal([1]), name='bias') hypothesis = tf.matmul(X, W) + b cost = tf.reduce_mean(tf.square(hypothesis - Y)) # Minimize optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01) train = optimizer.minimize(cost) sess = tf.Session() sess.run(tf.global_variables_initializer()) for step in range(2001): cost_val, hy_val, _ = sess.run([cost, hypothesis, train], feed_dict={X: x_data, Y: y_data}) print(step, "Cost: ", cost_val, "\nPrediction:\n", hy_val) # MinMaxScaler를 이용하여 normalize & 학습 # 2000 # Cost: 0.0116639 # Prediction: # [[1.09614301] # [0.77814764] # [0.56500143] # [0.38963056] # [0.47361973] # [0.4958491] # [0.0413903] # [0.21422961]] | cs |
--------------------------------
이 글은 모두를 위한 딥러닝을 듣고 정리한 글입니다.
'AI > 딥러닝(sung kim)' 카테고리의 다른 글
XOR 문제 딥러닝으로 풀기 (0) | 2018.07.23 |
---|---|
mnist data set (0) | 2018.07.23 |
learning rate, standardization, normalization (0) | 2018.07.12 |
과적합(Overfitting) (0) | 2018.07.12 |
TensorFlow로 Fancy Softmax Classification의 구현하기 (0) | 2018.07.11 |
댓글