■ 히스토리를 텐서보드와 연동하는 방법을 보여준다.
▶ 예제 코드 (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 |
import keras import keras.datasets.mnist as mnist import keras.models as models import keras.utils as utils import keras.layers as layers 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("모델 학습을 시작합니다.") tensorBoardHistory = keras.callbacks.TensorBoard(log_dir = "/graph", histogram_freq = 0, write_graph = True,\ write_images = True) model.fit(trainInputNDArray, trainCottectOutputNDArray, epochs = 1000, batch_size = 10,\ validation_data = (validationInputNDArray, validationCorrectOutputNDArray), callbacks = [tensorBoardHistory]) print("모델 학습을 종료합니다.") # 명령 프롬프트에서 아래 명령을 실행합니다. # tensorboard --logdir=/graph |