【spark】采用MultilayerPerceptron对MNIST的0-9数字进行识别

:神经网络介绍

http://ufldl.stanford.edu/tutorial/supervised/MultiLayerNeuralNetworks/

:由于只采用一种(28 * 28, 100, 50, 10)层进行训练,效果不是很好

package com.bbw5.ml.spark

import org.apache.spark.ml.tuning.ParamGridBuilder
import org.apache.spark.SparkContext
import org.apache.spark.sql.SQLContext
import org.apache.spark.ml.classification.LogisticRegression
import org.apache.spark.ml.evaluation.BinaryClassificationEvaluator
import org.apache.spark.ml.tuning.TrainValidationSplit
import com.bbw5.ml.spark.data.MNISTData
import org.apache.spark.ml.classification.MultilayerPerceptronClassifier
import org.apache.spark.ml.tuning.CrossValidator
import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator
import org.apache.spark.SparkConf
import org.apache.spark.ml.param.IntArrayParam
import scala.collection.mutable.ArrayBuffer
import org.apache.spark.ml.classification.MultilayerPerceptronClassificationModel

object MultilayerPerceptron4MNIST {
  def main(args: Array[String]) {
    val sparkConf = new SparkConf().setAppName("MultilayerPerceptron4MNIST")
    val sc = new SparkContext(sparkConf)
    val sqlContext = new org.apache.spark.sql.SQLContext(sc)
    
    tvSplit(sc,sqlContext)
  }

  def tvSplit(sc: SparkContext, sqlContext: SQLContext) {
    val dataDir = "I:/DM-dataset/MNIST/"
    import sqlContext.implicits._
    //取部分数据进行测试,不然内存不够
    val training = sc.parallelize(MNISTData.loadTrainData(dataDir, 10).toSeq, 4).
      randomSplit(Array(0.1, 0.9), seed = 1234L)(0).toDF("label", "features").cache()
    val test = sc.parallelize(MNISTData.loadTestData(dataDir, 10).toSeq, 4).
      randomSplit(Array(0.1, 0.9), seed = 1234L)(0).toDF("label", "features").cache()

    val mpc = new MultilayerPerceptronClassifier()

    val paramGrid = new ParamGridBuilder().
      addGrid[Array[Int]](mpc.layers, ArrayBuffer(Array[Int](28 * 28, 100, 50, 10))).
      addGrid(mpc.blockSize, Array(128)).addGrid(mpc.seed, Array(1234L)).addGrid(mpc.maxIter, Array(100)).build()

    val cv = new CrossValidator().setEstimator(mpc).
      setEvaluator(new MulticlassClassificationEvaluator).
      setEstimatorParamMaps(paramGrid).setNumFolds(3)

    // Run train validation split, and choose the best set of parameters.
    val model = cv.fit(training)
    sc.parallelize(Seq(model)).saveAsObjectFile("D:/Develop/Model/MNIST-MPC-" + System.currentTimeMillis())

    val bestModel = model.bestModel.asInstanceOf[MultilayerPerceptronClassificationModel]
    println("model param:\n" + bestModel.extractParamMap)

    // Make predictions on test data. model is the model with combination of parameters
    // that performed best.
    val testDF = model.transform(test)
    testDF.select("label", "prediction").show()
    testDF.groupBy("label", "prediction").count().show()

    val predictionAndLabels = testDF.select("prediction", "label")
    val evaluator = new MulticlassClassificationEvaluator().setMetricName("precision")
    //0.231
    println("Precision:" + evaluator.evaluate(predictionAndLabels))
  }
}


好的,下面我将向您介绍如何使用Weka自带的数据集进行神经网络结果分析。 1. 打开Weka 首先,打开Weka软件。 2. 选择数据集 在Weka主界面上,选择“Explorer”选项卡。在“Preprocess”面板中,选择“Open file”按钮打开数据集。在弹出的文件选择窗口中,找到Weka自带的数据集文件,例如“iris.arff”或“diabetes.arff”。 3. 选择神经网络算法 在“Classify”选项卡中,选择“Functions”子选项卡。在算法列表中,选择“MultilayerPerceptron”算法。 4. 调整神经网络参数 在“MultilayerPerceptron”算法界面中,您可以调整神经网络的参数,例如学习率、动量、隐藏层数和每层的节点数等。对于Weka自带的数据集,使用默认参数即可。 5. 训练神经网络 单击“Start”按钮,Weka将会自动将数据集拆分为训练集和测试集,并使用训练集训练神经网络。 6. 分析神经网络结果 训练完成后,Weka会显示混淆矩阵、分类准确率等性能指标。您可以根据这些指标对神经网络模型进行分析和优化。 以“iris.arff”为例,Weka训练完成后会显示以下结果: ``` === Summary === Correctly Classified Instances 146 97.3333 % Incorrectly Classified Instances 4 2.6667 % Kappa statistic 0.96 Mean absolute error 0.0357 Root mean squared error 0.189 Relative absolute error 7.6009 % Root relative squared error 40.5336 % Total Number of Instances 150 === Confusion Matrix === a b c <-- classified as 50 0 0 | a = Iris-setosa 0 47 3 | b = Iris-versicolor 0 1 49 | c = Iris-virginica ``` 从以上结果中可以看出,使用Weka自带的“iris.arff”数据集训练的神经网络模型,在测试集上的分类准确率为97.3333%。混淆矩阵显示,对于三个类别,分类效果都很好,仅有4个实例分类错误。 希望这些信息对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值