티스토리 뷰

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 = [[121], [132], [134], [155], [175], [125], [166], [177]]
y_data = [[001], [001], [001], [010], [010], [010], [100], [100]]
 
# Test 데이터
x_test = [[211], [312], [334]]
y_test = [[001], [001], [001]]
 
= tf.placeholder("float", [None, 3])
= tf.placeholder("float", [None, 3])
= tf.Variable(tf.random_normal([33]))
= 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 = [[121], [132], [134], [155], [175], [125], [166], [177]]
y_data = [[001], [001], [001], [010], [010], [010], [100], [100]]
 
# Test 데이터
x_test = [[211], [312], [334]]
y_test = [[001], [001], [001]]
 
= tf.placeholder("float", [None, 3])
= tf.placeholder("float", [None, 3])
= tf.Variable(tf.random_normal([33]))
= 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 = [[121], [132], [134], [155], [175], [125], [166], [177]]
y_data = [[001], [001], [001], [010], [010], [010], [100], [100]]
 
# Test 데이터
x_test = [[211], [312], [334]]
y_test = [[001], [001], [001]]
 
= tf.placeholder("float", [None, 3])
= tf.placeholder("float", [None, 3])
= tf.Variable(tf.random_normal([33]))
= 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.659973833.450012908100828.349976831.659973],
              [823.02002828.0700071828100821.655029828.070007],
              [819.929993824.4000241438100818.97998824.159973],
              [816820.9589841008100815.48999819.23999],
              [819.3599858231188100818.469971818.97998],
              [8198231198100816820.450012],
              [811.700012815.251098100809.780029813.669983],
              [809.51001816.6599731398100804.539978809.559998]])
 
# 0 ~ 3 : x_data
# 4 : y_data
x_data = xy[:, 0:-1]
y_data = xy[:, [-1]]
 
# placeholder
= tf.placeholder(tf.float32, shape=[None, 4])
= tf.placeholder(tf.float32, shape=[None, 1])
= tf.Variable(tf.random_normal([41]), name='weight')
= 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.659973833.450012908100828.349976831.659973],
              [823.02002828.0700071828100821.655029828.070007],
              [819.929993824.4000241438100818.97998824.159973],
              [816820.9589841008100815.48999819.23999],
              [819.3599858231188100818.469971818.97998],
              [8198231198100816820.450012],
              [811.700012815.251098100809.780029813.669983],
              [809.51001816.6599731398100804.539978809.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
= tf.placeholder(tf.float32, shape=[None, 4])
= tf.placeholder(tf.float32, shape=[None, 1])
= tf.Variable(tf.random_normal([41]), name='weight')
= 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
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/05   »
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
글 보관함