티스토리 뷰

Epochs

 - Epoch는 전체 데이터셋에 대한 forward pass와 backward pass를 나타낸다.

 - 그렇기 때문에 더 많은 데이터를 필요로하지 않고 Epoch의 count를 조절하므로써

 - 모델의 정확도를 높힐 수 있다.

 - 텐서플로우에서의 epoch에 대해서 그리고 적절한 수의 epoch에 대해 알아보자.


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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import numpy as np
 
#from helper import batches  
# Helper function created in Mini-batching section
import math
def batches(batch_size, features, labels):
    """
    Create batches of features and labels
    :param batch_size: The batch size
    :param features: List of features
    :param labels: List of labels
    :return: Batches of (Features, Labels)
    """
    assert len(features) == len(labels)
    outout_batches = []
    
    sample_size = len(features)
    for start_i in range(0, sample_size, batch_size):
        end_i = start_i + batch_size
        batch = [features[start_i:end_i], labels[start_i:end_i]]
        outout_batches.append(batch)
        
    return outout_batches
 
def print_epoch_stats(epoch_i, sess, last_features, last_labels):
    """
    Print cost and validation accuracy of an epoch
    """
    current_cost = sess.run(
        cost,
        feed_dict={features: last_features, labels: last_labels})
    valid_accuracy = sess.run(
        accuracy,
        feed_dict={features: valid_features, labels: valid_labels})
    print('Epoch: {:<4} - Cost: {:<8.3} Valid Accuracy: {:<5.3}'.format(
        epoch_i,
        current_cost,
        valid_accuracy))
 
n_input = 784  # MNIST data input (img shape: 28*28)
n_classes = 10  # MNIST total classes (0-9 digits)
 
# Import MNIST data
mnist = input_data.read_data_sets('/datasets/ud730/mnist', one_hot=True)
 
# The features are already scaled and the data is shuffled
train_features = mnist.train.images
valid_features = mnist.validation.images
test_features = mnist.test.images
 
train_labels = mnist.train.labels.astype(np.float32)
valid_labels = mnist.validation.labels.astype(np.float32)
test_labels = mnist.test.labels.astype(np.float32)
 
# Features and Labels
features = tf.placeholder(tf.float32, [None, n_input])
labels = tf.placeholder(tf.float32, [None, n_classes])
 
# Weights & bias
weights = tf.Variable(tf.random_normal([n_input, n_classes]))
bias = tf.Variable(tf.random_normal([n_classes]))
 
# Logits - xW + b
logits = tf.add(tf.matmul(features, weights), bias)
 
# Define loss and optimizer
learning_rate = tf.placeholder(tf.float32)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=labels))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)
 
# Calculate accuracy
correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(labels, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
 
init = tf.global_variables_initializer()
 
batch_size = 128
epochs = 10
learn_rate = 0.001
 
train_batches = batches(batch_size, train_features, train_labels)
 
with tf.Session() as sess:
    sess.run(init)
 
    # Training cycle
    for epoch_i in range(epochs):
 
        # Loop over all batches
        for batch_features, batch_labels in train_batches:
            train_feed_dict = {
                features: batch_features,
                labels: batch_labels,
                learning_rate: learn_rate}
            sess.run(optimizer, feed_dict=train_feed_dict)
 
        # Print cost and validation accuracy of an epoch
        print_epoch_stats(epoch_i, sess, batch_features, batch_labels)
 
    # Calculate accuracy for test dataset
    test_accuracy = sess.run(
        accuracy,
        feed_dict={features: test_features, labels: test_labels})
 
print('Test Accuracy: {}'.format(test_accuracy))
cs


>>


 - Epochs를 10으로 설정해 둔 점을 확인하자.

 - 각 epoch마다 cost가 점점 더 낮아지고 있고 또, 정확도가 향상되고 있는것을 확인할 수 있다.

 - epochs를 100으로 설정해두고 실행해보자.





 - 위의 결과를 보면 epoch가 마지막쯤에 가까워져서는 모델의 정확도가 더 높아지지 않는것을 확인할 수 있다.

 - 이 때, learning rate를 높이면 어떻게 되는지 확인해보자.



 - learning rate를 0.01 에서 0.1로 상향조정한 결과이다.

 - learning rate를 낮추면 더 많은 epoch를 줌으로써 더 높은 정확도를 얻어낼 수 있다.

 - 따라서 모델에 대한 적절한 learning rate와 epoch count를 결정할 수 있는 방법에 대해 알아보자.




 - 최종적으로 Epoch를 1000까지 올렸을때의 결과값.

 - Learning rate와 Epoch의 상관관계에 대해 알아보자.


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/04   »
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
글 보관함