一文了解如何在Python中使用自動(dòng)編碼器
介紹自動(dòng)編碼器實(shí)際上是一種人工神經(jīng)網(wǎng)絡(luò),用于對(duì)以無(wú)監(jiān)督方式提供的輸入數(shù)據(jù)進(jìn)行解壓縮和壓縮。解壓縮和壓縮操作是有損的且特定于數(shù)據(jù)的。數(shù)據(jù)特定意味著自動(dòng)編碼器將只能實(shí)際壓縮已經(jīng)訓(xùn)練過(guò)的數(shù)據(jù)。例如,如果你用狗的圖像訓(xùn)練一個(gè)自動(dòng)編碼器,那么它會(huì)給貓帶來(lái)糟糕的表現(xiàn)。自動(dòng)編碼器計(jì)劃學(xué)習(xí)表示對(duì)整個(gè)數(shù)據(jù)集的編碼。這可能會(huì)導(dǎo)致訓(xùn)練網(wǎng)絡(luò)降低維數(shù)。重構(gòu)部分也是通過(guò)這個(gè)學(xué)習(xí)的。
有損操作意味著重建圖像的質(zhì)量通常不如原始圖像清晰或高分辨率,并且對(duì)于具有更大損失的重建,差異更大,這被稱(chēng)為有損操作。下圖顯示了如何使用特定損失因子對(duì)圖像進(jìn)行編碼和解碼。
自動(dòng)編碼器是一種特殊類(lèi)型的前饋神經(jīng)網(wǎng)絡(luò),輸入應(yīng)該與輸出相似。因此,我們需要一種編碼方法、損失函數(shù)和解碼方法。最終目標(biāo)是以最小的損失完美地復(fù)制輸入。輸入將通過(guò)一層編碼器,它實(shí)際上是一個(gè)完全連接的神經(jīng)網(wǎng)絡(luò),它也構(gòu)成代碼解碼器,因此像 ANN 一樣使用相同的代碼進(jìn)行編碼和解碼。代碼實(shí)現(xiàn)通過(guò)反向傳播訓(xùn)練的 ANN 的工作方式與自動(dòng)編碼器相同。在本文中,我們將討論 3 種類(lèi)型的自動(dòng)編碼器,如下所示:簡(jiǎn)單的自動(dòng)編碼器深度 CNN 自動(dòng)編碼器去噪自動(dòng)編碼器對(duì)于自動(dòng)編碼器的實(shí)現(xiàn)部分,我們將使用流行的 MNIST 數(shù)字?jǐn)?shù)據(jù)集。
1. 簡(jiǎn)單的自動(dòng)編碼器我們首先導(dǎo)入所有必要的庫(kù):import all the dependencies
from keras.layers import Dense,Conv2D,MaxPooling2D,UpSampling2D
from keras import Input, Model
from keras.datasets import mnist
import numpy as np
import matplotlib.pyplot as plt
然后我們將構(gòu)建我們的模型,我們將提供決定輸入將被壓縮多少的維度數(shù)。維度越小,壓縮越大。encoding_dim = 15
input_img = Input(shape=(784,))
# encoded representation of input
encoded = Dense(encoding_dim, activation='relu')(input_img)
# decoded representation of code
decoded = Dense(784, activation='sigmoid')(encoded)
# Model which take input image and shows decoded images
autoencoder = Model(input_img, decoded)
然后我們需要分別構(gòu)建編碼器模型和解碼器模型,以便我們可以輕松區(qū)分輸入和輸出。# This model shows encoded images
encoder = Model(input_img, encoded)
# Creating a decoder model
encoded_input = Input(shape=(encoding_dim,))
# last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]
# decoder model
decoder = Model(encoded_input, decoder_layer(encoded_input))
然后我們需要用ADAM優(yōu)化器和交叉熵?fù)p失函數(shù)擬合來(lái)編譯模型。autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
然后你需要加載數(shù)據(jù):(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.a(chǎn)stype('float32') / 255.
x_test = x_test.a(chǎn)stype('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:])))
print(x_train.shape)
print(x_test.shape)
輸出 :(60000, 784)
(10000, 784)
如果你想查看數(shù)據(jù)的實(shí)際情況,可以使用以下代碼行:plt.imshow(x_train[0].reshape(28,28))
輸出 :
然后你需要訓(xùn)練你的模型:autoencoder.fit(x_train, x_train,
epochs=15,
batch_size=256,
validation_data=(x_test, x_test))
輸出 :Epoch 1/15
235/235 [==============================] - 14s 5ms/step - loss: 0.4200 - val_loss: 0.2263
Epoch 2/15
235/235 [==============================] - 1s 3ms/step - loss: 0.2129 - val_loss: 0.1830
Epoch 3/15
235/235 [==============================] - 1s 3ms/step - loss: 0.1799 - val_loss: 0.1656
Epoch 4/15
235/235 [==============================] - 1s 3ms/step - loss: 0.1632 - val_loss: 0.1537
Epoch 5/15
235/235 [==============================] - 1s 3ms/step - loss: 0.1533 - val_loss: 0.1481
Epoch 6/15
235/235 [==============================] - 1s 3ms/step - loss: 0.1488 - val_loss: 0.1447
Epoch 7/15
235/235 [==============================] - 1s 3ms/step - loss: 0.1457 - val_loss: 0.1424
Epoch 8/15
235/235 [==============================] - 1s 3ms/step - loss: 0.1434 - val_loss: 0.1405
Epoch 9/15
235/235 [==============================] - 1s 3ms/step - loss: 0.1415 - val_loss: 0.1388
Epoch 10/15
235/235 [==============================] - 1s 3ms/step - loss: 0.1398 - val_loss: 0.1374
Epoch 11/15
235/235 [==============================] - 1s 3ms/step - loss: 0.1386 - val_loss: 0.1360
Epoch 12/15
235/235 [==============================] - 1s 3ms/step - loss: 0.1373 - val_loss: 0.1350
Epoch 13/15
235/235 [==============================] - 1s 3ms/step - loss: 0.1362 - val_loss: 0.1341
Epoch 14/15
235/235 [==============================] - 1s 3ms/step - loss: 0.1355 - val_loss: 0.1334
Epoch 15/15
235/235 [==============================] - 1s 3ms/step - loss: 0.1348 - val_loss: 0.1328
訓(xùn)練后,你需要提供輸入,你可以使用以下代碼繪制結(jié)果:encoded_img = encoder.predict(x_test)
decoded_img = decoder.predict(encoded_img)
plt.figure(figsize=(20, 4))
for i in range(5):
# Display original
ax = plt.subplot(2, 5, i + 1)
plt.imshow(x_test[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# Display reconstruction
ax = plt.subplot(2, 5, i + 1 + 5)
plt.imshow(decoded_img[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
你可以分別清楚地看到編碼和解碼圖像的輸出,如下所示。
2. 深度 CNN 自動(dòng)編碼器由于這里的輸入是圖像,因此使用卷積神經(jīng)網(wǎng)絡(luò)或 CNN 確實(shí)更有意義。編碼器將由一堆 Conv2D 和最大池化層組成,解碼器將由一堆 Conv2D 和上采樣層組成。代碼 :model = Sequential()
# encoder network
model.a(chǎn)dd(Conv2D(30, 3, activation= 'relu', padding='same', input_shape = (28,28,1)))
model.a(chǎn)dd(MaxPooling2D(2, padding= 'same'))
model.a(chǎn)dd(Conv2D(15, 3, activation= 'relu', padding='same'))
model.a(chǎn)dd(MaxPooling2D(2, padding= 'same'))
#decoder network
model.a(chǎn)dd(Conv2D(15, 3, activation= 'relu', padding='same'))
model.a(chǎn)dd(UpSampling2D(2))
model.a(chǎn)dd(Conv2D(30, 3, activation= 'relu', padding='same'))
model.a(chǎn)dd(UpSampling2D(2))
model.a(chǎn)dd(Conv2D(1,3,activation='sigmoid', padding= 'same')) # output layer
model.compile(optimizer= 'adam', loss = 'binary_crossentropy')
model.summary()
輸出 :Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d (Conv2D) (None, 28, 28, 30) 300
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 14, 14, 30) 0
_________________________________________________________________
conv2d_1 (Conv2D) (None, 14, 14, 15) 4065
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 7, 7, 15) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 7, 7, 15) 2040
_________________________________________________________________
up_sampling2d (UpSampling2D) (None, 14, 14, 15) 0
_________________________________________________________________
conv2d_3 (Conv2D) (None, 14, 14, 30) 4080
_________________________________________________________________
up_sampling2d_1 (UpSampling2 (None, 28, 28, 30) 0
_________________________________________________________________
conv2d_4 (Conv2D) (None, 28, 28, 1) 271
=================================================================
Total params: 10,756
Trainable params: 10,756
Non-trainable params: 0
_________________________________________________________________
現(xiàn)在你需要加載數(shù)據(jù)并訓(xùn)練模型(x_train, _), (x_test, _) = mnist.load_data()
x_train = x_train.a(chǎn)stype('float32') / 255.
x_test = x_test.a(chǎn)stype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1))
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1))
model.fit(x_train, x_train,
epochs=15,
batch_size=128,
validation_data=(x_test, x_test))
輸出 :Epoch 1/15
469/469 [==============================] - 34s 8ms/step - loss: 0.2310 - val_loss: 0.0818
Epoch 2/15
469/469 [==============================] - 3s 7ms/step - loss: 0.0811 - val_loss: 0.0764
Epoch 3/15
469/469 [==============================] - 3s 7ms/step - loss: 0.0764 - val_loss: 0.0739
Epoch 4/15
469/469 [==============================] - 3s 7ms/step - loss: 0.0743 - val_loss: 0.0725
Epoch 5/15
469/469 [==============================] - 3s 7ms/step - loss: 0.0729 - val_loss: 0.0718
Epoch 6/15
469/469 [==============================] - 3s 7ms/step - loss: 0.0722 - val_loss: 0.0709
Epoch 7/15
469/469 [==============================] - 3s 7ms/step - loss: 0.0715 - val_loss: 0.0703
Epoch 8/15
469/469 [==============================] - 3s 7ms/step - loss: 0.0709 - val_loss: 0.0698
Epoch 9/15
469/469 [==============================] - 3s 7ms/step - loss: 0.0700 - val_loss: 0.0693
Epoch 10/15
469/469 [==============================] - 3s 7ms/step - loss: 0.0698 - val_loss: 0.0689
Epoch 11/15
469/469 [==============================] - 3s 7ms/step - loss: 0.0694 - val_loss: 0.0687
Epoch 12/15
469/469 [==============================] - 3s 7ms/step - loss: 0.0691 - val_loss: 0.0684
Epoch 13/15
469/469 [==============================] - 3s 7ms/step - loss: 0.0688 - val_loss: 0.0680
Epoch 14/15
469/469 [==============================] - 3s 7ms/step - loss: 0.0685 - val_loss: 0.0680
Epoch 15/15
469/469 [==============================] - 3s 7ms/step - loss: 0.0683 - val_loss: 0.0676
現(xiàn)在你需要提供輸入并繪制以下結(jié)果的輸出pred = model.predict(x_test)
plt.figure(figsize=(20, 4))
for i in range(5):
# Display original
ax = plt.subplot(2, 5, i + 1)
plt.imshow(x_test[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# Display reconstruction
ax = plt.subplot(2, 5, i + 1 + 5)
plt.imshow(pred[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
輸出 :
3. 去噪自動(dòng)編碼器現(xiàn)在我們將看到模型如何處理圖像中的噪聲。我們所說(shuō)的噪聲是指模糊的圖像、改變圖像的顏色,甚至是圖像上的白色標(biāo)記。noise_factor = 0.7
x_train_noisy = x_train + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_train.shape)
x_test_noisy = x_test + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_test.shape)
x_train_noisy = np.clip(x_train_noisy, 0., 1.)
x_test_noisy = np.clip(x_test_noisy, 0., 1.)
Here is how the noisy images look right now.
plt.figure(figsize=(20, 2))
for i in range(1, 5 + 1):
ax = plt.subplot(1, 5, i)
plt.imshow(x_test_noisy[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
輸出 :
現(xiàn)在圖像幾乎無(wú)法識(shí)別,為了增加自動(dòng)編碼器的范圍,我們將修改定義模型的層以增加過(guò)濾器,使模型性能更好,然后擬合模型。model = Sequential()
# encoder network
model.a(chǎn)dd(Conv2D(35, 3, activation= 'relu', padding='same', input_shape = (28,28,1)))
model.a(chǎn)dd(MaxPooling2D(2, padding= 'same'))
model.a(chǎn)dd(Conv2D(25, 3, activation= 'relu', padding='same'))
model.a(chǎn)dd(MaxPooling2D(2, padding= 'same'))
#decoder network
model.a(chǎn)dd(Conv2D(25, 3, activation= 'relu', padding='same'))
model.a(chǎn)dd(UpSampling2D(2))
model.a(chǎn)dd(Conv2D(35, 3, activation= 'relu', padding='same'))
model.a(chǎn)dd(UpSampling2D(2))
model.a(chǎn)dd(Conv2D(1,3,activation='sigmoid', padding= 'same')) # output layer
model.compile(optimizer= 'adam', loss = 'binary_crossentropy')
model.fit(x_train_noisy, x_train,
epochs=15,
batch_size=128,
validation_data=(x_test_noisy, x_test))
輸出 :Epoch 1/15
469/469 [==============================] - 5s 9ms/step - loss: 0.2643 - val_loss: 0.1456
Epoch 2/15
469/469 [==============================] - 4s 8ms/step - loss: 0.1440 - val_loss: 0.1378
Epoch 3/15
469/469 [==============================] - 4s 8ms/step - loss: 0.1373 - val_loss: 0.1329
Epoch 4/15
469/469 [==============================] - 4s 8ms/step - loss: 0.1336 - val_loss: 0.1305
Epoch 5/15
469/469 [==============================] - 4s 8ms/step - loss: 0.1313 - val_loss: 0.1283
Epoch 6/15
469/469 [==============================] - 4s 8ms/step - loss: 0.1294 - val_loss: 0.1268
Epoch 7/15
469/469 [==============================] - 4s 8ms/step - loss: 0.1278 - val_loss: 0.1257
Epoch 8/15
469/469 [==============================] - 4s 8ms/step - loss: 0.1267 - val_loss: 0.1251
Epoch 9/15
469/469 [==============================] - 4s 8ms/step - loss: 0.1259 - val_loss: 0.1244
Epoch 10/15
469/469 [==============================] - 4s 8ms/step - loss: 0.1251 - val_loss: 0.1234
Epoch 11/15
469/469 [==============================] - 4s 8ms/step - loss: 0.1241 - val_loss: 0.1234
Epoch 12/15
469/469 [==============================] - 4s 8ms/step - loss: 0.1239 - val_loss: 0.1222
Epoch 13/15
469/469 [==============================] - 4s 8ms/step - loss: 0.1232 - val_loss: 0.1223
Epoch 14/15
469/469 [==============================] - 4s 8ms/step - loss: 0.1226 - val_loss: 0.1215
Epoch 15/15
469/469 [==============================] - 4s 8ms/step - loss: 0.1221 - val_loss: 0.1211
訓(xùn)練結(jié)束后,我們將提供輸入并編寫(xiě)繪圖函數(shù)以查看最終結(jié)果。pred = model.predict(x_test_noisy)
plt.figure(figsize=(20, 4))
for i in range(5):
# Display original
ax = plt.subplot(2, 5, i + 1)
plt.imshow(x_test_noisy[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
# Display reconstruction
ax = plt.subplot(2, 5, i + 1 + 5)
plt.imshow(pred[i].reshape(28, 28))
plt.gray()
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
plt.show()
輸出 :
尾注我們已經(jīng)了解了自動(dòng)編碼器工作的結(jié)構(gòu),并使用了 3 種類(lèi)型的自動(dòng)編碼器。自動(dòng)編碼器有多種用途,如降維圖像壓縮、電影和歌曲推薦系統(tǒng)等。模型的性能可以通過(guò)訓(xùn)練更多的 epoch 或增加我們網(wǎng)絡(luò)的維度來(lái)提高。
發(fā)表評(píng)論
請(qǐng)輸入評(píng)論內(nèi)容...
請(qǐng)輸入評(píng)論/評(píng)論長(zhǎng)度6~500個(gè)字
最新活動(dòng)更多
-
即日-11.13立即報(bào)名>>> 【在線(xiàn)會(huì)議】多物理場(chǎng)仿真助跑新能源汽車(chē)
-
11月28日立即報(bào)名>>> 2024工程師系列—工業(yè)電子技術(shù)在線(xiàn)會(huì)議
-
12月19日立即報(bào)名>> 【線(xiàn)下會(huì)議】OFweek 2024(第九屆)物聯(lián)網(wǎng)產(chǎn)業(yè)大會(huì)
-
即日-12.26火熱報(bào)名中>> OFweek2024中國(guó)智造CIO在線(xiàn)峰會(huì)
-
即日-2025.8.1立即下載>> 《2024智能制造產(chǎn)業(yè)高端化、智能化、綠色化發(fā)展藍(lán)皮書(shū)》
-
精彩回顧立即查看>> 【限時(shí)免費(fèi)下載】TE暖通空調(diào)系統(tǒng)高效可靠的組件解決方案
推薦專(zhuān)題
- 1 【一周車(chē)話(huà)】沒(méi)有方向盤(pán)和踏板的車(chē),你敢坐嗎?
- 2 特斯拉發(fā)布無(wú)人駕駛車(chē),還未迎來(lái)“Chatgpt時(shí)刻”
- 3 特斯拉股價(jià)大跌15%:Robotaxi離落地還差一個(gè)蘿卜快跑
- 4 馬斯克給的“驚喜”夠嗎?
- 5 打完“價(jià)格戰(zhàn)”,大模型還要比什么?
- 6 馬斯克致敬“國(guó)產(chǎn)蘿卜”?
- 7 神經(jīng)網(wǎng)絡(luò),誰(shuí)是盈利最強(qiáng)企業(yè)?
- 8 比蘋(píng)果偉大100倍!真正改寫(xiě)人類(lèi)歷史的智能產(chǎn)品降臨
- 9 諾獎(jiǎng)進(jìn)入“AI時(shí)代”,人類(lèi)何去何從?
- 10 Open AI融資后成萬(wàn)億獨(dú)角獸,AI人才之爭(zhēng)開(kāi)啟
- 高級(jí)軟件工程師 廣東省/深圳市
- 自動(dòng)化高級(jí)工程師 廣東省/深圳市
- 光器件研發(fā)工程師 福建省/福州市
- 銷(xiāo)售總監(jiān)(光器件) 北京市/海淀區(qū)
- 激光器高級(jí)銷(xiāo)售經(jīng)理 上海市/虹口區(qū)
- 光器件物理工程師 北京市/海淀區(qū)
- 激光研發(fā)工程師 北京市/昌平區(qū)
- 技術(shù)專(zhuān)家 廣東省/江門(mén)市
- 封裝工程師 北京市/海淀區(qū)
- 結(jié)構(gòu)工程師 廣東省/深圳市