欢迎fork我的github:https://github.com/zhaoyu611/DeepLearningTutorialForChinese
最近在学习Git,所以正好趁这个机会,把学习到的知识实践一下~ 看完DeepLearning的原理,有了大体的了解,但是对于theano的代码,还是自己撸一遍印象更深 所以照着deeplearning.net上的代码,重新写了一遍,注释部分是原文翻译和自己的理解。 感兴趣的小伙伴可以一起完成这个工作哦~ 有问题欢迎联系我 Email: zhaoyuafeu@gmail.com QQ: 3062984605
#-*-coding: utf-8 -*-
__author__ = 'Administrator'
import cPickle
import gzip
import sys
import time
import numpy
import os
import theano
import theano.tensor as T
from theano.tensor.shared_randomstreams import RandomStreams
from logistic_sgd import load_data,LogisticRegression
from mlp import HiddenLayer
from rbm import RBM
class DBN(object):
"""
深度置信网络
深度置信网络是将若干RBMs堆叠组成的。第i层RBM的隐层是第i+1层的输入。
第一层RBM的输入是网络的输入,最后一层RBM的隐层是网络的输出。当用于分类时,
DBN顶部添加一个logistic回归,变成了MLP。
"""
def __init__(self,numpy_rng,theano_rng=None,n_ins=784,
hidden_layers_sizes=[500,500],n_outs=10):
"""
该类可实现可变层数的DBN
:param numpy_rng: numpy.random.RandomState 用于初始化权重的numpy随机数
:param theano_rng: theano.tensor.shared_randomstreams.RandomStreams
如果输入为None
:param n_ins: int DBN输入量的维度
:param hidden_layers_size: list 隐层输入量的维度
:param n_outs: int 网络输出量的维度
:return:
"""
self.sigmoid_layers=[]
self.rbm_layers=[]
self.params=[]
self.n_layers=len(hidden_layers_sizes)
assert self.n_layers>0
if not theano_rng:
theano_rng=RandomStreams(numpy_rng.randint(2**30))
#设置符号变量
self.x=T.matrix('x')
self.y=T.ivector('y')
#DBN是一个MLP,中间层的权重是在不同的RBM之间共享的。
#首先构造DBN为一个深层多感知器。在构造每个sigmoid层时,
#同样构造RBM与之共享变量。在预训练阶段,需要训练三个RBM(同样改变MLP的权重,
#微调阶段,通过在MLP上随机梯度下降法完成DBN训练。
for i in xrange(self.n_layers):
#构造sigmoid层,
#对于第一层,输入量大小是网络的输入量大小
#对于其它层,输入量大小是下层隐层单元的数量
if i==0:
input_size=n_ins
else:
input_size