■ 학습을 조기 종료시키는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
import keras import keras.callbacks as callbacks import keras.datasets.mnist as mnist import keras.models as models import keras.utils as utils import keras.layers as layers import matplotlib.pyplot as pp import numpy as np np.random.seed(3) print("데이터 로드를 시작합니다.") (trainInputNDArray, trainCottectOutputNDArray), (testInputNDArray, testCorrectOutputNDArray) = mnist.load_data() # trainInputNDArray : (60000, 28, 28) # trainCottectOutputNDArray : (60000,) # testInputNDArray : (10000, 28, 28) # testCorrectOutputNDArray : (10000,) # 훈련/검증 데이터를 분리한다. validationInputNDArray = trainInputNDArray[50000:] validationCorrectOutputNDArray = trainCottectOutputNDArray[50000:] trainInputNDArray = trainInputNDArray[:50000] trainCottectOutputNDArray = trainCottectOutputNDArray[:50000] # 훈련/검증/테스트 데이터 trainInputNDArray = trainInputNDArray.reshape(50000, 784).astype("float32") / 255.0 validationInputNDArray = validationInputNDArray.reshape(10000, 784).astype("float32") / 255.0 testInputNDArray = testInputNDArray.reshape(10000, 784).astype("float32") / 255.0 # trainInputNDArray : (50000, 784) # validationInputNDArray : (10000, 784) # testInputNDArray : (10000, 784) # 훈련/검증 데이터를 섞는다. trainRandomIndexNDArray = np.random.choice(50000, 700) validationRandomIndexNDArray = np.random.choice(10000, 300) trainInputNDArray = trainInputNDArray[trainRandomIndexNDArray] trainCottectOutputNDArray = trainCottectOutputNDArray[trainRandomIndexNDArray] validationInputNDArray = validationInputNDArray[validationRandomIndexNDArray] validationCorrectOutputNDArray = validationCorrectOutputNDArray[validationRandomIndexNDArray] # trainInputNDArray : (700, 784) # trainCottectOutputNDArray : (700,) # validationInputNDArray : (300, 784) # validationCorrectOutputNDArray : (300,) # 정답 데이터에 대해 원핫 인코딩 처리한다. trainCottectOutputNDArray = utils.np_utils.to_categorical(trainCottectOutputNDArray) validationCorrectOutputNDArray = utils.np_utils.to_categorical(validationCorrectOutputNDArray) testCorrectOutputNDArray = utils.np_utils.to_categorical(testCorrectOutputNDArray) # trainCottectOutputNDArray : (50000, 10) # validationCorrectOutputNDArray : (10000, 10) # testCorrectOutputNDArray : (10000, 10) print("데이터 로드를 종료합니다.") print("모델 정의를 시작합니다.") model = models.Sequential() model.add(layers.Dense(units = 64, input_dim = 784, activation = "relu")) model.add(layers.Dense(units = 10, activation = "softmax")) model.compile(loss = "categorical_crossentropy", optimizer = "sgd", metrics = ["accuracy"]) print("모델 정의를 종료합니다.") print("모델 학습을 시작합니다.") earlyStopping = callbacks.EarlyStopping(patience = 20) history = model.fit(trainInputNDArray, trainCottectOutputNDArray, epochs = 1000, batch_size = 100,\ validation_data = (validationInputNDArray, validationCorrectOutputNDArray), callbacks = [earlyStopping]) print("모델 학습을 종료합니다.") print("학습 결과를 조회합니다.") evaluateList = model.evaluate(testInputNDArray, testCorrectOutputNDArray, batch_size = 32) print("") print("loss : " + str(evaluateList[0])) print("accuracy : " + str(evaluateList[1])) figure, lossAxeSubplot = pp.subplots() accuracyAxeSubplot = lossAxeSubplot.twinx() lossAxeSubplot.plot(history.history["loss" ], "y", label = "train loss") lossAxeSubplot.plot(history.history["val_loss"], "r", label = "val loss" ) accuracyAxeSubplot.plot(history.history["acc" ], "b", label = "train acc") accuracyAxeSubplot.plot(history.history["val_acc"], "g", label = "val acc" ) lossAxeSubplot.set_xlabel("epoch") lossAxeSubplot.set_ylabel("loss") lossAxeSubplot.legend(loc = "upper left") accuracyAxeSubplot.set_ylabel("accuracy") accuracyAxeSubplot.legend(loc = "lower left") pp.show() |