Tensorflow에서 재현 가능한 결과(reproducible results) 얻기
텐서플로우에서 다음과 같은 간단한 MNIST 예제를 실행시켜보면 매번 학습 결과가 다른 것을 확인할 수 있습니다.
실행할때마다 일정한 학습 결과를 얻는 방법을 다룹니다.
2021. 7. 29 - 최초작성
2024. 3. 21 - RANDOM_SEED 변경, Tensorflow 2.15에서 테스트
2024. 3. 28 재현가능한 결과 보장 방법 수정
# 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) |
세번 연달아 실행시킨 결과인데 빨간색으로 표시한 train 데이터셋에 대한 수치와 파란색으로 표시한 test 데이터셋에 대한 수치가 일정하지 않습니다.
Epoch 1/5
2024-03-21 21:31:17.726368: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:117] Plugin optimizer for device_type GPU is enabled.
1875/1875 [==============================] - 10s 4ms/step - loss: 0.4016 - accuracy: 0.8839
Epoch 2/5
1875/1875 [==============================] - 7s 4ms/step - loss: 0.3636 - accuracy: 0.9006
Epoch 3/5
1875/1875 [==============================] - 7s 4ms/step - loss: 0.3803 - accuracy: 0.8968
Epoch 4/5
1875/1875 [==============================] - 7s 4ms/step - loss: 0.3978 - accuracy: 0.8955
Epoch 5/5
1875/1875 [==============================] - 7s 4ms/step - loss: 0.4053 - accuracy: 0.8933
313/313 - 1s - loss: 0.3092 - accuracy: 0.9190 - 958ms/epoch - 3ms/step
Epoch 1/5
2024-03-21 21:32:23.872847: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:117] Plugin optimizer for device_type GPU is enabled.
1875/1875 [==============================] - 8s 4ms/step - loss: 0.4064 - accuracy: 0.8833
Epoch 2/5
1875/1875 [==============================] - 7s 4ms/step - loss: 0.3709 - accuracy: 0.8987
Epoch 3/5
1875/1875 [==============================] - 7s 4ms/step - loss: 0.3858 - accuracy: 0.8957
Epoch 4/5
1875/1875 [==============================] - 7s 4ms/step - loss: 0.4018 - accuracy: 0.8930
Epoch 5/5
1875/1875 [==============================] - 7s 4ms/step - loss: 0.4110 - accuracy: 0.8922
313/313 - 1s - loss: 0.3157 - accuracy: 0.9170 - 926ms/epoch - 3ms/step
Epoch 1/5
2024-03-21 21:33:21.468247: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:117] Plugin optimizer for device_type GPU is enabled.
1875/1875 [==============================] - 7s 4ms/step - loss: 0.4073 - accuracy: 0.8830
Epoch 2/5
1875/1875 [==============================] - 7s 4ms/step - loss: 0.3660 - accuracy: 0.9006
Epoch 3/5
1875/1875 [==============================] - 7s 4ms/step - loss: 0.3765 - accuracy: 0.8978
Epoch 4/5
1875/1875 [==============================] - 7s 4ms/step - loss: 0.3911 - accuracy: 0.8967
Epoch 5/5
1875/1875 [==============================] - 7s 4ms/step - loss: 0.4060 - accuracy: 0.8944
313/313 - 1s - loss: 0.3500 - accuracy: 0.9090 - 946ms/epoch - 3ms/step
지난번 글 업데이트시 재현 가능한 결과가 보여지는 것을 확인한 후 문제 없는줄 알았는데 얼마전 다른 PC에서 확인했는데 전혀 재현 가능한 결과를 얻을 수 없었습니다. 정확한 원인은 알 수 없었지만 최근 업데이트된 케라스 문서에서 새로운 방법을 찾아서 해결했습니다.
다음 링크에서 소개하는 방법입니다.
https://keras.io/examples/keras_recipes/reproducibility_recipes/
import tensorflow as tf # Set the seed using keras.utils.set_random_seed. This will set: # 1) `numpy` seed # 2) backend random seed # 3) `python` random seed tf.keras.utils.set_random_seed(812) # If using TensorFlow, this will make GPU ops as deterministic as possible, # but it will affect the overall performance, so be mindful of that. tf.config.experimental.enable_op_determinism() |
세번 실행하여 똑같은 학습 결과를 얻었습니다. 빨간색으로 표시한 train 데이터셋에 대한 수치와 파란색으로 표시한 test 데이터셋에 대한 수치가 일정합니다.
Epoch 1/5
2024-03-28 20:45:23.575054: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:117] Plugin optimizer for device_type GPU is enabled.
1875/1875 [==============================] - 10s 5ms/step - loss: 0.4142 - accuracy: 0.8810
Epoch 2/5
1875/1875 [==============================] - 9s 5ms/step - loss: 0.3737 - accuracy: 0.8973
Epoch 3/5
1875/1875 [==============================] - 9s 5ms/step - loss: 0.3852 - accuracy: 0.8968
Epoch 4/5
1875/1875 [==============================] - 9s 5ms/step - loss: 0.4024 - accuracy: 0.8930
Epoch 5/5
1875/1875 [==============================] - 9s 5ms/step - loss: 0.4193 - accuracy: 0.8901
313/313 - 1s - loss: 0.3629 - accuracy: 0.9024 - 1s/epoch - 4ms/step
Epoch 1/5
2024-03-28 20:47:19.116479: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:117] Plugin optimizer for device_type GPU is enabled.
1875/1875 [==============================] - 9s 5ms/step - loss: 0.4142 - accuracy: 0.8810
Epoch 2/5
1875/1875 [==============================] - 9s 5ms/step - loss: 0.3737 - accuracy: 0.8973
Epoch 3/5
1875/1875 [==============================] - 9s 5ms/step - loss: 0.3852 - accuracy: 0.8968
Epoch 4/5
1875/1875 [==============================] - 9s 5ms/step - loss: 0.4024 - accuracy: 0.8930
Epoch 5/5
1875/1875 [==============================] - 9s 5ms/step - loss: 0.4193 - accuracy: 0.8901
313/313 - 1s - loss: 0.3629 - accuracy: 0.9024 - 1s/epoch - 4ms/step
Epoch 1/5
2024-03-28 20:49:01.188213: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:117] Plugin optimizer for device_type GPU is enabled.
1875/1875 [==============================] - 9s 5ms/step - loss: 0.4142 - accuracy: 0.8810
Epoch 2/5
1875/1875 [==============================] - 9s 5ms/step - loss: 0.3737 - accuracy: 0.8973
Epoch 3/5
1875/1875 [==============================] - 9s 5ms/step - loss: 0.3852 - accuracy: 0.8968
Epoch 4/5
1875/1875 [==============================] - 9s 5ms/step - loss: 0.4024 - accuracy: 0.8930
Epoch 5/5
1875/1875 [==============================] - 9s 5ms/step - loss: 0.4193 - accuracy: 0.8901
313/313 - 1s - loss: 0.3629 - accuracy: 0.9024 - 1s/epoch - 4ms/step