百度付費(fèi)推廣圖片seo優(yōu)化是什么意思
一、說明
????????自動編碼器是一種無監(jiān)督學(xué)習(xí)的神經(jīng)網(wǎng)絡(luò)模型,主要用于降維或特征提取。常見的自動編碼器包括基本的單層自動編碼器、深度自動編碼器、卷積自動編碼器和變分自動編碼器等。
????????其中,基本的單層自動編碼器由一個(gè)編碼器和一個(gè)解碼器組成,編碼器將輸入數(shù)據(jù)壓縮成低維數(shù)據(jù),解碼器將低維數(shù)據(jù)還原成原始數(shù)據(jù)。深度自動編碼器是在單層自動編碼器的基礎(chǔ)上增加了多個(gè)隱藏層,可以實(shí)現(xiàn)更復(fù)雜的特征提取。卷積自動編碼器則是針對圖像等數(shù)據(jù)特征提取的一種自動編碼器,它使用卷積神經(jīng)網(wǎng)絡(luò)進(jìn)行特征提取和重建。變分自動編碼器則是一種生成式模型,可以用于生成新的數(shù)據(jù)樣本。
????????總的來說,不同類型的自動編碼器適用于不同類型的數(shù)據(jù)和問題,選擇合適的自動編碼器可以提高模型的性能。
二、在Minist數(shù)據(jù)集實(shí)現(xiàn)自動編碼器
2.1 概述
????????本文中的代碼用于在 MNIST 數(shù)據(jù)集上訓(xùn)練自動編碼器。自動編碼器是一種旨在重建其輸入的神經(jīng)網(wǎng)絡(luò)。在此腳本中,自動編碼器由兩個(gè)較小的網(wǎng)絡(luò)組成:編碼器和解碼器。編碼器獲取輸入圖像,將其壓縮為 64 個(gè)特征,并將編碼表示傳遞給解碼器,然后解碼器重建輸入圖像。自動編碼器通過最小化重建圖像和原始圖像之間的均方誤差來訓(xùn)練。該腳本首先加載 MNIST 數(shù)據(jù)集并規(guī)范化像素值。然后,它將圖像重塑為一維表示,以便可以將其輸入神經(jīng)網(wǎng)絡(luò)。之后,使用tensorflow.keras庫中的輸入層和密集層創(chuàng)建編碼器和解碼器模型。自動編碼器模型是通過鏈接編碼器和解碼器模型創(chuàng)建的。然后使用亞當(dāng)優(yōu)化器和均方誤差損失函數(shù)編譯自動編碼器。最后,自動編碼器在歸一化和重塑的MNIST圖像上訓(xùn)練25個(gè)epoch。通過繪制訓(xùn)練集和測試集在 epoch 上的損失來監(jiān)控訓(xùn)練進(jìn)度。訓(xùn)練后,腳本繪制一些測試圖像及其相應(yīng)的重建。此外,還計(jì)算了原始圖像和重建圖像之間的均方誤差和結(jié)構(gòu)相似性指數(shù)(SSIM)。
????????下圖顯示了模型的良好擬合,可以看到模型的良好擬合。

????????該代碼比較兩個(gè)圖像,一個(gè)來自測試集的原始圖像和一個(gè)由自動編碼器生成的預(yù)測圖像。它使用該函數(shù)計(jì)算兩個(gè)圖像之間的均方誤差 (MSE),并使用 scikit-image 庫中的函數(shù)計(jì)算兩個(gè)圖像之間的結(jié)構(gòu)相似性指數(shù) (SSIM)。根據(jù) mse 和 ssim 代碼檢索test_labels以打印測試圖像的值。mse
ssim
2.2 代碼實(shí)現(xiàn)
import numpy as np
import tensorflow
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras.models import Model
from tensorflow.keras.datasets import mnist
from tensorflow.keras.layers import Input, Dense, Flatten
from tensorflow.keras.layers import Layer
from skimage import metrics
## import os can be skipped if there is nocompatibility issue
## with the OpenMP library and TensorFlow
import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"# Load the MNIST dataset
(x_train, train_labels), (x_test, test_labels) = mnist.load_data()# Normalize the data
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.# Flatten the images
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:])))# Randomize both the training and test
permutation = np.random.permutation(len(x_train))
x_train, train_labels = x_train[permutation], train_labels[permutation]
permutation = np.random.permutation(len(x_test))
x_test, test_labels = x_test[permutation], test_labels[permutation]
# Create the encoderlist_xtest = [ [x_test[i], test_labels[i]] for i in test_labels]
print(len(list_xtest)) encoder_input = Input(shape=(784,))
encoded = Dense(64, activation='relu')(encoder_input)
encoder = Model(encoder_input, encoded)# Create the decoder
decoder_input = Input(shape=(64,))
decoded = Dense(784, activation='sigmoid')(decoder_input)
decoder = Model(decoder_input, decoded)# Create the autoencoder
autoencoder = Model(encoder_input, decoder(encoder(encoder_input)))lr_schedule = tensorflow.keras.optimizers.schedules.ExponentialDecay(initial_learning_rate = 5e-01, decay_steps = 2500, decay_rate = 0.75,staircase=True)
tensorflow.keras.optimizers.Adam(learning_rate = lr_schedule,beta_1=0.95,beta_2=0.99,epsilon=1e-01)
autoencoder.compile(optimizer='adam', loss='mean_squared_error')# Train the autoencoder
history = autoencoder.fit(x_train, x_train,epochs=25,batch_size=512,shuffle=True,validation_data=(x_test, x_test))# Plot the training history
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper right')
plt.show()# Plot the test figures vs. predicted figures
decoded_imgs = autoencoder.predict(x_test)def mse(imageA, imageB):err = np.sum((imageA.astype("float") - imageB.astype("float")) ** 2)err /= float(imageA.shape[0])return errdef ssim(imageA, imageB):return metrics.structural_similarity(imageA, imageB,channel_axis=None)decomser = []
decossimr = []
n = 10
list_xtestn = [ [x_test[i], test_labels[i]] for i in range(10)]
print([list_xtestn[i][1] for i in range(n)])
plt.figure(figsize=(20, 4))
for i in range(n):# Display originalax = plt.subplot(2, n, 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 reconstructionax = plt.subplot(2, n, i + 1 + n)plt.imshow(decoded_imgs[i].reshape(28, 28))plt.gray()ax.get_xaxis().set_visible(False)ax.get_yaxis().set_visible(False)if mse(list_xtestn[i][0],decoded_imgs[i]) <= 0.01: msel = mse(list_xtestn[i][0],decoded_imgs[i])decomser.append(list_xtestn[i][1]) if ssim(list_xtestn[i][0],decoded_imgs[i]) > 0.85:ssiml = ssim(list_xtestn[i][0],decoded_imgs[i])decossimr.append(list_xtestn[i][1]) print("mse and ssim for image %s are %s and %s" %(i,msel,ssiml))
plt.show() print(decomser)
print(decossimr)
三、實(shí)驗(yàn)的部分結(jié)果示例?
????????該模型可以預(yù)測手寫數(shù)據(jù),如下所示。

????????此外,使用MSE和ssim方法將預(yù)測圖像與測試圖像進(jìn)行比較,可以訪問test_labels并打印預(yù)測數(shù)據(jù)。

????????此代碼演示如何使用自動編碼器通過圖像比較教程來訓(xùn)練和建立手寫識別網(wǎng)絡(luò)。一開始,訓(xùn)練和測試圖像是隨機(jī)的,因此每次運(yùn)行的圖像集都不同。
????????在另一篇文章中,我們將展示如何使用 Padé 近似值作為自動編碼器 (link.medium.com/cqiP5bd9ixb)?的激活函數(shù)。
引用:
- 原始的MNIST數(shù)據(jù)集:LeCun,Y.,Cortes,C.和Burges,C.J.(2010)。MNIST手寫數(shù)字?jǐn)?shù)據(jù)庫。AT&T 實(shí)驗(yàn)室 [在線]。可用:?http://yann。萊昆。com/exdb/mnist/
- 自動編碼器概念和應(yīng)用:Hinton,G.E.和Salakhutdinov,R.R.(2006)。使用神經(jīng)網(wǎng)絡(luò)降低數(shù)據(jù)的維數(shù)??茖W(xué), 313(5786), 504–507.
- 使用自動編碼器進(jìn)行圖像重建:Masci,J.,Meier,U.,Cire?an,D.和Schmidhuber,J.(2011年52月)。用于分層特征提取的堆疊卷積自動編碼器。在人工神經(jīng)網(wǎng)絡(luò)國際會議(第 59-<> 頁)中。施普林格,柏林,海德堡。
- The tensorflow.keras library: Chollet, F. (2018).使用 Python 進(jìn)行深度學(xué)習(xí)。紐約州謝爾特島:曼寧出版公司
- 均方誤差損失函數(shù)和亞當(dāng)優(yōu)化器:Kingma,D.P.和Ba,J.(2014)。Adam:一種隨機(jī)優(yōu)化的方法。arXiv預(yù)印本arXiv:1412.6980。
- 結(jié)構(gòu)相似性指數(shù)(SSIM):Wang,Z.,Bovik,A.C.,Sheikh,H.R.和Simoncelli,E.P.(2004)。圖像質(zhì)量評估:從錯(cuò)誤可見性到結(jié)構(gòu)相似性。IEEE圖像處理事務(wù),13(4),600-612。
-
弗朗西斯·貝尼斯坦特
·