keras_Autoencoder 自編碼

自編碼,簡(jiǎn)單來說就是把輸入數(shù)據(jù)進(jìn)行一個(gè)壓縮和解壓縮的過程。 原來有很多 Feature,壓縮成幾個(gè)來代表原來的數(shù)據(jù),解壓之后恢復(fù)成原來的維度,再和原數(shù)據(jù)進(jìn)行比較。
它是一種非監(jiān)督算法,只需要輸入數(shù)據(jù),解壓縮之后的結(jié)果與原數(shù)據(jù)本身進(jìn)行比較。
今天要做的事情是把 datasets.mnist 數(shù)據(jù)的 28×28=784 維的數(shù)據(jù),壓縮成 2 維的數(shù)據(jù),然后在一個(gè)二維空間中可視化出分類的效果。
用keras構(gòu)建自編碼器的步驟:
1.導(dǎo)入模塊并創(chuàng)建數(shù)據(jù)
2.建立模型
3.激活模型
4.訓(xùn)練模型
5.可視化結(jié)果


Demo.py

import numpy as np
np.random.seed(1337)  # for reproducibility
from keras.datasets import mnist
from keras.models import Model
from keras.layers import Dense, Input
import matplotlib.pyplot as plt
from keras.optimizers import Adam
#創(chuàng)建數(shù)據(jù)
# download the mnist to the path '~/.keras/datasets/' if it is the first time to be called
# X shape (60,000 28x28), y shape (10,000, )
(x_train, _), (x_test, y_test) = mnist.load_data()

# data pre-processing
x_train = x_train.astype('float32') / 255. - 0.5       # minmax_normalized
x_test = x_test.astype('float32') / 255. - 0.5         # minmax_normalized
x_train = x_train.reshape((x_train.shape[0], -1))
x_test = x_test.reshape((x_test.shape[0], -1))
print(x_train.shape)
print(x_test.shape)

#建立模型
# in order to plot in a 2D figure
encoding_dim = 2   #encoding_dim,要壓縮成的維度。

# this is our input placeholder
input_img = Input(shape=(784,))

# encoder layers
encoded = Dense(128, activation='relu')(input_img)
encoded = Dense(64, activation='relu')(encoded)
encoded = Dense(10, activation='relu')(encoded)
encoder_output = Dense(encoding_dim)(encoded)

# decoder layers
decoded = Dense(10, activation='relu')(encoder_output)
decoded = Dense(64, activation='relu')(decoded)
decoded = Dense(128, activation='relu')(decoded)
decoded = Dense(784, activation='tanh')(decoded)

#接下來直接用 Model 這個(gè)模塊來組建模型,輸入就是圖片,輸出是解壓的最后的結(jié)果。
# construct the autoencoder model
autoencoder = Model(input=input_img, output=decoded)
#當(dāng)我們想要看由 784 壓縮到 2維后,這個(gè)結(jié)果是什么樣的時(shí)候,也可以只單獨(dú)組建壓縮的板塊,
#此時(shí)它的輸入是圖片,輸出是壓縮環(huán)節(jié)的最后結(jié)果。
# construct the encoder model for plotting
encoder = Model(input=input_img, output=encoder_output)

#激活模型
#接下來是編譯自編碼這個(gè)模型,優(yōu)化器用的是 adam,損失函數(shù)用的是 mse。
# compile autoencoder
adam = Adam(lr = 1e-4)
autoencoder.compile(optimizer='adam', loss='mse')
#訓(xùn)練模型
# training
autoencoder.fit(x_train, x_train,
                nb_epoch=20,
                batch_size=256,
                shuffle=True)
#可視化結(jié)果
#最后看到可視化的結(jié)果,自編碼模型可以把這幾個(gè)數(shù)字給區(qū)分開來,
#我們可以用自編碼這個(gè)過程來作為一個(gè)特征壓縮的方法,
#和PCA的功能一樣,效果要比它好一些,因?yàn)樗欠蔷€性的結(jié)構(gòu)
# plotting
encoded_imgs = encoder.predict(x_test)
plt.scatter(encoded_imgs[:, 0], encoded_imgs[:, 1], c=y_test)
plt.colorbar()
plt.show()


結(jié)果:

....
Epoch 18/20
60000/60000 [==============================] - 6s - loss: 0.0411     
Epoch 19/20
60000/60000 [==============================] - 6s - loss: 0.0407     
Epoch 20/20
60000/60000 [==============================] - 6s - loss: 0.0405      
Paste_Image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容