[텐서플로우] MNIST 데이터를 이용한 RNN 모델

2020. 4. 30. 17:02노트/Python : 프로그래밍

 

데이터 불러오기 

from tensorflow.examples.tutorials.mnist import input_data

mnist=input_data.read_data_sets("./mnist/data/",one_hot=True)

 

변수정의 

lr = 0.01 
total_epoch = 30
batch_size = 128 

n_input = 28 # 한번에 입력받는 데이터 갯수 
n_step = 28 # 28줄 
n_hidden = 128  # 셀에서 나가는 출력의 갯수 
n_class = 10 # 숫자의 종류 (0~9)

w = tf.Variable(tf.random_normal([n_hidden , n_class ]))
b = tf.Variable(tf.random_normal([n_class]))

 

모델 구성 

 

# 신경망 모델 구성 
x = tf.placeholder(tf.float32, [None, n_step,n_input ])
y = tf.placeholder(tf.float32, [None, n_class])

# rnn 학습할 셀을 생성 
cell = tf.nn.rnn_cell.BasicLSTMCell(n_hidden)

# rnn 망을 구상 
outputs, states = tf.nn.dynamic_rnn(cell,  x, dtype=tf.float32)

"""
outputs : [batch_size, n_step, n_hidden]
=> 구조 변경(y형태로) => y : [batch_size, n_class]
[batch_size , n_step, n_hidden] >> 
[n_step, batch_size, n_hidden]
"""
outputs = tf.transpose(outputs,[1,0,2])
outputs = outputs[-1] # n_step 빠짐 

model = tf.matmul(outputs, w)+ b 
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits= model, labels = y))
opt = tf.train.AdamOptimizer(lr).minimize(cost)

 

모델 학습 

 

# 신경망 모델 학습 
sess = tf.Session()
sess.run(tf.global_variables_initializer())
total_batch = int(mnist.train.num_examples/batch_size)

for epoch in range(total_epoch):
    total_cost = 0
    for i in range(total_batch): 
        batchX, batchY = mnist.train.next_batch(batch_size)
        batchX = batchX.reshape((batch_size,n_step, n_input))
        # 128, 784 -> 128, 28, 28 
        _, costv= sess.run([opt, cost], feed_dict={x:batchX, y:batchY})
        total_cost+= costv
    print("epoch:","%4d" % (epoch+1), "cost 평균:", "{:.3f}".format(total_cost/total_batch))
    
>>>
epoch:    1 cost 평균: 0.352
epoch:    2 cost 평균: 0.089
epoch:    3 cost 평균: 0.066
epoch:    4 cost 평균: 0.056
epoch:    5 cost 평균: 0.047
epoch:    6 cost 평균: 0.042
epoch:    7 cost 평균: 0.039
epoch:    8 cost 평균: 0.036
epoch:    9 cost 평균: 0.033
epoch:   10 cost 평균: 0.030
epoch:   11 cost 평균: 0.029
epoch:   12 cost 평균: 0.036
epoch:   13 cost 평균: 0.027
epoch:   14 cost 평균: 0.026
epoch:   15 cost 평균: 0.024
epoch:   16 cost 평균: 0.026
epoch:   17 cost 평균: 0.024
epoch:   18 cost 평균: 0.025
epoch:   19 cost 평균: 0.028
epoch:   20 cost 평균: 0.026
epoch:   21 cost 평균: 0.024
epoch:   22 cost 평균: 0.031
epoch:   23 cost 평균: 0.029
epoch:   24 cost 평균: 0.027
epoch:   25 cost 평균: 0.035
epoch:   26 cost 평균: 0.035
epoch:   27 cost 평균: 0.031
epoch:   28 cost 평균: 0.029
epoch:   29 cost 평균: 0.021
epoch:   30 cost 평균: 0.028

 

 

모델 평가 

# 모델 평가 
isCorrect = tf.equal(tf.argmax(model,1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(isCorrect, tf.float32))
testSize = len(mnist.test.images)
testX = mnist.test.images.reshape(testSize, n_step, n_input)
testY = mnist.test.labels

print("정확도:",sess.run(accuracy, feed_dict={x:testX, y:testY}))
>>> 정확도: 0.9836