반응형


텐서플로우로 구현한 Logistic Regression입니다. MNIST 데이터를 가지고 손글씨 이미지를 훈련시키는 과정을 설명하고 있습니다.

다음 포스팅에서는 실제 손글씨를 촬영한 이미지를 인식하도록 해봅니다.




다음 사이트에 있는 텐서플로우 예제들을 공부한 결과를 비정기적으로 올리고 있습니다.

https://github.com/aymericdamien/TensorFlow-Examples






텐서플로우 2.0에서 텐서플로우 1.x 코드를 실행하는 방법을 설명합니다. 


Tensorflow 2.0에서 Tensorflow 1.x 코드 실행하기

https://webnautes.tistory.com/1393



# 수정 및 주석 : webnautes


import tensorflow as tf



#---------------------------------------------------------------------------------------------------- 1. MNIST 데이터를 가져옵니다.
# MNIST 데이터 관련 내용은 다음 포스팅 참고
#
# Tensorflow 예제 - MNIST 데이터 출력해보기 ( http://webnautes.tistory.com/1232 )
from tensorflow.examples.tutorials.mnist import input_data

# one_hot을 True로 설정하면 라벨을 one hot 벡터로 합니다. False이면 정수형 숫자가 라벨이 됩니다.
# /tmp/data/ 폴더를 생성하고 MNIST 데이터 압축파일을 다운로드 받아 압축을 풀고 데이터를 읽어옵니다.
# 이후에는 다운로드 되어있는 압축파일의 압축을 풀어서 데이터를 읽어옵니다.
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)



#---------------------------------------------------------------------------------------------------- 2. 모델을 생성합니다.
# 모델의 입력을 받기 위해 플레이스홀더를 사용합니다.
# 첫번째 차원이 None인 이유는 데이터 개수 제약없이 입력 받기 위해서입니다.
# 두번째 차원이 784인 것은  MNIST의 이미지의 크기가 28 x 28 = 784 픽셀이기 때문입니다.
x = tf.placeholder(tf.float32, [None, 784])

# 모델 파라미터
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))

# softmax를 사용한 모델을 생성
# W, b 모델 파라미터 -> 변수
# x 이미지 데이터 입력 -> 플레이스홀더
y_model = tf.matmul(x, W) + b



#---------------------------------------------------------------------------------------------------- 3. loss와 optimizer를 정의합니다.
y = tf.placeholder(tf.float32, [None, 10])  # 크기 10인 MNIST의 라벨 데이터

# 크로스 엔트로피(cross entropy) 함수 공식을 그대로 사용하면 수치적으로 불안정하여 계산 오류가 발생할 수 있습니다.
# cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(tf.nn.softmax(y_model)), reduction_indices=1))
#
# 그래서 tf.nn.softmax_cross_entropy_with_logits_v2를 사용합니다. (tf.nn.softmax_cross_entropy_with_logits는 deprecated 되었습니다.)

# cross entropy를 손실 함수(cost function)로 사용
#cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(tf.nn.softmax(y_model)), reduction_indices=1))
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y, logits=y_model))

# Gradient Descent - Backpropagation 기법으로 최적화
optimizer = tf.train.GradientDescentOptimizer(0.01).minimize(cost) # learning_rate = 0.01



#---------------------------------------------------------------------------------------------------- 4. 훈련을 위한 세션 시작
sess = tf.Session()


sess.run(tf.global_variables_initializer()) # 변수 초기화


for epoch in range(25): # 훈련을 25번 반복
   avg_cost = 0.

   # 1번 훈련시 전체 훈련 데이터를 사용하려면 100개씩 몇번 가져와야 하는지 계산하여 반복
   total_batch = int(mnist.train.num_examples / 100)
   for i in range(total_batch):
       # 전체 훈련 데이터(mnist.train)에서 100개씩 데이터를 가져옵니다.
       # (100, 784) (100, 10)
       batch_xs, batch_ys = mnist.train.next_batch(100)

       # optimizer와 cost 오퍼레이션을 실행합니다.
       _, c = sess.run([optimizer, cost], feed_dict={x: batch_xs, y: batch_ys})

       # 현재까지 평균 손실(loss)를 누적합니다.
       avg_cost += c / total_batch

   # 훈련 1번 끝날때 마다 중간 결과를 출력
   print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost))


print("최적화 완료")



#---------------------------------------------------------------------------------------------------- 5. 정확도 측정
# 라벨값 y와 모델로 계산된 값 y_model이 똑같이 같은 인덱스가 제일 크다고 하는지 검사
# ( tf.argmax 함수가 배열에서 가장 큰 값을 가리키는 인덱스를 리턴합니다.. )
# 결과적으로 correct_prediction는 True 또는 False 값의 리스트를 가지게 됨
#
# tf.argmax에 대한 자세한 내용은 다음 포스팅 참고
# Tensorflow 예제 - tf.argmax 함수 사용법 ( http://webnautes.tistory.com/1234 )
correct_prediction = tf.equal(tf.argmax(y_model, 1), tf.argmax(y, 1))


# tf.cast 함수를 사용하여 True 또는 False를 실수 1 또는 0으로 변환
# 전체 데이터가 일치한다면 모든 값이 1이며 평균인 accuracy는 1이 되어야 합니다.
#
# tf.reduce_mean에 대한 자세한 내용은 다음 포스팅 참고
# Tensorflow 예제 - tf.reduce_mean 함수 사용법 ( http://webnautes.tistory.com/1235 )
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

# 정확도 측정을 위해서 훈련 데이터(mnist.train) 대신에 별도의 테스트 데이터(mnist.test)를 사용해야 합니다.
print("Accuracy:", sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels}))

sess.close()



텐서플로우 1.8 이상을 사용중이라면 경고 메시지(붉은색 부분)가 보입니다.

tensorflow.examples.tutorials.mnist가 deprecated 되었기 때문입니다. 언제가 될지 모르지만 이후 버전에서는 tensorflow.examples.tutorials.mnist를 사용할 수 없게 된다는 의미입니다.



WARNING:tensorflow:From C:/Users/webnautes/PycharmProjects/TensorFlow_Project/tensorflow_example0.py:15: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.

Instructions for updating:

Please use alternatives such as official/mnist/dataset.py from tensorflow/models.

WARNING:tensorflow:From C:\Users\webnautes\PycharmProjects\TensorFlow_Project\venv\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.

Instructions for updating:

Please write your own downloading logic.

WARNING:tensorflow:From C:\Users\webnautes\PycharmProjects\TensorFlow_Project\venv\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.

Extracting /tmp/data/train-images-idx3-ubyte.gz

Instructions for updating:

Please use tf.data to implement this functionality.

Extracting /tmp/data/train-labels-idx1-ubyte.gz

WARNING:tensorflow:From C:\Users\webnautes\PycharmProjects\TensorFlow_Project\venv\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.

Extracting /tmp/data/t10k-images-idx3-ubyte.gz

Instructions for updating:

Please use tf.data to implement this functionality.

WARNING:tensorflow:From C:\Users\webnautes\PycharmProjects\TensorFlow_Project\venv\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.

Instructions for updating:

Please use tf.one_hot on tensors.

Extracting /tmp/data/t10k-labels-idx1-ubyte.gz

WARNING:tensorflow:From C:\Users\webnautes\PycharmProjects\TensorFlow_Project\venv\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version.

Instructions for updating:

Please use alternatives such as official/mnist/dataset.py from tensorflow/models.

Epoch: 0001 cost= 1.183076352

Epoch: 0002 cost= 0.665184037

Epoch: 0003 cost= 0.552788179

Epoch: 0004 cost= 0.498665012

Epoch: 0005 cost= 0.465521260

Epoch: 0006 cost= 0.442458578

Epoch: 0007 cost= 0.425512567

Epoch: 0008 cost= 0.412130045

Epoch: 0009 cost= 0.401401070

Epoch: 0010 cost= 0.392384722

Epoch: 0011 cost= 0.384796427

Epoch: 0012 cost= 0.378197923

Epoch: 0013 cost= 0.372393805

Epoch: 0014 cost= 0.367272223

Epoch: 0015 cost= 0.362745550

Epoch: 0016 cost= 0.358568502

Epoch: 0017 cost= 0.354875129

Epoch: 0018 cost= 0.351518696

Epoch: 0019 cost= 0.348347432

Epoch: 0020 cost= 0.345422015

Epoch: 0021 cost= 0.342711662

Epoch: 0022 cost= 0.340236284

Epoch: 0023 cost= 0.337918483

Epoch: 0024 cost= 0.335740143

Epoch: 0025 cost= 0.333700363

최적화 완료

Accuracy: 0.9144





원본 코드

https://github.com/aymericdamien/TensorFlow-Examples/blob/master/examples/2_BasicModels/logistic_regression.py


마지막 업데이트 - 2018. 8. 30



반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.

도움이 되셨다면 토스아이디로 후원해주세요.
https://toss.me/momo2024


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts