๐ŸŒŠ

Tensorflow Reproducible Result

Tags
Python
MachineLearning
ID matched
Created
Jan 28, 2023 12:07 PM
Last Updated
Last updated July 15, 2023
ย 
ย 
ย 

Reproducible Result?

  • ์ฝ”๋žฉ์—์„œ tensorflow๋ฅผ ํ™œ์šฉํ•œ CNN ๋ชจ๋ธ ํ•™์Šต์„ ์ง„ํ–‰ํ•˜๋‹ค๊ฐ€, ๋™์ผํ•œ ๋ ˆ์ด์–ด ๋ฐ ์„ค์ •์„ ํ•ด๋„ ๊ฒฐ๊ณผ๋งˆ๋‹ค loss ๋ฐ accuracy๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ๋‚˜์˜ค๋Š” ๊ฒฝ์šฐ๋ฅผ ๋ณด์•˜๋‹ค.
  • ๊ตฌ๊ธ€๋ง์„ ํ•ด๋ณด๋‹ˆ, random seed ๋ฐ gpu ์—ฐ์‚ฐ์— ๋”ฐ๋ผ์„œ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ๋‚˜์˜ค๋Š” ๊ฒƒ์ด ์›์ธ์ด์—ˆ๋‹ค.
  • ๋ ˆ์ด์–ด๋‚˜ ์„ค์ •์— ๋”ฐ๋ผ loss ๋ฐ accuracy๊ฐ€ ์–ด๋–ป๊ฒŒ ๋ณ€ํ™”๋˜๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์‹ถ์—ˆ๊ณ , ์ด์— ๋”ฐ๋ผ ์žฌํ˜„๊ฐ€๋Šฅํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋‚˜์˜ค๋„๋ก ์„ค์ •ํ•˜๊ณ  ์‹ถ์—ˆ๋‹ค.
  • ์ด์— ๋”ฐ๋ผ ๋ช‡๊ฐ€์ง€ ์„ค์ •์„ ์ ์šฉํ•ด์„œ, ๋™์ผํ•œ ๋ ˆ์ด์–ด ๋ฐ ์„ค์ •์—์„œ ๋™์ผํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋„์ถœ๋˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•˜์˜€๋‹ค.
ย 
ย 

ํ•„์š”ํ•œ ์„ค์ •

  • tensorflow์˜ utils์—์„œ set_random_seed ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์‹œ๋“œ๊ฐ’์„ ์„ค์ •ํ•œ๋‹ค.
  • tensorflow์˜ config์—์„œ enable_op_determinism ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
  • ๋…ธํŠธ๋ถ ์„ธ์…˜์„ ๋Š์ง€ ์•Š๊ณ  ๊ณ„์† ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๊ณ ์ž ํ•œ๋‹ค๋ฉด, tensorflow์˜ backend์—์„œ clear_session ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
ย 
ย 

๋…ธํŠธ๋ถ ์ฝ”๋“œ

import tensorflow as tf import numpy as np from keras.datasets import cifar10 from keras.utils import to_categorical from sklearn.model_selection import train_test_split from keras import models, layers from keras.callbacks import EarlyStopping, ModelCheckpoint from sklearn.metrics import confusion_matrix, classification_report
(X_train, y_train), (X_test, y_test) = cifar10.load_data() print("Train Shape", X_train.shape, y_train.shape) print("Test Shape", X_test.shape, y_test.shape)
notion image
print("Train Data") print(np.unique(y_train, return_counts=True)) print("Test Data") print(np.unique(y_test, return_counts=True))
notion image
X_train = X_train.astype(float) / 255 X_test = X_test.astype(float) / 255 y_train = to_categorical(y_train) y_test = to_categorical(y_test)
X_train, X_valid, y_train, y_valid = train_test_split(X_train, y_train, test_size=0.2, random_state=2045) print("Train Shape:", X_train.shape, y_train.shape) print("Valid Shape:", X_valid.shape, y_valid.shape)
notion image
tf.keras.backend.clear_session() tf.config.experimental.enable_op_determinism() tf.keras.utils.set_random_seed(2045) CIFAR = models.Sequential() CIFAR.add(layers.Conv2D(64, 2, input_shape=(32, 32, 3,))) CIFAR.add(layers.MaxPool2D(3)) CIFAR.add(layers.Conv2D(32, 2)) CIFAR.add(layers.MaxPool2D(3)) CIFAR.add(layers.Flatten()) CIFAR.add(layers.Dense(1024, activation='relu')) CIFAR.add(layers.Dropout(0.6)) CIFAR.add(layers.Dense(512, activation='relu')) CIFAR.add(layers.Dropout(0.4)) CIFAR.add(layers.Dense(128, activation='relu')) CIFAR.add(layers.Dense(10, activation='softmax'))
CIFAR.summary()
notion image
CIFAR.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
%%time batch_size = 128 es = EarlyStopping(monitor='accuracy', mode='max', patience=50, verbose=1) mc = ModelCheckpoint('best_CIFAR.h5', monitor='accuracy', mode='max', save_best_only=True, verbose=1) Hist_CIFAR = CIFAR.fit(X_train, y_train, epochs=10, batch_size=batch_size,\ callbacks=[es, mc], validation_data=(X_valid, y_valid),\ workers=1)
notion image
best_CIFAR = models.load_model('best_CIFAR.h5') loss, accuracy = best_CIFAR.evaluate(X_test, y_test, verbose=0) print('Loss = {:.5f}'.format(loss)) print('Accuracy = {:.5f}'.format(accuracy))
notion image
y_real = np.argmax(y_test, axis=1) preditions = best_CIFAR.predict(X_test) y_pred = np.argmax(preditions, axis=1) print(confusion_matrix(y_real, y_pred)) print(classification_report(y_real, y_pred))
notion image
ย 
ย 

ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ

  • ์—ฌ๋Ÿฌ๋ฒˆ ์‹œ๋„ํ•ด๋„ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋„์ถœํ•œ๋‹ค
  • ๋‹ค๋งŒ ๊ตฌ๋™ํ•˜๋Š” ํ•˜๋“œ์›จ์–ด๊ฐ€ ๋‹ค๋ฅธ ๊ฒฝ์šฐ์— ํ•˜๋“œ์›จ์–ด์— ๋”ฐ๋ผ ๋‹ค๋ฅธ ๊ฒฐ๊ณผ๋ฅผ ๋‚˜ํƒ€๋‚ธ๋‹ค.
    • ๋กœ์ปฌ ์‹คํ–‰ ๊ฒฐ๊ณผ์™€ ์ฝ”๋žฉ ์‹คํ–‰ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅด๊ฒŒ ๋‚˜์™”๋‹ค.
ย