#_*_coding:UTF-8_*_ import cPickle
import gzip
import os
import sys
import time
import numpy
import theano
import theano.tensor as T #theano中一些常見的符號(hào)操作在子庫tensor中
from theano.tensor.shared_randomStreams import RandomStreams
from logistic_sgd import load_data
from utils import tile_raster_images
import PIL.Image #繪圖所用
class dA(object):
def __init__(self, numpy_rng, theano_rng=None, input=None,
n_visible=784, n_hidden=500,
W=None, bhid=None, bvis=None):
self.n_visible = n_visible
self.n_hidden = n_hidden
if not theano_rng:
theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))
if not W:
initial_W = numpy.asarray(numpy_rng.uniform(
low=-4 * numpy.sqrt(6. / (n_hidden + n_visible)),
high=4 * numpy.sqrt(6. / (n_hidden + n_visible)),
size=(n_visible, n_hidden)), dtype=theano.config.floatX)
W = theano.shared(value=initial_W, name="W", borrow=True) #W,bvis,bhid都為共享變量
if not bvis:
bvis = theano.shared(value=numpy.zeros(n_visible, dtype=theano.config.floatX), borrow=True)
if not bhid:
bhid = theano.shared(value=numpy.zeros(n_hidden, dtype=theano.config.floatX), name="b", borrow=True)
self.W = W
self.b = bhid
self.b_prime = bvis
self.W_prime = self.W.T
self.theano_rng = theano_rng
if input == None:
self.x = T.dmatrix(name="input")
else:
self.x = input #保存輸入數(shù)據(jù)
self.params = [self.W, self.b, self.b_prime]
def get_corrupted_input(self, input, corruption_level):
return self.theano_rng.binomial(size=input.shape, n=1,
p=1 - corruption_level,
dtype=theano.config.floatX) * input #binomial()函數(shù)為產(chǎn)生0,1的分布,這里是設(shè)置產(chǎn)生1的概率為p
def get_hidden_values(self, input):
return T.nnet.sigmoid(T.dot(input, self.W) + self.b)
def get_reconstructed_input(self, hidden):
return T.nnet.sigmoid(T.dot(hidden, self.W_prime) + self.b_prime)
def get_cost_updates(self, corruption_level, learning_rate): #每調(diào)用該函數(shù)一次,就算出了前向傳播的誤差cost,網(wǎng)絡(luò)參數(shù)及其導(dǎo)數(shù)
tilde_x = self.get_corrupted_input(self.x, corruption_level)
y = self.get_hidden_values(tilde_x)
z = self.get_reconstructed_input(y)
L = - T.sum(self.x * T.log(z) + (1 - self.x) * T.log(1 - z), axis=1)
cost = T.mean(L)
gparams = T.grad(cost, self.params)
updates = []
for param, gparam in zip(self.params, gparams):
updates.append((param, param - learning_rate * gparam)) #append列表中存的是參數(shù)和其導(dǎo)數(shù)構(gòu)成的元組
return (cost, updates)
# 測(cè)試函數(shù)
def test_dA(learning_rate=0.1, training_epochs=15,
dataset="data/mnist.pkl.gz",
batch_size=20, output_folder="dA_plots"):
datasets = load_data(dataset)
train_set_x, train_set_y = datasets[0] #train_set_x矩陣中每一行代表一個(gè)樣本
n_train_batches = train_set_x.get_value(borrow=True).shape[0] / batch_size #求出batch的個(gè)數(shù)
index = T.lScalar() # index to a [mini]batch
x = T.matrix("x") # the data is presented as rasterized images
if not os.path.isdir(output_folder):
os.makedirs(output_folder)
os.chdir(output_folder)
# 沒有使用denoise時(shí)
rng = numpy.random.RandomState(123)
theano_rng = RandomStreams(rng.randint(2 ** 30))
da = dA(numpy_rng=rng, theano_rng=theano_rng, input=x,
n_visible=28 * 28, n_hidden=500) # 創(chuàng)建dA對(duì)象時(shí),并不需要數(shù)據(jù)x,只是給對(duì)象da中的一些網(wǎng)絡(luò)結(jié)構(gòu)參數(shù)賦值
cost, updates = da.get_cost_updates(corruption_level=0.,
learning_rate=learning_rate)
train_da = theano.function([index], cost, updates=updates, #theano.function()為定義一個(gè)符號(hào)函數(shù),這里的自變量為indexy
givens={x: train_set_x[index * batch_size: (index + 1) * batch_size]}) #輸出變量為cost
start_time = time.clock()
for epoch in xrange(training_epochs):
c = []
for batch_index in xrange(n_train_batches):
c.append(train_da(batch_index))
print "Training epoch %d, cost " % epoch, numpy.mean(c)
end_time = time.clock()
training_time = (end_time - start_time)
print >> sys.stderr, ("The no corruption code for file " +
os.path.split(__file__)[1] +
" ran for %.2fm" % ((training_time) / 60.))
image = PIL.Image.fromarray(
tile_raster_images(X=da.W.get_value(borrow=True).T,
img_shape=(28, 28), tile_shape=(10, 10),
tile_spacing=(1, 1)))
image.save("filters_corruption_0.png")
# 使用了denoise時(shí)
rng = numpy.random.RandomState(123)
theano_rng = RandomStreams(rng.randint(2 ** 30))
da = dA(numpy_rng=rng, theano_rng=theano_rng, input=x,
n_visible=28 * 28, n_hidden=500)
cost, updates = da.get_cost_updates(corruption_level=0.3,
learning_rate=learning_rate) #將輸入樣本每個(gè)像素點(diǎn)以30%的概率被清0
train_da = theano.function([index], cost, updates=updates,
givens={x: train_set_x[index * batch_size:
(index + 1) * batch_size]})
start_time = time.clock()
for epoch in xrange(training_epochs):
c = []
for batch_index in xrange(n_train_batches):
c.append(train_da(batch_index))
print "Training epoch %d, cost " % epoch, numpy.mean(c)
end_time = time.clock()
training_time = (end_time - start_time)
print >> sys.stderr, ("The 30% corruption code for file " +
os.path.split(__file__)[1] +
" ran for %.2fm" % (training_time / 60.))
image = PIL.Image.fromarray(tile_raster_images(
X=da.W.get_value(borrow=True).T,
img_shape=(28, 28), tile_shape=(10, 10),
tile_spacing=(1, 1)))
image.save("filters_corruption_30.png")
os.chdir("../")
if __name__ == "__main__":
test_dA()
其中與dAE相關(guān)的代碼為:
def get_corrupted_input(self, input, corruption_level):
return self.theano_rng.binomial(size=input.shape, n=1,p=1 - corruption_level,
dtype=theano.config.floatX) * input #binomial()函數(shù)為產(chǎn)生0,1的分布,這里是設(shè)置產(chǎn)生1的概率
?
?
參考資料:
Vincent, P., et al. (2008). Extracting and composing robust
features with denoising autoencoders. Proceedings of the 25th
international conference on Machine learning, ACM.
? ? ?https://github.com/rasmusbergpalm/DeepLearnToolbox
? ? ?http://deeplearning.net/tutorial/dA.html
? ? ?Deep learning :四十一 (Dropout 簡(jiǎn)單理解 )
? ? ?Installing Theano
? ? ?Easy Installation of an optimized Theano on Ubuntu
?
?
?
作者:tornadomeet
出處:http://www.cnblogs.com/tornadomeet
歡迎轉(zhuǎn)載或分享,但請(qǐng)務(wù)必聲明文章出處。 (新浪微博:tornadomeet,歡迎交流!)
文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/4277.html
摘要:的堆疊起來構(gòu)成,其中訓(xùn)練較高層的時(shí)加入了。作為非線性降維方法在圖像和文本降維實(shí)驗(yàn)中明顯優(yōu)于傳統(tǒng)方法。根據(jù)實(shí)驗(yàn)提出了對(duì)的的一種解釋。設(shè)計(jì)實(shí)驗(yàn)驗(yàn)證兩種因素的作用。傳統(tǒng)算法并不是以較大化為目標(biāo)的,另有證明算法不對(duì)應(yīng)任何優(yōu)化目標(biāo)函數(shù)
A?Fast?Learning?Algorithm?for?Deep?Belief?Nets?(2006)-?首
次提出layerwise?greedy?pretrai...
iKcamp
2019-04-25 17:57
評(píng)論0
收藏0
摘要:深度學(xué)習(xí)通過組合低層特征形成更加抽象的高層表示屬性類別或特征,以發(fā)現(xiàn)數(shù)據(jù)的分布式特征表示。深度學(xué)習(xí)的概念由等人于年提出。但是自年以來,機(jī)器學(xué)習(xí)領(lǐng)域,取得了突破性的進(jìn)展。
深度學(xué)習(xí)是機(jī)器學(xué)習(xí)研究中的一個(gè)新的領(lǐng)域,其動(dòng)機(jī)在于建立、模擬人腦進(jìn)行分析學(xué)習(xí)的神經(jīng)網(wǎng)絡(luò),它模仿人腦的機(jī)制來解釋數(shù)據(jù),例如圖像,聲音和文本。深度學(xué)習(xí)是無監(jiān)督學(xué)習(xí)的一種。
深度學(xué)習(xí)的概念源于人工神經(jīng)網(wǎng)絡(luò)的研究。含多隱層的多層感知...
Riddler
2019-04-25 17:57
評(píng)論0
收藏0
摘要:實(shí)驗(yàn)基礎(chǔ)其實(shí)實(shí)現(xiàn)該功能的主要步驟還是需要計(jì)算出網(wǎng)絡(luò)的損失函數(shù)以及其偏導(dǎo)數(shù),具體的公式可以參考前面的博文八。生成均勻分布的偽隨機(jī)數(shù)。
前言:
現(xiàn)在來進(jìn)入sparse autoencoder的一個(gè)實(shí)例練習(xí),參考Ng的網(wǎng)頁教程:Exercise:Sparse Autoencoder。
這個(gè)例子所要實(shí)現(xiàn)的內(nèi)容大概如下:從給定的很多張自然圖片中截取出大小為8*8的小patches圖片共10000張...
摘要:如果你對(duì)算法實(shí)戰(zhàn)感興趣,請(qǐng)快快關(guān)注我們吧。加入實(shí)戰(zhàn)微信群,實(shí)戰(zhàn)群,算法微信群,算法群。
作者:chen_h微信號(hào) & QQ:862251340微信公眾號(hào):coderpai簡(jiǎn)書地址:https://www.jianshu.com/p/b5c...
介紹一些人工智能技術(shù)的術(shù)語,如果你還有術(shù)語補(bǔ)充,請(qǐng)?jiān)L問 Github
English Terminology
中文術(shù)語
neur...
摘要:深度學(xué)習(xí)學(xué)習(xí)筆記整理系列作者聲明該的學(xué)習(xí)系列是整理自網(wǎng)上很大牛和機(jī)器學(xué)習(xí)專家所無私奉獻(xiàn)的資料的。但是自年以來,機(jī)器學(xué)習(xí)領(lǐng)域,取得了突破性的進(jìn)展。
Deep Learning(深度學(xué)習(xí))學(xué)習(xí)筆記整理系列
[email protected]
http://blog.csdn.net/zouxy09
作者:Zouxy
version 1.0? 2013-04-08聲明:1)該Deep Lea...
男| 高級(jí)講師
閱讀 3249· 2019-08-30 15:55
閱讀 2973· 2019-08-30 13:46
閱讀 1471· 2019-08-29 17:29
閱讀 3542· 2019-08-29 11:08
閱讀 3469· 2019-08-29 11:04
閱讀 1111· 2019-08-28 18:20
閱讀 565· 2019-08-26 13:37
閱讀 1358· 2019-08-26 11:49