반응형

텐서플로우에서 다음과 같은 간단한 MNIST 예제를 실행시켜보면 매번 학습 결과가 다른 것을 확인할 수 있습니다.

실행할때마다 일정한 학습 결과를 얻는 방법을 다룹니다. 

 

2021. 7. 29  - 최초작성

 

# https://www.tensorflow.org/tutorials/quickstart/beginner
import tensorflow as tf


mnist = tf.keras.datasets.mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

model = tf.keras.models.Sequential([
  tf.keras.layers.Flatten(input_shape=(28, 28)),
  tf.keras.layers.Dense(128, activation='relu'),
  tf.keras.layers.Dropout(0.2),
  tf.keras.layers.Dense(10, activation='softmax')
])

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(x_train, y_train, epochs=5)

model.evaluate(x_test,  y_test, verbose=2)



세번 연달아 실행시킨 결과인데 학습 결과가 일정하지 않습니다. 



Epoch 2/5

1875/1875 [==============================] - 5s 2ms/step - loss: 0.1442 - accuracy: 0.9568       

Epoch 3/5

1875/1875 [==============================] - 5s 2ms/step - loss: 0.1085 - accuracy: 0.9668       

Epoch 4/5

1875/1875 [==============================] - 5s 2ms/step - loss: 0.0873 - accuracy: 0.9742       

Epoch 5/5

1875/1875 [==============================] - 5s 2ms/step - loss: 0.0757 - accuracy: 0.9765       

313/313 - 1s - loss: 0.0733 - accuracy: 0.9774




Epoch 2/5

1875/1875 [==============================] - 5s 3ms/step - loss: 0.1469 - accuracy: 0.9560       

Epoch 3/5

1875/1875 [==============================] - 4s 2ms/step - loss: 0.1097 - accuracy: 0.9669       

Epoch 4/5

1875/1875 [==============================] - 4s 2ms/step - loss: 0.0881 - accuracy: 0.9724       

Epoch 5/5

1875/1875 [==============================] - 4s 2ms/step - loss: 0.0751 - accuracy: 0.9772       

313/313 - 1s - loss: 0.0756 - accuracy: 0.9761

 

Epoch 2/5

1875/1875 [==============================] - 5s 3ms/step - loss: 0.1424 - accuracy: 0.9578

Epoch 3/5

1875/1875 [==============================] - 5s 3ms/step - loss: 0.1087 - accuracy: 0.9674

Epoch 4/5

1875/1875 [==============================] - 5s 3ms/step - loss: 0.0905 - accuracy: 0.9718

Epoch 5/5

1875/1875 [==============================] - 5s 3ms/step - loss: 0.0776 - accuracy: 0.9761

313/313 - 1s - loss: 0.0735 - accuracy: 0.9771




KERAS FAQ에서 소개하는 다음 코드를 파이썬 코드 첫줄에 추가한 후, 다시 MNIST 예제를 실행해봅니다.

출처 - https://keras.io/getting_started/faq/#how-can-i-obtain-reproducible-results-using-keras-during-development 

 

import numpy as np
import tensorflow as tf
import random as python_random

# The below is necessary for starting Numpy generated random numbers
# in a well-defined initial state.
np.random.seed(123)

# The below is necessary for starting core Python generated random numbers
# in a well-defined state.
python_random.seed(123)

# The below set_seed() will make random number generation
# in the TensorFlow backend have a well-defined initial state.
# For further details, see:
# https://www.tensorflow.org/api_docs/python/tf/random/set_seed
tf.random.set_seed(1234)



Epoch 2/5

1875/1875 [==============================] - 4s 2ms/step - loss: 0.1408 - accuracy: 0.9584       

Epoch 3/5

1875/1875 [==============================] - 4s 2ms/step - loss: 0.1090 - accuracy: 0.9667       

Epoch 4/5

1875/1875 [==============================] - 4s 2ms/step - loss: 0.0892 - accuracy: 0.9722       

Epoch 5/5

1875/1875 [==============================] - 4s 2ms/step - loss: 0.0763 - accuracy: 0.9765       

313/313 - 0s - loss: 0.0729 - accuracy: 0.9776



Epoch 2/5

1875/1875 [==============================] - 4s 2ms/step - loss: 0.1408 - accuracy: 0.9584       

Epoch 3/5

1875/1875 [==============================] - 4s 2ms/step - loss: 0.1090 - accuracy: 0.9667       

Epoch 4/5

1875/1875 [==============================] - 4s 2ms/step - loss: 0.0892 - accuracy: 0.9724       

Epoch 5/5

1875/1875 [==============================] - 4s 2ms/step - loss: 0.0767 - accuracy: 0.9761       

313/313 - 1s - loss: 0.0727 - accuracy: 0.9768



Epoch 2/5

1875/1875 [==============================] - 4s 2ms/step - loss: 0.1408 - accuracy: 0.9584

Epoch 3/5

1875/1875 [==============================] - 4s 2ms/step - loss: 0.1090 - accuracy: 0.9667

Epoch 4/5

1875/1875 [==============================] - 4s 2ms/step - loss: 0.0892 - accuracy: 0.9723

Epoch 5/5

1875/1875 [==============================] - 4s 2ms/step - loss: 0.0766 - accuracy: 0.9762

313/313 - 1s - loss: 0.0737 - accuracy: 0.9771



학습 결과가 앞에서 했던것 보다는 일정합니다. 완전히 일치하지는 않네요.

Dropout을 사용해서 영향을 준듯합니다. 

 

 

지금 사용한 MNIST 코드보다는 데이터셋을 무작위로 뒤섞은 후, 사용하는 경우에 효과가 더 좋았습니다.



반응형

해본 것을 문서화하여 기록합니다.


포스트 작성시에는 문제 없었지만 이후 문제가 생길 수 있습니다.
질문을 남겨주면 가능한 빨리 답변드립니다.


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

  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기

댓글을 달아 주세요

">