成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

深度學習:自動編碼器基礎和類型

開發 開發工具 深度學習
很顯然,深度學習即將對我們的社會產生重大顯著的影響。今天我們將進一步了解深度學習的架構類型,并詳細討論自動編碼器。

很顯然,深度學習即將對我們的社會產生重大顯著的影響。Mobibit 創始人兼 CEO Pramod Chandrayan 近日在 codeburst.io 上發文對自動編碼器的基礎知識和類型進行了介紹并給出了代碼實例。

[[204722]]

繼續我之前的文章《深度學習:什么&為什么?》(https://goo.gl/Ka3YoF),今天我們將進一步了解深度學習的架構類型,并詳細討論自動編碼器。

當人類大腦與深度學習機器合作時:

在我們開始揭秘深度網絡之前,讓我們先定義一下深度學習。根據我的理解:

深度學習是一種先進的機器學習技術,其中存在多個彼此通信的抽象層,每一層都與前一層深度相連,并根據前一層饋送的輸出進行決策。

Investopedia 將深度學習定義成:

深度學習是人工智能(AI)領域中機器學習中的一個子集,其有網絡狀的結構,可以從非結構化或無標記的數據中以無監督的方式學習。也被稱為深度神經學習或深度神經網絡。

今天我們將深入解讀無監督預訓練網絡(Unsupervised Pertained Networks)的工作方式。

UPN:無監督預訓練網絡

這種無監督學習網絡可以進一步分類成

  • 自動編碼器
  • 深度信念網絡(DBN)
  • 生成對抗網絡(GAN)

自動編碼器是一種有三層的神經網絡:輸入層、隱藏層(編碼層)和解碼層。該網絡的目的是重構其輸入,使其隱藏層學習到該輸入的良好表征。

自動編碼器神經網絡是一種無監督機器學習算法,其應用了反向傳播,可將目標值設置成與輸入值相等。自動編碼器的訓練目標是將輸入復制到輸出。在內部,它有一個描述用于表征其輸入的代碼的隱藏層。

 


 

 

自動編碼器的目標是學習函數 h(x)≈x。換句話說,它要學習一個近似的恒等函數,使得輸出 x^ 近似等于輸入 x。自動編碼器屬于神經網絡家族,但它們也和 PCA(主成分分析)緊密相關。

關于自動編碼器的一些關鍵事實:

  • 它是一種類似于 PCA 的無監督機器學習算法
  • 它要最小化和 PCA 一樣的目標函數
  • 它是一種神經網絡
  • 這種神經網絡的目標輸出就是其輸入

盡管自動編碼器與 PCA 很相似,但自動編碼器比 PCA 靈活得多。在編碼過程中,自動編碼器既能表征線性變換,也能表征非線性變換;而 PCA 只能執行線性變換。因為自動編碼器的網絡表征形式,所以可將其作為層用于構建深度學習網絡。

自動編碼器的類型:

  1. 去噪自動編碼器
  2. 稀疏自動編碼器
  3. 變分自動編碼器(VAE)
  4. 收縮自動編碼器(CAE/contractive autoencoder)

A. 去噪自動編碼器

這是最基本的一種自動編碼器,它會隨機地部分采用受損的輸入來解決恒等函數風險,使得自動編碼器必須進行恢復或去噪。

這項技術可用于得到輸入的良好表征。良好的表征是指可以從受損的輸入穩健地獲得的表征,該表征可被用于恢復其對應的無噪聲輸入。

去噪自動編碼器背后的思想很簡單。為了迫使隱藏層發現更加穩健的特征并且為了防止其只是學習其中的恒等關系,我們在訓練自動編碼器時會讓其從受損的版本中重建輸入。

應用在輸入上的噪聲量以百分比的形式呈現。一般來說,30% 或 0.3 就很好,但如果你的數據非常少,你可能就需要考慮增加更多噪聲。

堆疊的去噪自動編碼器(SDA):

堆疊的去噪自動編碼器(SDA)

這是一種在層上使用了無監督預訓練機制的去噪自編碼器,其中當一層被預訓練用于在之前層的輸入上執行特征選擇和特征提取后,后面會跟上一個監督式的微調(fine-tuning)階段。SDA 只是將很多個去噪自動編碼器融合在了一起。一旦前面 k 層訓練完成,我們就可以訓練第 k+1 層,因為我們現在可以根據下面的層計算代碼或隱含表征。

一旦所有層都預訓練完成,網絡就會進入一個被稱為微調的階段。在這里我們會為微調使用監督學習機制,以最小化被監督任務上的預測誤差。然后,我們以訓練多層感知器的方式訓練整個網絡。在這個階段,我們僅考慮每個自動編碼器的編碼部分。這個階段是有監督的,自此以后我們就在訓練中使用目標類別了。

使用代碼示例解釋 SDA

這一節源自 deeplearning.net(對于想要理解深度學習的人來說,這個網站提供了很好的參考),其中使用案例對堆疊的去噪自動編碼器進行了很好的解釋。

我們可以以兩種方式看待堆疊的去噪自動編碼器:一是自動編碼器列表,二是多層感知器(MLP)。在預訓練過程中,我們使用了***種方式,即我們將我們的模型看作是一組自動編碼器列表,并分開訓練每個自動編碼器。在第二個訓練階段,我們使用第二種方式。這兩種方式是有聯系的,因為:

自動編碼器和 MLP 的 sigmoid 層共享參數;

MLP 的中間層計算出的隱含表征被用作自動編碼器的輸入。

  1. class SdA(object):  
  2. """Stacked denoising auto-encoder class (SdA)  
  3. A stacked denoising autoencoder model is obtained by stacking several  
  4. dAs. The hidden layer of the dA at layer `i` becomes the input of  
  5. the dA at layer `i+1`. The first layer dA gets as input the input of  
  6. the SdA, and the hidden layer of the last dA represents the output.  
  7. Note that after pretraining, the SdA is dealt with as a normal MLP,  
  8. the dAs are only used to initialize the weights.  
  9. """ 
  10. def __init__(  
  11. self,  
  12. numpy_rng,  
  13. theano_rng=None 
  14. n_ins=784 
  15. hidden_layers_sizes=[500, 500],  
  16. n_outs=10 
  17. corruption_levels=[0.1, 0.1]  
  18. ):  
  19. """ This class is made to support a variable number of layers. 
  20. :type numpy_rng: numpy.random.RandomState 
  21. :param numpy_rng: numpy random number generator used to draw initial  
  22. weights  
  23. :type theano_rng: theano.tensor.shared_randomstreams.RandomStreams  
  24. :param theano_rng: Theano random generator; if None is given one is  
  25. generated based on a seed drawn from `rng`  
  26. :type n_ins: int 
  27. :param n_ins: dimension of the input to the sdA  
  28. :type hidden_layers_sizes: list of ints  
  29. :param hidden_layers_sizes: intermediate layers size, must contain  
  30. at least one value  
  31. :type n_outs: int  
  32. :param n_outs: dimension of the output of the network  
  33. :type corruption_levels: list of float  
  34. :param corruption_levels: amount of corruption to use for each  
  35. layer  
  36. """  
  37. self.sigmoid_layers = [] 
  38. self.dA_layers = []  
  39. self.params = []  
  40. self.n_layers = len(hidden_layers_sizes)  
  41. assert self.n_layers > 0 
  42. if not theano_rng: 
  43. theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))  
  44. # allocate symbolic variables for the data  
  45. self.x = T.matrix('x') # the data is presented as rasterized images  
  46. self.y = T.ivector('y') # the labels are presented as 1D vector of  
  47. # [int] labels 

self.sigmoid_layers 將會存儲 MLP 形式的 sigmoid 層,而 self.dA_layers 將會存儲與該 MLP 層關聯的去噪自動編碼器。接下來,我們構建 n_layers sigmoid 層和 n_layers 去噪自動編碼器,其中 n_layers 是我們的模型的深度。我們使用了多層感知器中引入的 HiddenLayer 類,但有一項修改:我們將 tanh 非線性替換成了 logistic 函數

我們鏈接了 sigmoid 層來構建一個 MLP,而且我們在構建自動編碼器時使得每個自動編碼器的編碼部分都與其對應的 sigmoid 層共享權重矩陣和偏置。

  1. for i in range(self.n_layers):  
  2. # construct the sigmoidal layer  
  3. # the size of the input is either the number of hidden units of  
  4. # the layer below or the input size if we are on the first layer  
  5. if i == 0:  
  6. input_size = n_ins  
  7. else:  
  8. input_size = hidden_layers_sizes[i - 1]  
  9. # the input to this layer is either the activation of the hidden  
  10. # layer below or the input of the SdA if you are on the first  
  11. # layer  
  12. if i == 0:  
  13. layer_input = self.x  
  14. else:  
  15. layer_input = self.sigmoid_layers[-1].output  
  16. sigmoid_layer = HiddenLayer(rng=numpy_rng 
  17. input=layer_input 
  18. n_in=input_size 
  19. n_out=hidden_layers_sizes[i],  
  20. activation=T.nnet.sigmoid)  
  21. # add the layer to our list of layers  
  22. self.sigmoid_layers.append(sigmoid_layer)  
  23. # its arguably a philosophical question... 
  24. # but we are going to only declare that the parameters of the 
  25. # sigmoid_layers are parameters of the StackedDAA 
  26. # the visible biases in the dA are parameters of those  
  27. # dA, but not the SdA  
  28. self.params.extend(sigmoid_layer.params)  
  29. # Construct a denoising autoencoder that shared weights with this  
  30. # layer  
  31. dAdA_layer = dA(numpy_rngnumpy_rng=numpy_rng,  
  32. theano_rngtheano_rng=theano_rng,  
  33. input=layer_input 
  34. n_visible=input_size 
  35. n_hidden=hidden_layers_sizes[i],  
  36. W=sigmoid_layer.W,  
  37. bhid=sigmoid_layer.b)  
  38. self.dA_layers.append(dA_layer) 

現在我們只需要在這個 sigmoid 層上添加一個 logistic 層即可,這樣我們就有了一個 MLP。我們將使用 LogisticRegression 類,這個類是在使用 logistic 回歸分類 MNIST 數字時引入的。

  1. # We now need to add a logistic layer on top of the MLP  
  2. self.logLayer = LogisticRegression 
  3. input=self.sigmoid_layers[-1].output, 
  4. n_in=hidden_layers_sizes[-1],  
  5. n_out=n_outs  
  6. self.params.extend(self.logLayer.params)  
  7. # construct a function that implements one step of finetunining  
  8. # compute the cost for second phase of training,  
  9. # defined as the negative log likelihood  
  10. selfself.finetune_cost = self.logLayer.negative_log_likelihood(self.y) 
  11. # compute the gradients with respect to the model parameters 
  12. # symbolic variable that points to the number of errors made on the  
  13. # minibatch given by self.x and self.y  
  14. selfself.errors = self.logLayer.errors(self.y) 

SdA 類也提供了一種為其層中的去噪自動編碼器生成訓練函數的方法。它們會作為一個列表返回,其中元素 i 是一個函數——該函數實現了訓練對應于第 i 層的 dA 的步驟。

  1. def pretraining_functions(self, train_set_x, batch_size):  
  2. ''' Generates a list of functions, each of them implementing one  
  3. step in trainnig the dA corresponding to the layer with same index.  
  4. The function will require as input the minibatch index, and to train  
  5. a dA you just need to iterate, calling the corresponding function on 
  6. all minibatch indexes. 
  7. :type train_set_x: theano.tensor.TensorType  
  8. :param train_set_x: Shared variable that contains all datapoints used  
  9. for training the dA  
  10. :type batch_size: int  
  11. :param batch_size: size of a [mini]batch 
  12. :type learning_rate: float  
  13. :param learning_rate: learning rate used during training for any of  
  14. the dA layers 
  15. '''  
  16. # index to a [mini]batch  
  17. index = T.lscalar('index') # index to a minibatch 

為了修改訓練過程中的受損水平或學習率,我們將它們與 Theano 變量聯系了起來。

  1. corruption_level = T.scalar('corruption') # % of corruption to use  
  2. learning_rate = T.scalar('lr') # learning rate to use  
  3. # begining of a batch, given `index`  
  4. batch_begin = index * batch_size  
  5. # ending of a batch given `index`  
  6. batch_end = batch_begin + batch_size  
  7. pretrain_fns = [] 
  8. for dA in self.dA_layers:  
  9. # get the cost and the updates list  
  10. cost, updates = dA.get_cost_updates(corruption_level,  
  11. learning_rate)  
  12. # compile the theano function  
  13. fn = theano.function(  
  14. inputs=[  
  15. index,  
  16. theano.In(corruption_level, value=0.2),  
  17. theano.In(learning_rate, value=0.1)  
  18. ],  
  19. outputs=cost 
  20. updatesupdates=updates,  
  21. givens={  
  22. self.x: train_set_x[batch_begin: batch_end]  
  23.  
  24.  
  25. # append `fn` to the list of functions  
  26. pretrain_fns.append(fn)  
  27. return pretrain_fns 

現在任意 pretrain_fns[i] 函數都可以使用索引參數了,可選的有 corruption(受損水平)或 lr(學習率)。注意這些參數名是在它們被構建時賦予 Theano 變量的名字,而不是 Python 變量(learning_rate 或 corruption_level)的名字。在使用 Theano 時一定要記住這一點。我們用同樣的方式構建了用于構建微調過程中所需函數的方法(train_fn、valid_score 和 test_score)。

  1. def build_finetune_functions(self, datasets, batch_size, learning_rate):  
  2. '''Generates a function `train` that implements one step of  
  3. finetuning, a function `validate` that computes the error on  
  4. a batch from the validation set, and a function `test` that  
  5. computes the error on a batch from the testing set  
  6. :type datasets: list of pairs of theano.tensor.TensorType 
  7. :param datasets: It is a list that contain all the datasets;  
  8. the has to contain three pairs, `train`,  
  9. `valid`, `test` in this order, where each pair  
  10. is formed of two Theano variables, one for the  
  11. datapoints, the other for the labels  
  12. :type batch_size: int  
  13. :param batch_size: size of a minibatch 
  14. :type learning_rate: float 
  15. :param learning_rate: learning rate used during finetune stage  
  16. '''  
  17. (train_set_x, train_set_y) = datasets[0]  
  18. (valid_set_x, valid_set_y) = datasets[1]  
  19. (test_set_x, test_set_y) = datasets[2]  
  20. # compute number of minibatches for training, validation and testing  
  21. n_valid_batches = valid_set_x.get_value(borrow=True).shape[0]  
  22. n_valid_batches //= batch_size  
  23. n_test_batches = test_set_x.get_value(borrow=True).shape[0]  
  24. n_test_batches //= batch_size  
  25. index = T.lscalar('index') # index to a [mini]batch  
  26. # compute the gradients with respect to the model parameters  
  27. gparams = T.grad(self.finetune_cost, self.params)  
  28. # compute list of fine-tuning updates  
  29. updates = [  
  30. (param, param - gparam * learning_rate)  
  31. for param, gparam in zip(self.params, gparams)  
  32.  
  33. train_fn = theano.function(  
  34. inputs=[index],  
  35. outputs=self.finetune_cost,  
  36. updatesupdates=updates,  
  37. givens={  
  38. self.x: train_set_x[ 
  39. index * batch_size: (index + 1) * batch_size  
  40. ],  
  41. self.y: train_set_y[  
  42. index * batch_size: (index + 1) * batch_size  
  43.  
  44. },  
  45.  
  46. test_score_i = theano.function(  
  47. [index],  
  48. self.errors,  
  49. givens={  
  50. self.x: test_set_x[  
  51. index * batch_size: (index + 1) * batch_size  
  52. ],  
  53. self.y: test_set_y[  
  54. index * batch_size: (index + 1) * batch_size  
  55.  
  56. },  
  57.  
  58. valid_score_i = theano.function(  
  59. [index],  
  60. self.errors,  
  61. givens={  
  62. self.x: valid_set_x[  
  63. index * batch_size: (index + 1) * batch_size  
  64. ],  
  65. self.y: valid_set_y[  
  66. index * batch_size: (index + 1) * batch_size  
  67.  
  68. },  
  69.  
  70. # Create a function that scans the entire validation set  
  71. def valid_score():  
  72. return [valid_score_i(i) for i in range(n_valid_batches)]  
  73. # Create a function that scans the entire test set  
  74. def test_score(): 
  75. return [test_score_i(i) for i in range(n_test_batches)]  
  76. return train_fn, valid_score, test_score 

注意,valid_score 和 test_score 并不是 Theano 函數,而是分別在整個驗證集和整個測試集上循環的 Python 函數,可以在這些集合上產生一個損失列表。

總結

下面給出的幾行代碼就構建了一個堆疊的去噪自動編碼器:

  1. numpynumpy_rng = numpy.random.RandomState(89677)  
  2. print('... building the model')  
  3. # construct the stacked denoising autoencoder class  
  4. sda = SdA 
  5. numpy_rngnumpy_rng=numpy_rng,  
  6. n_ins=28 * 28,  
  7. hidden_layers_sizes=[1000, 1000, 1000],  
  8. n_outs=10  

該網絡的訓練分兩個階段:逐層的預訓練,之后是微調。

對于預訓練階段,我們將在網絡的所有層上進行循環。對于每個層,我們都將使用編譯過的實現 SGD 步驟的函數,以優化權重,從而降低該層的重構成本。這個函數將根據 pretraining_epochs 在訓練集上執行固定數量的 epoch。

  1. ######################### 
  2. # PRETRAINING THE MODEL #  
  3. #########################  
  4. print('... getting the pretraining functions')  
  5. pretraining_fns = sda.pretraining_functions(train_set_xtrain_set_x=train_set_x, 
  6. batch_sizebatch_size=batch_size)  
  7. print('... pre-training the model')  
  8. start_time = timeit.default_timer()  
  9. ## Pre-train layer-wise  
  10. corruption_levels = [.1, .2, .3]  
  11. for i in range(sda.n_layers):  
  12. # go through pretraining epochs  
  13. for epoch in range(pretraining_epochs): 
  14. # go through the training set 
  15. c = [] 
  16. for batch_index in range(n_train_batches): 
  17. c.append(pretraining_fns[i](index=batch_index 
  18. corruption=corruption_levels[i],  
  19. lr=pretrain_lr))  
  20. print('Pre-training layer %i, epoch %d, cost %f' % (i, epoch, numpy.mean(c, dtype='float64')))  
  21. end_time = timeit.default_timer() 
  22. print(('The pretraining code for file ' + 
  23. os.path.split(__file__)[1] + 
  24. ' ran for %.2fm' % ((end_time - start_time) / 60.)), file=sys.stderr) 

這里的微調循環和多層感知器中的微調過程很相似。唯一的區別是它使用了 build_finetune_functions 給出的函數。

執行代碼

用戶可以通過調用以下 Python CLI 來運行該代碼:

  1. python code/SdA.py 

默認情況下,該代碼會為每一層運行 15 次預訓練 epoch,其批大小為 1。***層的受損水平為 0.1,第二層為 0.2,第三層為 0.3。預訓練的學習率為 0.001,微調學習率為 0.1。預訓練耗時 585.01 分鐘,每 epoch 平均 13 分鐘。微調經歷了 36 epoch,耗時 444.2 分鐘,每 epoch 平均 12.34 分鐘。***的驗證分數是 1.39%,測試分數是 1.3%。這些結果是在配置了 Intel Xeon E5430 @ 2.66GHz CPU 的機器上得到的,它有單線程的 GotoBLAS。

原文:https://codeburst.io/deep-learning-types-and-autoencoders-a40ee6754663

【本文是51CTO專欄機構“機器之心”的原創譯文,微信公眾號“機器之心( id: almosthuman2014)”】

 

戳這里,看該作者更多好文

責任編輯:趙寧寧 來源: 51CTO專欄
相關推薦

2017-07-10 13:45:33

自動編碼數據生成GAN

2019-05-22 17:34:16

代碼開發工具

2021-03-22 10:52:13

人工智能深度學習自編碼器

2021-03-29 11:37:50

人工智能深度學習

2021-12-17 10:09:47

編碼器語言模型DeepMind

2017-08-16 21:58:05

自動編碼器DeepCoder深度學習

2022-02-14 10:16:29

AI模型編碼

2024-06-18 08:52:50

LLM算法深度學習

2022-04-02 21:46:27

深度學習編碼器圖像修復

2022-03-02 13:44:10

3D模型數據

2017-12-26 10:48:37

深度學習原始數據

2025-04-02 07:30:00

2021-11-02 20:44:47

數字化

2012-04-01 16:40:45

編碼器

2025-04-10 06:30:00

2025-04-10 11:52:55

2012-04-10 16:55:22

PowerSmart編碼器

2020-07-03 08:45:26

神經網絡結構

2020-07-01 09:08:55

神經網絡深度學習網絡
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 中文字幕一页二页 | 欧美日韩一卡二卡 | 国产精品日产欧美久久久久 | 日日草夜夜草 | 亚洲精品日韩综合观看成人91 | 国产色婷婷精品综合在线手机播放 | 天天拍天天射 | 精品欧美一区二区三区久久久 | 一区二区三区av | 色屁屁在线观看 | 欧美一区二区大片 | 亚洲欧美在线免费观看 | www.亚洲视频 | 成人高清在线视频 | 一级片在线观看视频 | 免费黄色在线观看 | 日日爱视频| 亚洲三区视频 | 国产亚洲成av人片在线观看桃 | 中文字幕成人在线 | 51ⅴ精品国产91久久久久久 | 蜜桃臀av一区二区三区 | 免费色网址 | 天天综合久久网 | 精品免费视频 | 在线免费观看黄色网址 | 中文字幕一区在线观看视频 | 日韩在线免费视频 | 精品少妇一区二区三区在线播放 | 欧美性网 | 成人午夜网 | 成人在线视频网 | 久久99蜜桃综合影院免费观看 | 久久综合一区 | 久久久久久久一级 | 久草在线视频中文 | 国产精品中文字幕在线 | 免费国产一区二区视频 | 日韩精品免费视频 | 国产精品久久久爽爽爽麻豆色哟哟 | 亚洲精品乱码久久久久久黑人 |