첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

728x90
반응형
import matplotlib.pyplot as pp
import numpy as np
import tensorflow as tf
import tensorflow.examples.tutorials.mnist as mnist

def DrawResult(originalImageNDArray, noisyImageNDArray, outputImageNDArray):
    pp.matshow(np.reshape(originalImageNDArray, (28, 28)), cmap = pp.get_cmap("gray"))
    pp.title("Original Image")
    pp.colorbar()

    pp.matshow(np.reshape(noisyImageNDArray, (28, 28)), cmap = pp.get_cmap("gray"))
    pp.title("Input Image")
    pp.colorbar()
    
    pp.matshow(np.reshape(outputImageNDArray, (28, 28)), cmap = pp.get_cmap("gray"))
    pp.title("Reconstructed Image")
    pp.colorbar()

    pp.show()

# 네트워크 파라미터
InputLayerNodeCount   = 784
hiddenLayer1NodeCount = 256 
hiddenLayer2NodeCount = 256 
outputLayerNodeCount  = 784

epochCount  = 100
batchSize   = 100
displayStep = 10

print("MNIST 데이터 로드를 시작합니다.")

mnistDatasets = mnist.input_data.read_data_sets("data", one_hot = True)

print("MNIST 데이터 로드를 종료합니다.")

# 네트워크 모델을 정의한다.
inputLayerTensor    = tf.placeholder("float", [None, InputLayerNodeCount ])
correctOutputTensor = tf.placeholder("float", [None, outputLayerNodeCount])
dropoutRateTensor   = tf.placeholder("float")

hiddenLayer1WeightVariable = tf.Variable(tf.random_normal([InputLayerNodeCount  , hiddenLayer1NodeCount]))
hiddenLayer2WeightVariable = tf.Variable(tf.random_normal([hiddenLayer1NodeCount, hiddenLayer2NodeCount]))
outputLayerWeightVariable  = tf.Variable(tf.random_normal([hiddenLayer2NodeCount, outputLayerNodeCount ]))

hiddenLayer1BiasVariable = tf.Variable(tf.random_normal([hiddenLayer1NodeCount]))
hiddenLayer2BiasVariable = tf.Variable(tf.random_normal([hiddenLayer2NodeCount]))
outputLayerBiasVariable  = tf.Variable(tf.random_normal([outputLayerNodeCount ]))

hiddenLayer1OutputTensor        = tf.nn.sigmoid(tf.add(tf.matmul(inputLayerTensor, hiddenLayer1WeightVariable), hiddenLayer1BiasVariable))
hiddenLayer1OutputTensorDropout = tf.nn.dropout(hiddenLayer1OutputTensor, dropoutRateTensor)
hiddenLayer2OutputTensor        = tf.nn.sigmoid(tf.add(tf.matmul(hiddenLayer1OutputTensorDropout, hiddenLayer2WeightVariable), hiddenLayer2BiasVariable))
hiddenLayer2OutputTensorDropout = tf.nn.dropout(hiddenLayer2OutputTensor, dropoutRateTensor)
outputLayerOutputTensor         = tf.nn.sigmoid(tf.matmul(hiddenLayer2OutputTensorDropout, outputLayerWeightVariable) + outputLayerBiasVariable)

costTensor = tf.reduce_mean(tf.pow(outputLayerOutputTensor - correctOutputTensor, 2))

optmizerOperation = tf.train.RMSPropOptimizer(0.01).minimize(costTensor)

# 그래를 실행한다.
with tf.Session() as session:
    session.run(tf.global_variables_initializer())
    print("훈련을 시작합니다.")
    for epoch in range(epochCount):
        batchCount  = int(mnistDatasets.train.num_examples / batchSize)
        totalCost = 0.
        for batch in range(batchCount):
            batchInputNDArray, batchCorrectOutputNDArray = mnistDatasets.train.next_batch(batchSize)
            noisyBatchInputNDArray = batchInputNDArray + 0.3 * np.random.randn(batchSize, 784)
            feedDictionary = {inputLayerTensor : noisyBatchInputNDArray, correctOutputTensor : batchInputNDArray, dropoutRateTensor : 0.8}
            session.run(optmizerOperation, feed_dict = feedDictionary)
            totalCost += session.run(costTensor, feed_dict = feedDictionary)
        if epoch % displayStep == 0:
            print("Epoch : %02d/%02d, 평균 비용 : %.6f" % (epoch, epochCount, totalCost / batchCount))

            print("테스트를 시작합니다.")

            testIndex = np.random.randint(mnistDatasets.test.images.shape[0], size = 1)

            originalImageNDArray = mnistDatasets.test.images[testIndex, :]
            testImageNDArray     = mnistDatasets.test.images[testIndex, :]
            testImageLabel       = np.argmax(mnistDatasets.test.labels[testIndex, :], 1)

            print("테스트 이미지 라벨 : %d" % (testImageLabel))

            noisyTestImageNDArray = testImageNDArray + 0.3 * np.random.randn(1, 784)

            outputImageNDArray = session.run(outputLayerOutputTensor, feed_dict = {inputLayerTensor : noisyTestImageNDArray, dropoutRateTensor : 1})

            DrawResult(originalImageNDArray, noisyTestImageNDArray, outputImageNDArray)

            print("훈련을 다시 시작합니다.")
728x90
반응형
Posted by 사용자 icodebroker

댓글을 달아 주세요