13.auto-encoder

auto-encoder

  • auto-encoder는 입력값과 출력값을 같게하는 신경망
  • 가운데 hidden layer가 input layer보다 작아 데이터를 압축하는 효과를 갖음
  • 이 과정으로 인해 noise 제거에 효과적
  • auto-encoder의 원리는 출력값을 입력값과 같아지도록 가중치를 찾아냄
In [1]:
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt


mnist dataload

In [3]:
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("./mnist/data/", one_hot=True)
Extracting ./mnist/data/train-images-idx3-ubyte.gz
Extracting ./mnist/data/train-labels-idx1-ubyte.gz
Extracting ./mnist/data/t10k-images-idx3-ubyte.gz
Extracting ./mnist/data/t10k-labels-idx1-ubyte.gz


hyper parameter

In [4]:
learning_rate = 0.001 # learning_rate
training_epoch = 20 # 전체 횟수
batch_size = 256 # 한 번에 학습할 데이터(이미지 갯수)
n_hidden = 256 # hidden_layer 갯수
n_input = 28*28 # input_layer의 크기


setting place holder

In [5]:
X = tf.placeholder(tf.float32, [None, n_input], name="X")
# encoder와 decoder를 만드는 방식에 따라 다양한 auto-encoder를 맏들 수 있음


setting encoder

In [6]:
W_encode = tf.Variable(tf.random_normal([n_input, n_hidden]))
b_encode = tf.Variable(tf.random_normal([n_hidden]))

with tf.name_scope("encoder"):
    encoder = tf.add(tf.matmul(X, W_encode), b_encode)
    encoder = tf.nn.sigmoid(encoder)


setting decoder

In [7]:
W_decode = tf.Variable(tf.random_normal([n_hidden, n_input]))
b_decode = tf.Variable(tf.random_normal([n_input]))

with tf.name_scope("decoder"):
    decoder = tf.add(tf.matmul(encoder, W_decode), b_decode)
    decoder = tf.nn.sigmoid(decoder)


cost

In [8]:
# tf.pow()
# x = tf.constant([[2, 2], [3, 3]])
# y = tf.constant([[8, 16], [2, 3]])
# tf.pow(x, y)  # [[256, 65536], [9, 27]]

# 입력값인 X를 평가하기 위한 실츠값으로 사용, decoder가 내보낸 결과값의 차이를 cost로 구현
# 이 값의 차이는 거리함수로 구현
with tf.name_scope("cost"):
    cost = tf.reduce_mean(tf.pow(X-decoder, 2))
    opt = tf.train.RMSPropOptimizer(learning_rate).minimize(cost)
    
    tf.summary.scalar("cost", cost)
In [9]:
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
cost_epoch = []

merged = tf.summary.merge_all()
writer = tf.summary.FileWriter("./logs/mnist_autoencoder", sess.graph)

total_batch = int(mnist.train.num_examples / batch_size)

for epoch in range(training_epoch):
    total_cost = 0
    
    for i in range(total_batch):
        batch_xs, batch_ys = mnist.train.next_batch(batch_size)
        _, cost_val = sess.run([opt, cost], feed_dict={X: batch_xs})
        
        total_cost += cost_val
        cost_epoch.append(total_cost)
        
        summary = sess.run(merged, feed_dict={X: batch_xs})
        writer.add_summary(summary)
    print("Epoch: %4d,  Avg.cost %.4f " % (epoch+1, total_cost/total_batch))
print("opt complete")        
Epoch:    1,  Avg.cost 0.4240 
Epoch:    2,  Avg.cost 0.1718 
Epoch:    3,  Avg.cost 0.1135 
Epoch:    4,  Avg.cost 0.1032 
Epoch:    5,  Avg.cost 0.0961 
Epoch:    6,  Avg.cost 0.0911 
Epoch:    7,  Avg.cost 0.0870 
Epoch:    8,  Avg.cost 0.0833 
Epoch:    9,  Avg.cost 0.0805 
Epoch:   10,  Avg.cost 0.0773 
Epoch:   11,  Avg.cost 0.0749 
Epoch:   12,  Avg.cost 0.0725 
Epoch:   13,  Avg.cost 0.0709 
Epoch:   14,  Avg.cost 0.0695 
Epoch:   15,  Avg.cost 0.0682 
Epoch:   16,  Avg.cost 0.0670 
Epoch:   17,  Avg.cost 0.0657 
Epoch:   18,  Avg.cost 0.0637 
Epoch:   19,  Avg.cost 0.0627 
Epoch:   20,  Avg.cost 0.0619 
opt complete
In [10]:
%matplotlib inline

plt.figure(figsize=(20, 8))
plt.plot(cost_epoch, "g")
plt.title("cost_value")
plt.show()
In [11]:
import jptensor as jp

tf_graph = tf.get_default_graph().as_graph_def()
jp.show_graph(tf_graph)
In [12]:
sample_size = 10

samples = sess.run(decoder, feed_dict={X: mnist.test.images[:sample_size]})
In [13]:
%matplotlib inline

fig, ax = plt.subplots(2, sample_size, figsize=(sample_size, 2))

for i in range(sample_size):
    ax[0][i].set_axis_off()
    ax[1][i].set_axis_off()
    ax[0][i].imshow(np.reshape(mnist.test.images[i], (28, 28)))
    ax[1][i].imshow(np.reshape(samples[i], (28, 28)))
plt.show()
In [14]:
from IPython.core.display import HTML, display
display(HTML("<style> .container{width:100% !important;}</style>"))

'Deep_Learning' 카테고리의 다른 글

15.RNN_mnist  (1) 2018.12.18
14.gan  (0) 2018.12.16
12.mnist_cnn  (0) 2018.12.12
11.mnist_matplotlib_dropout_tensorgraph  (0) 2018.12.10
10.mnist_dropout  (0) 2018.12.10

+ Recent posts