artificial neural network in spark MLLib
神经网络模型
每个node包含两种操作:线性变换(仿射变换)和激发函数(activation function)。

其中仿射变换是通用的,而激发函数可以很多种,如下图。

MLLib中实现ANN
使用两层(Layer)来对应模型中的一层:
- AffineLayer 仿射变换: output = W · input + b
- 如果是最后一层,使用
SoftmaxLayerWithCrossEntropyLoss或者SigmoidLayerWithSquaredError;如果是中间层,则使用functionalLayer(new SigmoidFunction()). 目前MLlib只支持sigmoid函数,实际上ReLU激发函数更普遍
BP算法计算Gradient的四个步骤:

对照BP算法的步骤,可以发现分隔成Affine和Activation的好处。BP1和BP2中的计算,不同的activation函数有不同的计算形式,将affine变换和activation函数解耦方便组合,进而方便形成各种类型的神经网络。
MLLib FeedForward Trainer
训练器重要模块如下:

ANN模型中每层对应AffineLayer + FunctionalLayerModel OR SofrmaxLayerModelWIthCrossEntropyLoss
每个LayerModel实现三个函数:eval, computePrevDelta, grad, 作为输出层的SoftmaxLayerModel有些特殊,额外具有LossFunction特性。
可验证affine+activation LayerModel的计算组合跟BP1-4一致。
AffineLayerModel (仿射变换层)
eval
\(\text{output} = W \cdot \text{input} + b\)computePrevDelta
\(prev\delta = W * \delta\)grad
$\dot{W} = input \cdot \delta^l / \text{data size} $
input is \(a^{l-1}\),前一层的激发函数输出
\(\dot{b} = \delta^l / \text{data size}\)
FunctionalLayerModel(activate function \(\sigma\))
作为affineModel的activation model,只影响prev\(\delta\) 的计算,grad不计算
eval
\(\text{output} = \sigma (\text{input})\)computePrevDelta
\(\delta :=\delta * \sigma'(\text{input})\)grad
pass
SoftmaxLayerModelWithCrossEntropyLoss
作为最后一层激发函数,这一层很特殊。
eval
计算参见手写公式。computePrevDelta
不计算grad
不计算loss
计算\(\delta^L\),公式推导参见手写公式,代码如下:
ApplyInPlace(output, target, delta, (o: Double, t: Double) => o - t)
返回loss
Softmax输出层的激发函数:
\(a^L_j = \frac{e^{z^L_j}}{\sum_k e^{z^L_k}}\)
计算BP1:\(\delta^L_j = a^L_j -y_j\)

训练mnist手写数字识别
import org.apache.spark.ml.classification.MultilayerPerceptronClassifier
import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator
import org.apache.spark.ml.linalg.Vectors
import org.apache.spark.sql.{DataFrame, Dataset, Row, SparkSession}
object ann extends App {
val spark = SparkSession
.builder
.appName("ANN for MNIST")
.master("local[3]")
.getOrCreate()
spark.sparkContext.setLogLevel("ERROR")
import spark.implicits._
// Load the data stored in text as a DataFrame.
val dataRdd: DataFrame= spark.sparkContext.textFile("handson-ml/data/train.csv")
.map {
line =>
val linesp = line.split(",")
val linespDouble = linesp.map(f => f.toDouble)
(linespDouble.head, Vectors.dense(linespDouble.takeRight(linespDouble.length - 1)))
}.toDF("label","features")
val data = dataRdd
// Split the data into train and test
val splits: Array[DataFrame] = data.randomSplit(Array(0.6, 0.4), seed = 1234L)
val train: Dataset[Row] = splits(0)
val test: Dataset[Row] = splits(1)
val layers = Array[Int](28*28, 300, 100, 10)
// create the trainer and set its parameters
val trainer = new MultilayerPerceptronClassifier()
.setLayers(layers)
.setBlockSize(128)
.setSeed(1234L)
.setMaxIter(100)
.setLabelCol("label")
.setFeaturesCol("features")
// train the model
val model = trainer.fit(train)
// compute accuracy on the test set
val result = model.transform(test)
val predictionAndLabels = result.select("prediction", "label")
val evaluator = new MulticlassClassificationEvaluator()
.setMetricName("accuracy")
println("Test set accuracy = " + evaluator.evaluate(predictionAndLabels))
}
后记
测试集结果精度为96.68%。实际上并不高,同样的数据集使用TensorFlow训练,activation function选择ReLU,同样使用Softmax作为输出,结果可以达到98%以上。Sigmoid函数容易带来vanishing gradients问题,导致学习曲线变平。
artificial neural network in spark MLLib的更多相关文章
- 人工神经网络 Artificial Neural Network
2017-12-18 23:42:33 一.什么是深度学习 深度学习(deep neural network)是机器学习的分支,是一种试图使用包含复杂结构或由多重非线性变换构成的多个处理层对数据进行高 ...
- 吴恩达深度学习第1课第4周-任意层人工神经网络(Artificial Neural Network,即ANN)(向量化)手写推导过程(我觉得已经很详细了)
学习了吴恩达老师深度学习工程师第一门课,受益匪浅,尤其是吴老师所用的符号系统,准确且易区分. 遵循吴老师的符号系统,我对任意层神经网络模型进行了详细的推导,形成笔记. 有人说推导任意层MLP很容易,我 ...
- Neural Network and Artificial Neural Network
神经网络的基本单元为神经元neuron,也称为process unit,可以做一些基本的运算操作. 人脑和动物大脑的发育,依赖于经验的积累和学习.神经网络就是一个用来仿照人脑进行学习的机器,其包含 ...
- What is “Neural Network”
Modern neuroscientists often discuss the brain as a type of computer. Neural networks aim to do the ...
- 论文笔记系列-Neural Network Search :A Survey
论文笔记系列-Neural Network Search :A Survey 论文 笔记 NAS automl survey review reinforcement learning Bayesia ...
- 机器学习之Artificial Neural Networks
人类通过模仿自然界中的生物,已经发明了很多东西,比如飞机,就是模仿鸟翼,但最终,这些东西会和原来的东西有些许差异,artificial neural networks (ANNs)就是模仿动物大脑的神 ...
- Spark MLlib Deep Learning Convolution Neural Network (深度学习-卷积神经网络)3.1
3.Spark MLlib Deep Learning Convolution Neural Network (深度学习-卷积神经网络)3.1 http://blog.csdn.net/sunbow0 ...
- Spark MLlib Deep Learning Convolution Neural Network (深度学习-卷积神经网络)3.2
3.Spark MLlib Deep Learning Convolution Neural Network(深度学习-卷积神经网络)3.2 http://blog.csdn.net/sunbow0 ...
- Spark MLlib Deep Learning Convolution Neural Network (深度学习-卷积神经网络)3.3
3.Spark MLlib Deep Learning Convolution Neural Network(深度学习-卷积神经网络)3.3 http://blog.csdn.net/sunbow0 ...
随机推荐
- Vue 全家桶介绍
Vue有著名的全家桶系列,包含了vue-router(http://router.vuejs.org),vuex(http://vuex.vuejs.org), vue-resource(https: ...
- 使用crf++
在example文件夹下存在4个使用crf的实例 1.在命令行执行 进入路径:./example/seg 执行:sh exec.sh 2. 在python中执行 进入路径:./python 执行:(1 ...
- 【WebService】WebService之CXF的拦截器(五)
CXF拦截器介绍 CXF拦截器是功能的主要实现单元,也是主要的扩展点,可以在不对核心模块进行修改的情况下,动态添加功能.当服务被调用时,会经过多个拦截器链(Interceptor Chain)处理,拦 ...
- canvas绘图实现浏览器等待效果
一:创建画布 <canvas width="600" height="600" id="canvas" style="bor ...
- css进阶篇
一.css的属性值 1)字体属性 font-size: 5px; /* 字体大小 */ font-size: 20px/50%/larger /* 字体的大小 */ font-family:'Luci ...
- 正则表达式,re模块
一.正则表达式 正则表达式 : 匹配字符串,一般用于爬取数据. 正则表达式查询网址 : http://tool.chinaz.com/regex/?qq-pf-to=pcqq.group 1.元字符( ...
- 824. Goat Latin
class Solution { public: string toGoatLatin(string S) { S.push_back(' '); //add a space that the loo ...
- LINUX系统下MySQL 压力测试工具super smack
摘要:1.源文件下载地址:http://vegan.net/tony/supersmack/2.安装:注意在编译时,可以先把对应的libmysqlclient.so.*拷贝到/usr/lib3.测试: ...
- 2019.01.20 bzoj2388: 旅行规划(分块+凸包)
传送门 分块好题. 题意:维护区间加,维护区间前缀和的最大值(前缀和指从1开始的). 思路: 考虑分块维护答案. 我们把每个点看成(i,sumi)(i,sum_i)(i,sumi)答案一定会在凸包上 ...
- 2018.10.30 NOIP模拟 字胡串(单调栈+容斥)
传送门 对于每个点,用单调栈求出它左右第一个比他大的位置. 然后对每个点O(logai)O(log_{a_i})O(logai)求出第一个拥有跟它不同二进制位的位置. 然后容斥一下就行了. 代码