# library import

from keras.datasets import mnist

from keras.layers import Input, Dense

from keras.models import Model # <=기존 모델의 인풋 값과 내 모델의 아웃풋 값을 전달

import numpy as np

import pandas as pd

import matplotlib.pyplot as plt

%matplotlib inline

==========================

#data load

(X_train, _), (X_test, _) = mnist.load_data()

#chage types as a real value 

X_train = X_train.astype('float32')/255

X_test = X_test.astype('float32')/255

#reshape the data from (60000, 28,28) , (10000, 28,28)to (60000, 784), (10000, 784)  

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:]))

print(X_train.shape)

print(X_test.shape)

 

#set input layer

input_img= Input(shape=(784,))

#===================encoder================================#

encoded = Dense(units=128, activation='relu')(input_img)

encoded = Dense(units=64, activation='relu')(encoded) ###

encoded = Dense(units=32, activation='relu')(encoded)

#===================decoder================================#

decoded = Dense(units=64, activation='relu')(encoded)

decoded = Dense(units=128, activation='relu')(decoded)

decoded = Dense(units=784, activation='sigmoid')(decoded)

Input layer, Enc. Dec. Network 개념도

중간에 네트워크 짜는 개념은 알고 가야 할 것 같아서 설명을 넣는다. 

- 상기와 같이 초기에 전처리한 Input layer를 넣고 그 다음에 차차 압축해 가면서 Encoder를 넣는다. 

- 그리고 그 다음에 압축된 데이터를 복원하기 위해 Latent space(여기서는 32)로부터 차차 다시 올라가도록 한다. 

- 그리고 이러헤 복원시키는 이미지를 사용하는 오토인코더를 만든다. (최종모델)

#####################################

autoencoder=Model(input_img, decoded) # decode를 거쳐 복원하는 최종 모델 (auto encoder)???????(확인 필요) Model의 의미

autoencoder.compile(optimizer='adam', loss='binary_crossentropy',metrics=['accuracy'])

autoencoder.fit(X_train, X_train,

                epochs=50,

                batch_size=256,

                shuffle=True,

                validation_data=(X_test, X_test))

#####################################

encoder = Model(input_img, encoded) # encode를 거쳐 lated space로 압축하는 모델(확인 필요)

다음은 실수로 오토인코딩 하는 코드 살펴 보는 것으로 하는 것으로

 

+ Recent posts