forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-16445][MLLIB][SPARKR] Multilayer Perceptron Classifier wrapper…
… in SparkR https://issues.apache.org/jira/browse/SPARK-16445 ## What changes were proposed in this pull request? Create Multilayer Perceptron Classifier wrapper in SparkR ## How was this patch tested? Tested manually on local machine Author: Xin Ren <[email protected]> Closes apache#14447 from keypointt/SPARK-16445.
- Loading branch information
Showing
6 changed files
with
293 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
mllib/src/main/scala/org/apache/spark/ml/r/MultilayerPerceptronClassifierWrapper.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.spark.ml.r | ||
|
||
import org.apache.hadoop.fs.Path | ||
import org.json4s._ | ||
import org.json4s.JsonDSL._ | ||
import org.json4s.jackson.JsonMethods._ | ||
|
||
import org.apache.spark.ml.{Pipeline, PipelineModel} | ||
import org.apache.spark.ml.classification.{MultilayerPerceptronClassificationModel, MultilayerPerceptronClassifier} | ||
import org.apache.spark.ml.util.{MLReadable, MLReader, MLWritable, MLWriter} | ||
import org.apache.spark.sql.{DataFrame, Dataset} | ||
|
||
private[r] class MultilayerPerceptronClassifierWrapper private ( | ||
val pipeline: PipelineModel, | ||
val labelCount: Long, | ||
val layers: Array[Int], | ||
val weights: Array[Double] | ||
) extends MLWritable { | ||
|
||
def transform(dataset: Dataset[_]): DataFrame = { | ||
pipeline.transform(dataset) | ||
} | ||
|
||
/** | ||
* Returns an [[MLWriter]] instance for this ML instance. | ||
*/ | ||
override def write: MLWriter = | ||
new MultilayerPerceptronClassifierWrapper.MultilayerPerceptronClassifierWrapperWriter(this) | ||
} | ||
|
||
private[r] object MultilayerPerceptronClassifierWrapper | ||
extends MLReadable[MultilayerPerceptronClassifierWrapper] { | ||
|
||
val PREDICTED_LABEL_COL = "prediction" | ||
|
||
def fit( | ||
data: DataFrame, | ||
blockSize: Int, | ||
layers: Array[Double], | ||
solver: String, | ||
maxIter: Int, | ||
tol: Double, | ||
stepSize: Double, | ||
seed: Int | ||
): MultilayerPerceptronClassifierWrapper = { | ||
// get labels and feature names from output schema | ||
val schema = data.schema | ||
|
||
// assemble and fit the pipeline | ||
val mlp = new MultilayerPerceptronClassifier() | ||
.setLayers(layers.map(_.toInt)) | ||
.setBlockSize(blockSize) | ||
.setSolver(solver) | ||
.setMaxIter(maxIter) | ||
.setTol(tol) | ||
.setStepSize(stepSize) | ||
.setSeed(seed) | ||
.setPredictionCol(PREDICTED_LABEL_COL) | ||
val pipeline = new Pipeline() | ||
.setStages(Array(mlp)) | ||
.fit(data) | ||
|
||
val multilayerPerceptronClassificationModel: MultilayerPerceptronClassificationModel = | ||
pipeline.stages.head.asInstanceOf[MultilayerPerceptronClassificationModel] | ||
|
||
val weights = multilayerPerceptronClassificationModel.weights.toArray | ||
val layersFromPipeline = multilayerPerceptronClassificationModel.layers | ||
val labelCount = data.select("label").distinct().count() | ||
|
||
new MultilayerPerceptronClassifierWrapper(pipeline, labelCount, layersFromPipeline, weights) | ||
} | ||
|
||
/** | ||
* Returns an [[MLReader]] instance for this class. | ||
*/ | ||
override def read: MLReader[MultilayerPerceptronClassifierWrapper] = | ||
new MultilayerPerceptronClassifierWrapperReader | ||
|
||
override def load(path: String): MultilayerPerceptronClassifierWrapper = super.load(path) | ||
|
||
class MultilayerPerceptronClassifierWrapperReader | ||
extends MLReader[MultilayerPerceptronClassifierWrapper]{ | ||
|
||
override def load(path: String): MultilayerPerceptronClassifierWrapper = { | ||
implicit val format = DefaultFormats | ||
val rMetadataPath = new Path(path, "rMetadata").toString | ||
val pipelinePath = new Path(path, "pipeline").toString | ||
|
||
val rMetadataStr = sc.textFile(rMetadataPath, 1).first() | ||
val rMetadata = parse(rMetadataStr) | ||
val labelCount = (rMetadata \ "labelCount").extract[Long] | ||
val layers = (rMetadata \ "layers").extract[Array[Int]] | ||
val weights = (rMetadata \ "weights").extract[Array[Double]] | ||
|
||
val pipeline = PipelineModel.load(pipelinePath) | ||
new MultilayerPerceptronClassifierWrapper(pipeline, labelCount, layers, weights) | ||
} | ||
} | ||
|
||
class MultilayerPerceptronClassifierWrapperWriter(instance: MultilayerPerceptronClassifierWrapper) | ||
extends MLWriter { | ||
|
||
override protected def saveImpl(path: String): Unit = { | ||
val rMetadataPath = new Path(path, "rMetadata").toString | ||
val pipelinePath = new Path(path, "pipeline").toString | ||
|
||
val rMetadata = ("class" -> instance.getClass.getName) ~ | ||
("labelCount" -> instance.labelCount) ~ | ||
("layers" -> instance.layers.toSeq) ~ | ||
("weights" -> instance.weights.toArray.toSeq) | ||
val rMetadataJson: String = compact(render(rMetadata)) | ||
sc.parallelize(Seq(rMetadataJson), 1).saveAsTextFile(rMetadataPath) | ||
|
||
instance.pipeline.save(pipelinePath) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters