MLlib--多层感知机(MLP)算法原理及Spark MLlib调用实例(Scala/Java/Python)

来源:http://blog.csdn.net/liulingyuan6/article/details/53432429


多层感知机

算法简介:

        多层感知机是基于反向人工神经网络(feedforwardartificial neural network)。多层感知机含有多层节点,每层节点与网络的下一层节点完全连接。输入层的节点代表输入数据,其他层的节点通过将输入数据与层上节点的权重w以及偏差b线性组合且应用一个激活函数,得到该层输出。多层感知机通过方向传播来学习模型,其中我们使用逻辑损失函数以及L-BFGS。K+1层多层感知机分类器可以写成矩阵形式如下:

 

中间层节点使用sigmoid方程:

 

输出层使用softmax方程:

 

输出层中N代表类别数目。

参数:

featuresCol:

类型:字符串型。

含义:特征列名。

labelCol:

类型:字符串型。

含义:标签列名。

layers:

类型:整数数组型。

含义:层规模,包括输入规模以及输出规模。

maxIter:

类型:整数型。

含义:迭代次数(>=0)。

predictionCol:

类型:字符串型。

含义:预测结果列名。

seed:

类型:长整型。

含义:随机种子。

stepSize:

类型:双精度型。

含义:每次迭代优化步长。

tol:

类型:双精度型。

含义:迭代算法的收敛性。

示例:

Scala:

[plain]  view plain  copy
  1. import org.apache.spark.ml.classification.MultilayerPerceptronClassifier  
  2. import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator  
  3.   
  4. // Load the data stored in LIBSVM format as a DataFrame.  
  5. val data = spark.read.format("libsvm")  
  6.   .load("data/mllib/sample_multiclass_classification_data.txt")  
  7. // Split the data into train and test  
  8. val splits = data.randomSplit(Array(0.6, 0.4), seed = 1234L)  
  9. val train = splits(0)  
  10. val test = splits(1)  
  11. // specify layers for the neural network:  
  12. // input layer of size 4 (features), two intermediate of size 5 and 4  
  13. // and output of size 3 (classes)  
  14. val layers = Array[Int](4, 5, 4, 3)  
  15. // create the trainer and set its parameters  
  16. val trainer = new MultilayerPerceptronClassifier()  
  17.   .setLayers(layers)  
  18.   .setBlockSize(128)  
  19.   .setSeed(1234L)  
  20.   .setMaxIter(100)  
  21. // train the model  
  22. val model = trainer.fit(train)  
  23. // compute accuracy on the test set  
  24. val result = model.transform(test)  
  25. val predictionAndLabels = result.select("prediction", "label")  
  26. val evaluator = new MulticlassClassificationEvaluator()  
  27.   .setMetricName("accuracy")  
  28. println("Accuracy: " + evaluator.evaluate(predictionAndLabels))  

Java:

[java]  view plain  copy
  1. import org.apache.spark.sql.Dataset;  
  2. import org.apache.spark.sql.Row;  
  3. import org.apache.spark.sql.SparkSession;  
  4. import org.apache.spark.ml.classification.MultilayerPerceptronClassificationModel;  
  5. import org.apache.spark.ml.classification.MultilayerPerceptronClassifier;  
  6. import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator;  
  7.   
  8. // Load training data  
  9. String path = "data/mllib/sample_multiclass_classification_data.txt";  
  10. Dataset<Row> dataFrame = spark.read().format("libsvm").load(path);  
  11. // Split the data into train and test  
  12. Dataset<Row>[] splits = dataFrame.randomSplit(new double[]{0.60.4}, 1234L);  
  13. Dataset<Row> train = splits[0];  
  14. Dataset<Row> test = splits[1];  
  15. // specify layers for the neural network:  
  16. // input layer of size 4 (features), two intermediate of size 5 and 4  
  17. // and output of size 3 (classes)  
  18. int[] layers = new int[] {4543};  
  19. // create the trainer and set its parameters  
  20. MultilayerPerceptronClassifier trainer = new MultilayerPerceptronClassifier()  
  21.   .setLayers(layers)  
  22.   .setBlockSize(128)  
  23.   .setSeed(1234L)  
  24.   .setMaxIter(100);  
  25. // train the model  
  26. MultilayerPerceptronClassificationModel model = trainer.fit(train);  
  27. // compute accuracy on the test set  
  28. Dataset<Row> result = model.transform(test);  
  29. Dataset<Row> predictionAndLabels = result.select("prediction""label");  
  30. MulticlassClassificationEvaluator evaluator = new MulticlassClassificationEvaluator()  
  31.   .setMetricName("accuracy");  
  32. System.out.println("Accuracy = " + evaluator.evaluate(predictionAndLabels));  

Python:

[python]  view plain  copy
  1. from pyspark.ml.classification import MultilayerPerceptronClassifier  
  2. from pyspark.ml.evaluation import MulticlassClassificationEvaluator  
  3.   
  4. # Load training data  
  5. data = spark.read.format("libsvm")\  
  6.     .load("data/mllib/sample_multiclass_classification_data.txt")  
  7. # Split the data into train and test  
  8. splits = data.randomSplit([0.60.4], 1234)  
  9. train = splits[0]  
  10. test = splits[1]  
  11. # specify layers for the neural network:  
  12. # input layer of size 4 (features), two intermediate of size 5 and 4  
  13. # and output of size 3 (classes)  
  14. layers = [4543]  
  15. # create the trainer and set its parameters  
  16. trainer = MultilayerPerceptronClassifier(maxIter=100, layers=layers, blockSize=128, seed=1234)  
  17. # train the model  
  18. model = trainer.fit(train)  
  19. # compute accuracy on the test set  
  20. result = model.transform(test)  
  21. predictionAndLabels = result.select("prediction""label")  
  22. evaluator = MulticlassClassificationEvaluator(metricName="accuracy")  
  23. print("Accuracy: " + str(evaluator.evaluate(predictionAndLabels)))  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值