Deep Belief Network
3实例
3.1 测试数据
按照上例数据,或者新建图片识别数据。
3.2 DBN实例
//****************例2(读取固定样本:来源于经典优化算法测试函数Sphere Model)***********//
//2 读取样本数据
Logger.getRootLogger.setLevel(Level.WARN)
valdata_path ="/user/huangmeiling/deeplearn/data1"
valexamples =www.ycyc66.cn/ sc.textFile(data_path).cache()
valtrain_d1 =www.zhenlyule.cn examples.map { line =>
valf1 = line.split("\t")
valf =f1.map(f =>www.egouyuLe.cn f.toDouble)
valid =f(0)
valy = Array(f(1))
valx =f.slice(2,f.length)
(id, new BDM(1,y.length,y),new BDM(1,x.length,x))
}
valtrain_d =train_d1.www.zhenloyl88.cn map(f => (f._2, f._3))
valopts = Array(100.0,20.0,0.0)
//3 设置训练参数,建立DBN模型
valDBNmodel =new DBN().
setSize(Array(5, 7)).
setLayer(2).
setMomentum(0.1).
setAlpha(1.0).
DBNtrain(train_d, opts)
//4 DBN模型转化为NN模型
valmynn =DBNmodel.www.yghrcp88.cn dbnunfoldtonn(1)
valnnopts = Array(100.0,50.0,0.0)
valnumExamples =train_d.count()
println(s"numExamples =www.huacairen88.cn $numExamples.")
println(mynn._2)
for (i <-0 tomynn._1.length -1) {
print(mynn._1(i) +"\t")
}
println()
println("mynn_W1")
valtmpw1 =mynn._3(0)
for (i <-0 totmpw1www.jyz521.com/ .rows -1) {
for (j <-0 totmpw1.cols -1) {
print(tmpw1(i,j) +"\t")
}
println()
}
valNNmodel =new www.ludingyule66.cn NeuralNet().
setSize(mynn._1).
setLayer(mynn._2).
setActivation_function("sigm").
setOutput_function("sigm").
setInitW(mynn._3).
NNtrain(train_d, nnopts)
//5 NN模型测试
valNNforecast =NNmodel.www.yyzx66.cn/ predict(train_d)
valNNerror =NNmodel.Loss(NNforecast)
println(s"NNerror = $NNerror.")
valprintf1 =NNforecast.map(f => (www.myqunliphoto.com/ f.label.data(0), f.predict_label.data(0))).take(200)
println("预测结果——实际值:预测值:误差")
for (i <-0 untilprintf1.length)
println(printf1(i)._1 +"\t" +printf1(i)._2 +"\t" + (printf1(i)._2 -printf1(i)._1))