1225
[17주차 - Day3] Recommendation system
케라스 오토인코더 실습
# 케라스 이용한 오토인코더 실습
import numpy as np
import matplotlib.pyplot as plt
from keras.layers import Input, Dense
from keras.models import Model
from keras.datasets import mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.astype('float32') / 256
x_test = x_test.astype('float32') / 256
print(x_train.shape)
x_train = x_train.reshape(len(x_train), 28*28)
x_test = x_test.reshape(len(x_test), 28*28)
print(x_train.shape)
print(x_test.shape)
input_layer = Input(shape=(784,))
encoded = Dense(64, activation='relu')(input_layer)
decoded = Dense(784, activation='sigmoid')(encoded)
autoencoder = Model(input_layer, decoded)
encoder = Model(input_layer, encoded)
input_layer_decoder = Input(shape=(64,))
decoder_layer = autoencoder.layers[-1](input_layer_decoder)
decoder = Model(input_layer_decoder, decoder_layer)
autoencoder.summary()
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
autoencoder.fit(x_train, x_train, epochs=50, batch_size=256, shuffle=True, validation_data=(x_test, x_test))
def visualize(data, size):
n = 8
plt.figure(figsize=(20,4))
plt.gray()
for i in range(n):
ax = plt.subplot(2, n, i+1)
plt.imshow(data[i].reshape(size, size))
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
visualize(x_test, 28)
visualize(encoded_images, 8)
visualize(decoded_images, 28)
'프로그래머스 데브 코스 > TIL' 카테고리의 다른 글
[6기] 프로그래머스 인공지능 데브코스 118일차 TIL (0) | 2023.12.27 |
---|---|
[6기] 프로그래머스 인공지능 데브코스 117일차 TIL (1) | 2023.12.26 |
[6기] 프로그래머스 인공지능 데브코스 115일차 TIL (0) | 2023.12.24 |
[6기] 프로그래머스 인공지능 데브코스 114일차 TIL (1) | 2023.12.23 |
[6기] 프로그래머스 인공지능 데브코스 113일차 TIL (0) | 2023.12.22 |