■ 오토 인코더를 만드는 방법을 보여준다.
▶ 예제 코드 (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 |
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() |