DBN程序剖析

最近学习深度学习,学习时间半月不到,很多程序似懂非懂,用的又是不太明白的python。不过不怕。什么也难不倒无产阶级,自己剖析下,不指望指点别人,只希望高人能指点。

主函数大体可以分为 建立DBN网络,预训练模型和微调模型三部分。

1.建立DBN网络

由class DBN(object)的__init__函数来完成,思路是由前面的隐含层生成后面的隐含层,并生成与每一个隐含层对应的RBM层

2.预训练模型

首先获得预训练函数,由class DBN(object)的pretraining_functions函数来完成,训练的对象是RBM层.里面的核心语句为

 cost, updates = rbm.get_cost_updates(learning_rate,persistent=None, k=k)

 fn = theano.function(inputs=[index,theano.Param(learning_rate, default=0.1)], outputs=cost,updates=updates,givens={self.x:train_set_x[batch_begin:batch_end]})

第二句的输出是cost和updates ,要看清他们的结构还得从第一句的RBM的get_cost_updates函数来看。

get_cost_updates是执行一步CD/PCD运算。

 

 

程序首先建立一个类class DBN(object),里面包含3个函数:

1. def __init__(self, numpy_rng, theano_rng=None, n_ins=784,  hidden_layers_sizes=[500, 500], n_outs=10):

self.n_layers = len(hidden_layers_sizes) 结果为2,表示2层

if not theano_rng:
            theano_rng = RandomStreams(numpy_rng.randint(2 ** 30))

theano_rng说是随机产生器,不知道做什么用,往下看。

self.x = T.matrix('x')  # the data is presented as rasterized images 数据是光栅化的图像,是不是可以理解为采样的图像
self.y = T.ivector('y')  # the labels are presented as 1D vector
                                 # of [int] labels

下面的代码对于每一层进行扫描

  for i in xrange(self.n_layers):
            指定输入大小:第一层的时候输入是图像,否则就是对应隐含层
           指定输入层。

         根据  输入和输出层大小、输入图像 就可以求出隐含层

         根据隐含层可以求出RBM层
          

posted @ 2014-04-10 11:55  I know you  阅读(2666)  评论(0编辑  收藏  举报