■ 히스토리를 텐서보드와 연동하기

----------------------------------------------------------------------------------------------------

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

----------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker

댓글을 달아 주세요