첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

■ 오토 인코더를 만드는 방법을 보여준다.

 

▶ 예제 코드 (PY)

import matplotlib.pyplot as pp
import numpy as np
import tensorflow as tf
import tensorflow.examples.tutorials.mnist as mnist

# MNIST 데이터를 로드한다.
mnistDatasets = mnist.input_data.read_data_sets("data", one_hot = True)

# 파라미터를 설정한다.
learningRate = 0.01
epochCount   = 20
batchSize    = 256
displayStep  = 1
exampleCount = 10

# 네트워크 파라미터를 설정한다.
inputLayerNodeCount   = 784 # 28×28픽셀
hiddenLayer1NodeCount = 256
hiddenLayer2NodeCount = 128

# 모델을 정의한다.
inputLayerTensor = tf.placeholder("float", [None, inputLayerNodeCount])

encoderHiddenLayer1WeightVariable = tf.Variable(tf.random_normal([inputLayerNodeCount  , hiddenLayer1NodeCount]))
encoderHiddenLayer2WeightVariable = tf.Variable(tf.random_normal([hiddenLayer1NodeCount, hiddenLayer2NodeCount]))
decoderHiddenLayer1WeightVariable = tf.Variable(tf.random_normal([hiddenLayer2NodeCount, hiddenLayer1NodeCount]))
decoderHiddenLayer2WeightVariable = tf.Variable(tf.random_normal([hiddenLayer1NodeCount, inputLayerNodeCount  ]))

encoderHiddenLayer1BiasVariable = tf.Variable(tf.random_normal([hiddenLayer1NodeCount]))
encoderHiddenLayer2BiasVariable = tf.Variable(tf.random_normal([hiddenLayer2NodeCount]))
decoderHiddenLayer1BiasVariable = tf.Variable(tf.random_normal([hiddenLayer1NodeCount]))
decoderHiddenLayer2BiasVariable = tf.Variable(tf.random_normal([inputLayerNodeCount  ]))

encoderHiddenLayer1OutputTensor = tf.nn.sigmoid(tf.add(tf.matmul(inputLayerTensor               , encoderHiddenLayer1WeightVariable), encoderHiddenLayer1BiasVariable))
encoderHiddenLayer2OutputTensor = tf.nn.sigmoid(tf.add(tf.matmul(encoderHiddenLayer1OutputTensor, encoderHiddenLayer2WeightVariable), encoderHiddenLayer2BiasVariable))
decoderHiddenLayer1OutputTensor = tf.nn.sigmoid(tf.add(tf.matmul(encoderHiddenLayer2OutputTensor, decoderHiddenLayer1WeightVariable), decoderHiddenLayer1BiasVariable))
decoderHiddenLayer2OutputTensor = tf.nn.sigmoid(tf.add(tf.matmul(decoderHiddenLayer1OutputTensor, decoderHiddenLayer2WeightVariable), decoderHiddenLayer2BiasVariable))
outputLayerOutputTensor         = decoderHiddenLayer2OutputTensor
correctOutputTensor             = inputLayerTensor

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

optimizerOperation = tf.train.RMSPropOptimizer(learningRate).minimize(costTensor)

# 그래프를 실행한다.
with tf.Session() as session:
    session.run(tf.global_variables_initializer())
    batchCount = int(mnistDatasets.train.num_examples / batchSize)
    for epoch in range(epochCount):
        for batch in range(batchCount):
            batchInputNDArray, _ = mnistDatasets.train.next_batch(batchSize)
            _, cost = session.run([optimizerOperation, costTensor], feed_dict = {inputLayerTensor : batchInputNDArray})
        if epoch % displayStep == 0:
            print("Epoch :", '%04d' % (epoch + 1), "비용 = ", "{:.9f}".format(cost))
    print("최적화가 완료되었습니다!")

    # 테스트 집합에 대해 인코딩/디코딩을 처리한다.
    outputImageNDArray = session.run(outputLayerOutputTensor, feed_dict= {inputLayerTensor : mnistDatasets.test.images[:exampleCount]})

    # 원본/복원 이미지를 비교한다.
    figure, axesNDArray = pp.subplots(2, 10, figsize = (10, 2))
    for example in range(exampleCount):
        axesNDArray [0][example].imshow(np.reshape(mnistDatasets.test.images[example], (28, 28)))
        axesNDArray [1][example].imshow(np.reshape(outputImageNDArray[example], (28, 28)))
    figure.show()
    pp.draw()
    pp.show()
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요