Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one or more
0003  * contributor license agreements.  See the NOTICE file distributed with
0004  * this work for additional information regarding copyright ownership.
0005  * The ASF licenses this file to You under the Apache License, Version 2.0
0006  * (the "License"); you may not use this file except in compliance with
0007  * the License.  You may obtain a copy of the License at
0008  *
0009  *    http://www.apache.org/licenses/LICENSE-2.0
0010  *
0011  * Unless required by applicable law or agreed to in writing, software
0012  * distributed under the License is distributed on an "AS IS" BASIS,
0013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0014  * See the License for the specific language governing permissions and
0015  * limitations under the License.
0016  */
0017 
0018 package org.apache.spark.examples.ml;
0019 
0020 // $example on$
0021 import org.apache.spark.sql.Dataset;
0022 import org.apache.spark.sql.Row;
0023 import org.apache.spark.sql.SparkSession;
0024 import org.apache.spark.ml.classification.MultilayerPerceptronClassificationModel;
0025 import org.apache.spark.ml.classification.MultilayerPerceptronClassifier;
0026 import org.apache.spark.ml.evaluation.MulticlassClassificationEvaluator;
0027 // $example off$
0028 
0029 /**
0030  * An example for Multilayer Perceptron Classification.
0031  */
0032 public class JavaMultilayerPerceptronClassifierExample {
0033 
0034   public static void main(String[] args) {
0035     SparkSession spark = SparkSession
0036       .builder()
0037       .appName("JavaMultilayerPerceptronClassifierExample")
0038       .getOrCreate();
0039 
0040     // $example on$
0041     // Load training data
0042     String path = "data/mllib/sample_multiclass_classification_data.txt";
0043     Dataset<Row> dataFrame = spark.read().format("libsvm").load(path);
0044 
0045     // Split the data into train and test
0046     Dataset<Row>[] splits = dataFrame.randomSplit(new double[]{0.6, 0.4}, 1234L);
0047     Dataset<Row> train = splits[0];
0048     Dataset<Row> test = splits[1];
0049 
0050     // specify layers for the neural network:
0051     // input layer of size 4 (features), two intermediate of size 5 and 4
0052     // and output of size 3 (classes)
0053     int[] layers = new int[] {4, 5, 4, 3};
0054 
0055     // create the trainer and set its parameters
0056     MultilayerPerceptronClassifier trainer = new MultilayerPerceptronClassifier()
0057       .setLayers(layers)
0058       .setBlockSize(128)
0059       .setSeed(1234L)
0060       .setMaxIter(100);
0061 
0062     // train the model
0063     MultilayerPerceptronClassificationModel model = trainer.fit(train);
0064 
0065     // compute accuracy on the test set
0066     Dataset<Row> result = model.transform(test);
0067     Dataset<Row> predictionAndLabels = result.select("prediction", "label");
0068     MulticlassClassificationEvaluator evaluator = new MulticlassClassificationEvaluator()
0069       .setMetricName("accuracy");
0070 
0071     System.out.println("Test set accuracy = " + evaluator.evaluate(predictionAndLabels));
0072     // $example off$
0073 
0074     spark.stop();
0075   }
0076 }