最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Keras搭建自編碼器操作

 更新時間:2020年07月03日 09:11:21   作者:經(jīng)年不往  
這篇文章主要介紹了Keras搭建自編碼器操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

簡介:

傳統(tǒng)機器學(xué)習(xí)任務(wù)任務(wù)很大程度上依賴于好的特征工程,但是特征工程往往耗時耗力,在視頻、語音和視頻中提取到有效特征就更難了,工程師必須在這些領(lǐng)域有非常深入的理解,并且需要使用專業(yè)算法提取這些數(shù)據(jù)的特征。深度學(xué)習(xí)則可以解決人工難以提取有效特征的問題,大大緩解機器學(xué)習(xí)模型對特征工程的依賴。

深度學(xué)習(xí)在早期一度被認為是一種無監(jiān)督的特征學(xué)習(xí)過程,模仿人腦對特征逐層抽象的過程。這其中兩點很重要:一是無監(jiān)督學(xué)習(xí);二是逐層訓(xùn)練。例如在圖像識別問題中,假定我們有許多汽車圖片,要如何利用計算機進行識別任務(wù)呢?如果從像素級開始進行訓(xùn)練分類器,那么絕大多數(shù)算法很難工作。如果我們提取高階特征,比如汽車的車輪、汽車的車窗、車身等。那么就可以使用這些高階特征非常準確的對圖像進行分類。不過高階特征都是由底層特征組成,這便是深度學(xué)習(xí)訓(xùn)練過程中所做的特征學(xué)習(xí)。

早年有學(xué)者發(fā)現(xiàn),可以使用少量的基本特征進行組合拼裝得到更高層抽象的特征,這其實就是我們常說的特征的稀疏表達。對圖像任務(wù)來說,一張原始圖片可以由較少的圖片碎片組合得到。對語音識別任務(wù)來講,絕大多數(shù)的聲音也可以由一些基本的結(jié)構(gòu)線性組合得到。對人臉識別任務(wù)來說,根據(jù)不同的器官,如:鼻子、嘴、眉毛、眼睛瞪,這些器官可以向上拼出不同樣式的人臉,最后模型通過在圖片中匹配這些不同樣式的人臉來進行識別。在深度神經(jīng)網(wǎng)絡(luò)中,對每一層神經(jīng)網(wǎng)絡(luò)來說前一層的輸出都是未加工的像素,而這一層則是對像素進行加工組織成更高階的特征的過程(即前面提到過的圖片碎片進行線性組合加工的過程)。

根據(jù)上述基本概念的描述,特征是可以不斷抽象轉(zhuǎn)為高一層特征的,那我們?nèi)绾握业竭@些基本結(jié)構(gòu),然后如何抽象?這里引出無監(jiān)督的自編碼器來提取特征。自編碼器--顧名思義,可以使用自身高階特征編碼自己。它的輸入和輸出是一致的。因此,它的基本思想是使用稀疏一些高階特征重新組合來重構(gòu)自己。自編碼器的剛開始提出是Hinton在Science上發(fā)表文章,用來解決數(shù)據(jù)降維問題。此外,Hinton還提出了基于深度信念網(wǎng)絡(luò)的無監(jiān)督逐層訓(xùn)練的貪心算法,為訓(xùn)練很深的網(wǎng)絡(luò)提供了一個可行的方案。深度信念網(wǎng)絡(luò)的提出是使用逐層訓(xùn)練的方式提取特征,使得在有監(jiān)督學(xué)習(xí)任務(wù)之前,使得網(wǎng)絡(luò)權(quán)重初始化到一個比較好的位置。其思想與自編碼器的非常相似。在此基礎(chǔ)上,國內(nèi)外學(xué)者又提出了自編碼器的各種版本,如:稀疏自編碼器、去噪自編碼器等。

本文使用Keras深度學(xué)習(xí)開發(fā)庫,在MNIST數(shù)據(jù)集上實現(xiàn)了簡單自編碼器、深度稀疏自編碼器和卷積自編碼器。

自編碼器用途:

目前自編碼器的應(yīng)用主要有兩個方面,第一是數(shù)據(jù)去噪,第二是為進行可視化而降維。配合適當?shù)木S度和稀疏約束,自編碼器可以學(xué)習(xí)到比PCA等技術(shù)更有意思的數(shù)據(jù)投影。此外,在數(shù)據(jù)共有特征建模方面,也有叫廣泛的應(yīng)用。

1、簡單自編碼器

簡單自編碼器

from keras.layers import Input, Dense
from keras.models import Model
from keras.datasets import mnist
import numpy as np
import matplotlib.pyplot as plt
 
(x_train, _), (x_test, _) = mnist.load_data()
 
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('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)
 
encoding_dim = 32
input_img = Input(shape=(784,))
 
encoded = Dense(encoding_dim, activation='relu')(input_img)
decoded = Dense(784, activation='sigmoid')(encoded)
 
autoencoder = Model(inputs=input_img, outputs=decoded)
encoder = Model(inputs=input_img, outputs=encoded)
 
encoded_input = Input(shape=(encoding_dim,))
decoder_layer = autoencoder.layers[-1]
 
decoder = Model(inputs=encoded_input, outputs=decoder_layer(encoded_input))
 
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
 
autoencoder.fit(x_train, x_train, epochs=50, batch_size=256, 
  shuffle=True, validation_data=(x_test, x_test))
 
encoded_imgs = encoder.predict(x_test)
decoded_imgs = decoder.predict(encoded_imgs)
 
n = 10 # how many digits we will display
plt.figure(figsize=(20, 4))
for i in range(n):
 ax = 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)
 
 ax = 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)
plt.show()

測試效果:

2、深度自編碼器、稀疏自編碼器

為解決自編碼重構(gòu)損失大的問題,使用多層網(wǎng)絡(luò)搭建自編碼器。對隱層單元施加稀疏性約束的話,會得到更為緊湊的表達,只有一小部分神經(jīng)元會被激活。在Keras中,我們可以通過添加一個activity_regularizer達到對某層激活值進行約束的目的

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 
 
# X shape (60,000 28x28), y shape (10,000, ) 
(x_train, _), (x_test, y_test) = mnist.load_data() 
 
# 數(shù)據(jù)預(yù)處理 
x_train = x_train.astype('float32') / 255. # minmax_normalized 
x_test = x_test.astype('float32') / 255. # 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) 
 
# 壓縮特征維度至2維 
encoding_dim = 2 
 
# this is our input placeholder 
input_img = Input(shape=(784,)) 
 
# 編碼層 
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) 
 
# 解碼層 
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) 
 
# 構(gòu)建自編碼模型 
autoencoder = Model(inputs=input_img, outputs=decoded) 
 
# 構(gòu)建編碼模型 
encoder = Model(inputs=input_img, outputs=encoder_output) 
 
# compile autoencoder 
autoencoder.compile(optimizer='adam', loss='mse') 
 
autoencoder.summary()
encoder.summary()
 
# training 
autoencoder.fit(x_train, x_train, epochs=10, batch_size=256, shuffle=True) 
 
# plotting 
encoded_imgs = encoder.predict(x_test) 
 
plt.scatter(encoded_imgs[:, 0], encoded_imgs[:, 1], c=y_test,s=3) 
plt.colorbar() 
plt.show() 
 
decoded_imgs = autoencoder.predict(x_test)
# use Matplotlib (don't ask)
import matplotlib.pyplot as plt
 
n = 10 # how many digits we will display
plt.figure(figsize=(20, 4))
for i in range(n):
 # display original
 ax = 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 reconstruction
 ax = 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)
plt.show() 

運行結(jié)果:

3、卷積自編碼器

卷積自編碼器的編碼器部分由卷積層和MaxPooling層構(gòu)成,MaxPooling負責(zé)空域下采樣。而解碼器由卷積層和上采樣層構(gòu)成。

from keras.layers import Input, Dense, Convolution2D, MaxPooling2D, UpSampling2D
from keras.models import Model
from keras.datasets import mnist
import numpy as np
 
(x_train, _), (x_test, _) = mnist.load_data()
 
x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.
print('---> x_train shape: ', x_train.shape)
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1))
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1))
print('---> xtrain shape: ', x_train.shape)
print('---> x_test shape: ', x_test.shape)
input_img = Input(shape=(28, 28, 1))
 
x = Convolution2D(16, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Convolution2D(8, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Convolution2D(8, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
 
x = Convolution2D(8, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(8, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Convolution2D(16, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Convolution2D(1, (3, 3), activation='sigmoid', padding='same')(x)
 
autoencoder = Model(inputs=input_img, outputs=decoded)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
 
# 打開一個終端并啟動TensorBoard,終端中輸入 tensorboard --logdir=/autoencoder
autoencoder.fit(x_train, x_train, epochs=10, batch_size=256,
  shuffle=True, validation_data=(x_test, x_test))
 
decoded_imgs = autoencoder.predict(x_test)
import matplotlib.pyplot as plt
decoded_imgs = autoencoder.predict(x_test)
 
n = 10
plt.figure(figsize=(20, 4))
for i in range(1, n+1):
 # display original
 ax = plt.subplot(2, n, i)
 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, n, i + n)
 plt.imshow(decoded_imgs[i].reshape(28, 28))
 plt.gray()
 ax.get_xaxis().set_visible(False)
 ax.get_yaxis().set_visible(False)
plt.show()

訓(xùn)練結(jié)果展示:

以上這篇Keras搭建自編碼器操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 淺談如何使用python抓取網(wǎng)頁中的動態(tài)數(shù)據(jù)實現(xiàn)

    淺談如何使用python抓取網(wǎng)頁中的動態(tài)數(shù)據(jù)實現(xiàn)

    這篇文章主要介紹了淺談如何使用python抓取網(wǎng)頁中的動態(tài)數(shù)據(jù)實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • PyQt5爬取12306車票信息程序的實現(xiàn)

    PyQt5爬取12306車票信息程序的實現(xiàn)

    12306是學(xué)習(xí)爬蟲的比較好的一個練手網(wǎng)站。本文主要實現(xiàn)了PyQt5爬取12306車票信息程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Python自帶的IDE在哪里

    Python自帶的IDE在哪里

    在本篇內(nèi)容里小編給大家分享的是關(guān)于如何找到Python自帶的IDE的相關(guān)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。
    2020-07-07
  • Python實現(xiàn)哲學(xué)家就餐問題實例代碼

    Python實現(xiàn)哲學(xué)家就餐問題實例代碼

    這篇文章主要給大家介紹了關(guān)于Python實現(xiàn)哲學(xué)家就餐問題的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • python 爬蟲 批量獲取代理ip的實例代碼

    python 爬蟲 批量獲取代理ip的實例代碼

    今天小編就為大家分享一篇python 爬蟲 批量獲取代理ip的實例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-05-05
  • 利用Python正則表達式過濾敏感詞的方法

    利用Python正則表達式過濾敏感詞的方法

    今天小編就為大家分享一篇利用Python正則表達式過濾敏感詞的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-01-01
  • pycharm部署、配置anaconda環(huán)境的教程

    pycharm部署、配置anaconda環(huán)境的教程

    PyCharm是一款很好用很流行的python編輯器。Anaconda是專注于數(shù)據(jù)分析的Python發(fā)行版本,包含了conda、Python等190多個科學(xué)包及其依賴項,這篇文章主要介紹了pycharm部署、配置anaconda環(huán)境的教程,需要的朋友可以參考下
    2020-03-03
  • CAPL與Python交互的實現(xiàn)

    CAPL與Python交互的實現(xiàn)

    CAPL能做超級多的功能,本文主要介紹了CAPL與Python交互的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05
  • Python編程實現(xiàn)的簡單神經(jīng)網(wǎng)絡(luò)算法示例

    Python編程實現(xiàn)的簡單神經(jīng)網(wǎng)絡(luò)算法示例

    這篇文章主要介紹了Python編程實現(xiàn)的簡單神經(jīng)網(wǎng)絡(luò)算法,結(jié)合實例形式分析了神經(jīng)網(wǎng)絡(luò)算法的原理及Python相關(guān)算法實現(xiàn)技巧,需要的朋友可以參考下
    2018-01-01
  • Python flashtext文本搜索和替換操作庫功能使用探索

    Python flashtext文本搜索和替換操作庫功能使用探索

    本文將深入介紹Python flashtext庫,包括其基本用法、功能特性、示例代碼以及實際應(yīng)用場景,以幫助大家更好地利用這個有用的工具
    2024-01-01

最新評論

盘山县| 尖扎县| 绍兴县| 永州市| 滨海县| 张家口市| 普洱| 白河县| 绥阳县| 翁牛特旗| 佳木斯市| 溧阳市| 苍溪县| 张家港市| 志丹县| 阿合奇县| 元阳县| 牙克石市| 泰安市| 惠州市| 静海县| 磐安县| 宿迁市| 林西县| 上高县| 汝州市| 仁化县| 德格县| 十堰市| 嘉善县| 潜山县| 亳州市| 清涧县| 葫芦岛市| 海林市| 湖口县| 铁力市| 灵川县| 昔阳县| 平安县| 嘉定区|