自編碼器是開(kāi)發(fā)無(wú)監(jiān)督學(xué)習(xí)模型的主要方式之一。但什么是自動(dòng)編碼器?
簡(jiǎn)而言之,自動(dòng)編碼器通過(guò)接收數(shù)據(jù)、壓縮和編碼數(shù)據(jù),然后從編碼表示中重構(gòu)數(shù)據(jù)來(lái)進(jìn)行操作。對(duì)模型進(jìn)行訓(xùn)練,直到損失最小化并且盡可能接近地再現(xiàn)數(shù)據(jù)。通過(guò)這個(gè)過(guò)程,自動(dòng)編碼器可以學(xué)習(xí)數(shù)據(jù)的重要特征。
自動(dòng)編碼器是由多個(gè)層組成的神經(jīng)網(wǎng)絡(luò)。自動(dòng)編碼器的定義方面是輸入層包含與輸出層一樣多的信息。輸入層和輸出層具有完全相同數(shù)量的單元的原因是自動(dòng)編碼器旨在復(fù)制輸入數(shù)據(jù)。然后分析數(shù)據(jù)并以無(wú)監(jiān)督方式重建數(shù)據(jù)后輸出數(shù)據(jù)副本。
通過(guò)自動(dòng)編碼器的數(shù)據(jù)不僅僅是從輸入直接映射到輸出。自動(dòng)編碼器包含三個(gè)組件:壓縮數(shù)據(jù)的編碼(輸入)部分、處理壓縮數(shù)據(jù)(或瓶頸)的組件和解碼器(輸出)部分。當(dāng)數(shù)據(jù)被輸入自動(dòng)編碼器時(shí),它會(huì)被編碼,然后壓縮到更小的尺寸。然后對(duì)網(wǎng)絡(luò)進(jìn)行編碼/壓縮數(shù)據(jù)的訓(xùn)練,并輸出該數(shù)據(jù)的重建。
? 神經(jīng)網(wǎng)絡(luò)學(xué)習(xí)了輸入數(shù)據(jù)的“本質(zhì)”或最重要的特征,這是自動(dòng)編碼器的核心價(jià)值。訓(xùn)練完網(wǎng)絡(luò)后,訓(xùn)練好的模型就可以合成相似的數(shù)據(jù),并添加或減去某些目標(biāo)特征。例如,您可以在加了噪聲的圖像上訓(xùn)練自動(dòng)編碼器,然后使用經(jīng)過(guò)訓(xùn)練的模型從圖像中去除噪聲。
自編碼器的應(yīng)用包括:異常檢測(cè)、數(shù)據(jù)去噪(例如圖像、音頻)、圖像著色、圖像修復(fù)、信息檢索等、降維等。
二、自動(dòng)編碼器的架構(gòu)自動(dòng)編碼器基本上可以分為三個(gè)不同的組件:編碼器、瓶頸和解碼器。
編碼器:編碼器是一個(gè)前饋、全連接的神經(jīng)網(wǎng)絡(luò),它將輸入壓縮為潛在空間表示,并將輸入圖像編碼為降維的壓縮表示。壓縮后的圖像是原始圖像的變形版本。
code:網(wǎng)絡(luò)的這一部分包含輸入解碼器的簡(jiǎn)化表示。
解碼器:解碼器和編碼器一樣也是一個(gè)前饋網(wǎng)絡(luò),結(jié)構(gòu)與編碼器相似。該網(wǎng)絡(luò)負(fù)責(zé)將輸入從代碼中重建回原始維度。
首先,輸入通過(guò)編碼器進(jìn)行壓縮并存儲(chǔ)在稱為code的層中,然后解碼器從代碼中解壓縮原始輸入。自編碼器的主要目標(biāo)是獲得與輸入相同的輸出。
通常情況下解碼器架構(gòu)是編碼器的鏡像,但也不是絕對(duì)的。唯一的要求是輸入和輸出的維度必須相同。
三、自動(dòng)編碼器的類型 1、卷積自動(dòng)編碼器卷積自動(dòng)編碼器是通用的特征提取器。卷積自編碼器是采用卷積層代替全連接層,原理和自編碼器一樣,對(duì)輸入的象征進(jìn)行降采樣以提供較小維度潛在表示,并強(qiáng)制自編碼器學(xué)習(xí)象征的壓縮版本。
這種類型的自動(dòng)編碼器適用于部分損壞的輸入,并訓(xùn)練以恢復(fù)原始未失真的圖像。如上所述,這種方法是限制網(wǎng)絡(luò)簡(jiǎn)單復(fù)制輸入的有效方法。
目標(biāo)是網(wǎng)絡(luò)將能夠復(fù)制圖像的原始版本。通過(guò)將損壞的數(shù)據(jù)與原始數(shù)據(jù)進(jìn)行比較,網(wǎng)絡(luò)可以了解數(shù)據(jù)的哪些特征最重要,哪些特征不重要/損壞。換句話說(shuō),為了讓模型對(duì)損壞的圖像進(jìn)行去噪,它必須提取圖像數(shù)據(jù)的重要特征。
收縮自動(dòng)編碼器的目標(biāo)是降低表示對(duì)訓(xùn)練輸入數(shù)據(jù)的敏感性。 為了實(shí)現(xiàn)這一點(diǎn),在自動(dòng)編碼器試圖最小化的損失函數(shù)中添加一個(gè)正則化項(xiàng)或懲罰項(xiàng)。
收縮自動(dòng)編碼器通常僅作為其他幾種自動(dòng)編碼器節(jié)點(diǎn)存在。去噪自動(dòng)編碼器使重建函數(shù)抵抗輸入的小但有限大小的擾動(dòng),而收縮自動(dòng)編碼器使特征提取函數(shù)抵抗輸入的無(wú)窮小擾動(dòng)。
4、變分自動(dòng)編碼器Variational Autoencoders,這種類型的自動(dòng)編碼器對(duì)潛在變量的分布做出了假設(shè),并在訓(xùn)練過(guò)程中使用了隨機(jī)梯度變分貝葉斯估計(jì)器。
訓(xùn)練時(shí),編碼器為輸入圖像的不同特征創(chuàng)建潛在分布。
本質(zhì)上,該模型學(xué)習(xí)了訓(xùn)練圖像的共同特征,并為它們分配了它們發(fā)生的概率。然后可以使用概率分布對(duì)圖像進(jìn)行逆向工程,生成與原始訓(xùn)練圖像相似的新圖像。?
這種類型的自動(dòng)編碼器可以像GAN一樣生成新圖像。由于 VAE 在生成行為方面比GAN更加靈活和可定制,因此它們適用于任何類型的藝術(shù)生成。
四、自動(dòng)編碼器與PCA有何不同?PCA 和自動(dòng)編碼器是降低特征空間維數(shù)的兩種流行方法。
PCA 從根本上說(shuō)是一種線性變換,但自動(dòng)編碼器可以描述復(fù)雜的非線性過(guò)程。如果我們要構(gòu)建一個(gè)線性網(wǎng)絡(luò)(即在每一層不使用非線性激活函數(shù)),我們將觀察到與 PCA 中相似的降維。
PCA 試圖發(fā)現(xiàn)描述原始數(shù)據(jù)的低維超平面,而自動(dòng)編碼器能夠?qū)W習(xí)非線性流形(流形簡(jiǎn)單地定義為連續(xù)的、不相交的表面)。
與自動(dòng)編碼器相比,PCA 的計(jì)算速度更快且成本更低。但由于參數(shù)數(shù)量多,自編碼器容易過(guò)擬合。
五、自編碼器與GAN有何不同?? 1、兩者都是生成模型。AE試圖找到以特定輸入(更高維度)為條件的數(shù)據(jù)的低維表示,而GAN 試圖創(chuàng)建足以概括以鑒別器為條件的真實(shí)數(shù)據(jù)分布的表示。
? 2、雖然它們都屬于無(wú)監(jiān)督學(xué)習(xí)的范疇,但它們是解決問(wèn)題的不同方法。
GAN 是一種生成模型——它應(yīng)該學(xué)習(xí)生成數(shù)據(jù)集的新樣本。
變分自動(dòng)編碼器是生成模型,但普通的自動(dòng)編碼器只是重建它們的輸入,不能生成真實(shí)的新樣本。
https://www.quora.com/What-is-the-difference-between-Generative-Adversarial-Networks-and-Autoencodershttps://www.quora.com/What-is-the-difference-between-Generative-Adversarial-Networks-and-Autoencoders
? 去噪自動(dòng)編碼器的一個(gè)應(yīng)用的例子是預(yù)處理圖像以提高光學(xué)字符識(shí)別 (OCR) 算法的準(zhǔn)確性。如果您以前應(yīng)用過(guò)OCR,就會(huì)知道一丁點(diǎn)錯(cuò)誤的噪聲(例如,打印機(jī)墨水污跡、掃描過(guò)程中的圖像質(zhì)量差等)都會(huì)嚴(yán)重影響OCR識(shí)別的效果。使用去噪自編碼器,可以自動(dòng)對(duì)圖像進(jìn)行預(yù)處理,提高質(zhì)量,從而提高OCR識(shí)別算法的準(zhǔn)確性。
? 我們這里故意向MNIST訓(xùn)練圖像添加噪聲。目的是使我們的自動(dòng)編碼器能夠有效地從輸入圖像中去除噪聲。
2、參考代碼? 創(chuàng)建autoencoder_for_denoising.py文件,插入以下代碼。
# import the necessary packages
from tensorflow.keras.layers import BatchNormalization
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import Conv2DTranspose
from tensorflow.keras.layers import LeakyReLU
from tensorflow.keras.layers import Activation
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Reshape
from tensorflow.keras.layers import Input
from tensorflow.keras.models import Model
from tensorflow.keras import backend as K
import numpy as np
class ConvAutoencoder:
@staticmethod
def build(width, height, depth, filters=(32, 64), latentDim=16):
# initialize the input shape to be "channels last" along with
# the channels dimension itself
# channels dimension itself
inputShape = (height, width, depth)
chanDim = -1
# define the input to the encoder
inputs = Input(shape=inputShape)
x = inputs
# loop over the number of filters
for f in filters:
# apply a CONV =>RELU =>BN operation
x = Conv2D(f, (3, 3), strides=2, padding="same")(x)
x = LeakyReLU(alpha=0.2)(x)
x = BatchNormalization(axis=chanDim)(x)
# flatten the network and then construct our latent vector
volumeSize = K.int_shape(x)
x = Flatten()(x)
latent = Dense(latentDim)(x)
# build the encoder model
encoder = Model(inputs, latent, name="encoder")
# start building the decoder model which will accept the
# output of the encoder as its inputs
latentInputs = Input(shape=(latentDim,))
x = Dense(np.prod(volumeSize[1:]))(latentInputs)
x = Reshape((volumeSize[1], volumeSize[2], volumeSize[3]))(x)
# loop over our number of filters again, but this time in
# reverse order
for f in filters[::-1]:
# apply a CONV_TRANSPOSE =>RELU =>BN operation
x = Conv2DTranspose(f, (3, 3), strides=2, padding="same")(x)
x = LeakyReLU(alpha=0.2)(x)
x = BatchNormalization(axis=chanDim)(x)
# apply a single CONV_TRANSPOSE layer used to recover the
# original depth of the image
x = Conv2DTranspose(depth, (3, 3), padding="same")(x)
outputs = Activation("sigmoid")(x)
# build the decoder model
decoder = Model(latentInputs, outputs, name="decoder")
# our autoencoder is the encoder + decoder
autoencoder = Model(inputs, decoder(encoder(inputs)), name="autoencoder")
# return a 3-tuple of the encoder, decoder, and autoencoder
return (encoder, decoder, autoencoder)
# set the matplotlib backend so figures can be saved in the background
import matplotlib
matplotlib.use("Agg")
# import the necessary packages
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.datasets import mnist
import matplotlib.pyplot as plt
import numpy as np
import argparse
import cv2
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-s", "--samples", type=int, default=8, help="# number of samples to visualize when decoding")
ap.add_argument("-o", "--output", type=str, default="output.png", help="path to output visualization file")
ap.add_argument("-p", "--plot", type=str, default="plot.png", help="path to output plot file")
args = vars(ap.parse_args())
# initialize the number of epochs to train for and batch size
EPOCHS = 25
BS = 32
# load the MNIST dataset
print("[INFO] loading MNIST dataset...")
((trainX, _), (testX, _)) = mnist.load_data()
# add a channel dimension to every image in the dataset, then scale
# the pixel intensities to the range [0, 1]
trainX = np.expand_dims(trainX, axis=-1)
testX = np.expand_dims(testX, axis=-1)
trainX = trainX.astype("float32") / 255.0
testX = testX.astype("float32") / 255.0
# sample noise from a random normal distribution centered at 0.5 (since
# our images lie in the range [0, 1]) and a standard deviation of 0.5
trainNoise = np.random.normal(loc=0.5, scale=0.5, size=trainX.shape)
testNoise = np.random.normal(loc=0.5, scale=0.5, size=testX.shape)
trainXNoisy = np.clip(trainX + trainNoise, 0, 1)
testXNoisy = np.clip(testX + testNoise, 0, 1)
# construct our convolutional autoencoder
print("[INFO] building autoencoder...")
(encoder, decoder, autoencoder) = ConvAutoencoder.build(28, 28, 1)
opt = Adam(lr=1e-3)
autoencoder.compile(loss="mse", optimizer=opt)
# train the convolutional autoencoder
H = autoencoder.fit(trainXNoisy, trainX, validation_data=(testXNoisy, testX), epochs=EPOCHS, batch_size=BS)
# construct a plot that plots and saves the training history
N = np.arange(0, EPOCHS)
plt.style.use("ggplot")
plt.figure()
plt.plot(N, H.history["loss"], label="train_loss")
plt.plot(N, H.history["val_loss"], label="val_loss")
plt.title("Training Loss and Accuracy")
plt.xlabel("Epoch #")
plt.ylabel("Loss/Accuracy")
plt.legend(loc="lower left")
plt.savefig(args["plot"])
# use the convolutional autoencoder to make predictions on the
# testing images, then initialize our list of output images
print("[INFO] making predictions...")
decoded = autoencoder.predict(testXNoisy)
outputs = None
# loop over our number of output samples
for i in range(0, args["samples"]):
# grab the original image and reconstructed image
original = (testXNoisy[i] * 255).astype("uint8")
recon = (decoded[i] * 255).astype("uint8")
# stack the original and reconstructed image side-by-side
output = np.hstack([original, recon])
# if the outputs array is empty, initialize it as the current
# side-by-side image display
if outputs is None:
outputs = output
# otherwise, vertically stack the outputs
else:
outputs = np.vstack([outputs, output])
# save the outputs image to disk
cv2.imwrite(args["output"], outputs)
? 然后輸入以下命令進(jìn)行訓(xùn)練。
python train_denoising_autoencoder.py --output output_denoising.png --plot plot_denoising.png
3、訓(xùn)練結(jié)果? 經(jīng)過(guò)25epoch的訓(xùn)練結(jié)果。
對(duì)應(yīng)的去噪結(jié)果圖,左邊是添加噪聲的原始MNIST數(shù)字,而右邊是去噪自動(dòng)編碼器的輸出——可以看到去噪自動(dòng)編碼器能夠在消除噪音的同時(shí)從圖像中恢復(fù)原始信號(hào)。
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
本文名稱:機(jī)器學(xué)習(xí)筆記-自動(dòng)編碼器autoencoder-創(chuàng)新互聯(lián)
標(biāo)題來(lái)源:http://www.rwnh.cn/article40/dosgho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、云服務(wù)器、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航、外貿(mào)建站、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容