本文主要包含Spark MLlib Deep Learning Deep Belief Network (深度学习-深度信念网络)2.1,mllibbelief等服务器相关知识,网友希望可以进行参考
Spark MLlib Deep Learning Deep Belief Network (深度学习-深度信念网络)2.1,mllibbelief
Spark MLlib Deep Learning Deep Belief Network (深度学习-深度信念网络)2.1
http://blog.csdn.net/sunbow0
Spark MLlib Deep Learning工具箱,是根据现有深度学习教程《UFLDL教程》中的算法,在SparkMLlib中的实现。具体Spark MLlib Deep Learning(深度学习)目录结构:
第一章Neural Net(NN)
1、源码
2、源码解析
3、实例
第二章Deep Belief Nets(DBNs)
1、源码
2、源码解析
3、实例
第三章Convolution Neural Network(CNN)
第四章 Stacked Auto-Encoders(SAE)
第五章CAE
第二章Deep Belief Network (深度信念网络)
1源码
目前Spark MLlib Deep Learning工具箱源码的github地址为:
https://github.com/sunbow1/SparkMLlibDeepLearn
1.1 DBN代码
package DBN
import org.apache.spark._
import org.apache.spark.SparkContext._
import org.apache.spark.rdd.RDD
import org.apache.spark.Logging
import org.apache.spark.mllib.regression.LabeledPoint
import org.apache.spark.mllib.linalg._
import org.apache.spark.mllib.linalg.distributed.RowMatrix
import breeze.linalg.{
Matrix => BM,
CSCMatrix => BSM,
DenseMatrix => BDM,
Vector => BV,
DenseVector => BDV,
SparseVector => BSV,
axpy => brzAxpy,
svd => brzSvd
}
import breeze.numerics.{
exp => Bexp,
tanh => Btanh
}
import scala.collection.mutable.ArrayBuffer
import java.util.Random
import scala.math._
/**
* W:权重
* b:偏置
* c:偏置
*/
case class DBNweight(
W: BDM[Double],
vW: BDM[Double],
b: BDM[Double],
vb: BDM[Double],
c: BDM[Double],
vc: BDM[Double]) extends Serializable
/**
* 配置参数
*/
case class DBNConfig(
size: Array[Int],
layer: Int,
momentum: Double,
alpha: Double) extends Serializable
/**
* DBN(Deep Belief Network)
*/
class DBN(
private var size: Array[Int],
private var layer: Int,
private var momentum: Double,
private var alpha: Double) extends Serializable with Logging {
// var size=Array(5, 10, 10)
// var layer=3
// var momentum=0.0
// var alpha=1.0
/**
* size = architecture; 网络结构
* layer = numel(nn.size); 网络层数
* momentum = 0.0; Momentum
* alpha = 1.0; alpha
*/
def this() = this(DBN.Architecture, 3, 0.0, 1.0)
/** 设置神经网络结构. Default: [10, 5, 1]. */
def setSize(size: Array[Int]): this.type = {
this.size = size
this
}
/** 设置神经网络层数据. Default: 3. */
def setLayer(layer: Int): this.type = {
this.layer = layer
this
}
/** 设置Momentum. Default: 0.0. */
def setMomentum(momentum: Double): this.type = {
this.momentum = momentum
this
}
/** 设置alpha. Default: 1. */
def setAlpha(alpha: Double): this.type = {
this.alpha = alpha
this
}
/**
* 深度信念网络(Deep Belief Network)
* 运行训练DBNtrain
*/
def DBNtrain(train_d: RDD[(BDM[Double], BDM[Double])], opts: Array[Double]): DBNModel = {
// 参数配置 广播配置
val sc = train_d.sparkContext
val dbnconfig = DBNConfig(size, layer, momentum, alpha)
// 初始化权重
var dbn_W = DBN.InitialW(size)
var dbn_vW = DBN.InitialvW(size)
var dbn_b = DBN.Initialb(size)
var dbn_vb = DBN.Initialvb(size)
var dbn_c = DBN.Initialc(size)
var dbn_vc = DBN.Initialvc(size)
// 训练第1层
printf("Training Level: %d.\n", 1)
val weight0 = new DBNweight(dbn_W(0), dbn_vW(0), dbn_b(0), dbn_vb(0), dbn_c(0), dbn_vc(0))
val weight1 = RBMtrain(train_d, opts, dbnconfig, weight0)
dbn_W(0) = weight1.W
dbn_vW(0) = weight1.vW
dbn_b(0) = weight1.b
dbn_vb(0) = weight1.vb
dbn_c(0) = weight1.c
dbn_vc(0) = weight1.vc
// 打印权重
printf("dbn_W%d.\n", 1)
val tmpw0 = dbn_W(0)
for (i <- 0 to tmpw0.rows - 1) {
for (j <- 0 to tmpw0.cols - 1) {
print(tmpw0(i, j) + "\t")
}
println()
}
// 训练第2层 至 n层
for (i <- 2 to dbnconfig.layer - 1) {
// 前向计算x
// x = sigm(repmat(rbm.c', size(x, 1), 1) + x * rbm.W');
printf("Training Level: %d.\n", i)
val tmp_bc_w = sc.broadcast(dbn_W(i - 2))
val tmp_bc_c = sc.broadcast(dbn_c(i - 2))
val train_d2 = train_d.map { f =>
val lable = f._1
val x = f._2
val x2 = DBN.sigm(x * tmp_bc_w.value.t + tmp_bc_c.value.t)
(lable, x2)
}
// 训练第i层
val weighti = new DBNweight(dbn_W(i - 1), dbn_vW(i - 1), dbn_b(i - 1), dbn_vb(i - 1), dbn_c(i - 1), dbn_vc(i - 1))
val weight2 = RBMtrain(train_d2, opts, dbnconfig, weighti)
dbn_W(i - 1) = weight2.W
dbn_vW(i - 1) = weight2.vW
dbn_b(i - 1) = weight2.b
dbn_vb(i - 1) = weight2.vb
dbn_c(i - 1) = weight2.c
dbn_vc(i - 1) = weight2.vc
// 打印权重
printf("dbn_W%d.\n", i)
val tmpw1 = dbn_W(i - 1)
for (i <- 0 to tmpw1.rows - 1) {
for (j <- 0 to tmpw1.cols - 1) {
print(tmpw1(i, j) + "\t")
}
println()
}
}
new DBNModel(dbnconfig, dbn_W, dbn_b, dbn_c)
}
/**
* 深度信念网络(Deep Belief Network)
* 每一层神经网络进行训练rbmtrain
*/
def RBMtrain(train_t: RDD[(BDM[Double], BDM[Double])],
opts: Array[Double],
dbnconfig: DBNConfig,
weight: DBNweight): DBNweight = {
val sc = train_t.sparkContext
var StartTime = System.currentTimeMillis()
var EndTime = System.currentTimeMillis()
// 权重参数变量
var rbm_W = weight.W
var rbm_vW = weight.vW
var rbm_b = weight.b
var rbm_vb = weight.vb
var rbm_c = weight.c
var rbm_vc = weight.vc
// 广播参数
val bc_config = sc.broadcast(dbnconfig)
// 训练样本数量
val m = train_t.count
// 计算batch的数量
val batchsize = opts(0).toInt
val numepochs = opts(1).toInt
val numbatches = (m / batchsize).toInt
// numepochs是循环的次数
for (i <- 1 to numepochs) {
StartTime = System.currentTimeMillis()
val splitW2 = Array.fill(numbatches)(1.0 / numbatches)
var err = 0.0
// 根据分组权重,随机划分每组样本数据
for (l <- 1 to numbatches) {
// 1 广播权重参数
val bc_rbm_W = sc.broadcast(rbm_W)
val bc_rbm_vW = sc.broadcast(rbm_vW)
val bc_rbm_b = sc.broadcast(rbm_b)
val bc_rbm_vb = sc.broadcast(rbm_vb)
val bc_rbm_c = sc.broadcast(rbm_c)
val bc_rbm_vc = sc.broadcast(rbm_vc)
// // 打印权重
// println(i + "\t" + l)
// val tmpw0 = bc_rbm_W.value
// for (i <- 0 to tmpw0.rows - 1) {
// for (j <- 0 to tmpw0.cols - 1) {

