kers에서 autoencorder

1901 단어 Keras2.0Autoencoder

개요


kers로 autoencorder를 만들어 보았습니다.

컨디션


windows 7 sp1 64bit
anaconda3
tensorflow 1.2
keras2.0

사진.



실행

Epoch 10/10


59136/60000 [============================>.] - ETA: 0s - loss: 0.1711
59904/60000 [============================>.] - ETA: 0s - loss: 0.1710
60000/60000 [==============================] - 4s - loss: 0.1710 - val_loss: 0.1672

샘플 코드

from tensorflow.contrib.keras.python.keras.layers import Input, Dense
from tensorflow.contrib.keras.python.keras.models import Model
from tensorflow.contrib.keras.python.keras.datasets import mnist
import numpy as np
import matplotlib.pyplot as plt

(x_train, _), (x_test, _) = mnist.load_data()
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1 : ])))
x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1 : ])))

input_img = Input(shape = (784, ))
encoded = Dense(32, activation = 'relu')(input_img)
decoded = Dense(784, activation = 'sigmoid')(encoded)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer = 'adadelta', loss = 'binary_crossentropy')
autoencoder.fit(x_train, x_train, epochs = 10, batch_size = 256, shuffle = True, validation_data = (x_test, x_test))

decoded_imgs = autoencoder.predict(x_test)
n = 10
plt.figure(figsize = (20, 4))
for i in range(n):
    ax = plt.subplot(2, n, i + 1)
    plt.imshow(x_test[i].reshape(28, 28))
    ax = plt.subplot(2, n, i + 1 + n)
    plt.imshow(decoded_imgs[i].reshape(28, 28))
plt.savefig("auto2.png")
plt.show()
이상.

좋은 웹페이지 즐겨찾기