-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
An implementation via usage of MOJO models
- Loading branch information
Showing
13 changed files
with
451 additions
and
82 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
ml/src/main/java/org/apache/spark/ml/h2o/features/H2OTargetEncoderHoldoutStrategy.java
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,23 @@ | ||
/* | ||
* 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.h2o.features; | ||
|
||
public enum H2OTargetEncoderHoldoutStrategy { | ||
LeaveOneOut, | ||
KFold, | ||
None | ||
} |
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
24 changes: 24 additions & 0 deletions
24
ml/src/main/scala/org/apache/spark/ml/h2o/models/H2OMOJOWritable.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,24 @@ | ||
/* | ||
* 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.h2o.models | ||
|
||
import org.apache.spark.ml.param.Params | ||
import org.apache.spark.ml.util.{MLWritable, MLWriter} | ||
|
||
trait H2OMOJOWritable extends MLWritable with Params with HasMojoData { | ||
override def write: MLWriter = new H2OMOJOWriter(this, getMojoData()) | ||
} |
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
71 changes: 71 additions & 0 deletions
71
ml/src/main/scala/org/apache/spark/ml/h2o/models/H2OTargetEncoderMojoModel.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,71 @@ | ||
/* | ||
* 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.h2o.models | ||
|
||
import hex.genmodel.algos.targetencoder.TargetEncoderMojoModel | ||
import org.apache.spark.h2o.converters.RowConverter | ||
import org.apache.spark.h2o.utils.H2OSchemaUtils | ||
import org.apache.spark.ml.Model | ||
import org.apache.spark.ml.h2o.param.H2OTargetEncoderParams | ||
import org.apache.spark.ml.param.ParamMap | ||
import org.apache.spark.sql.{DataFrame, Dataset, Row} | ||
import org.apache.spark.sql.functions._ | ||
import org.apache.spark.ml.util.Identifiable | ||
import water.support.ModelSerializationSupport | ||
|
||
class H2OTargetEncoderMojoModel(override val uid: String) extends Model[H2OTargetEncoderMojoModel] | ||
with H2OMOJOWritable with H2OTargetEncoderParams { | ||
|
||
def this() = this(Identifiable.randomUID("H2OTargetEncoderMojoModel")) | ||
|
||
override def transform(dataset: Dataset[_]): DataFrame = { | ||
val outputCols = getOutputCols() | ||
val udfWrapper = H2OTargetEncoderMojoUdfWrapper(getMojoData(), getOutputCols()) | ||
val outputColumnName = this.getClass.getSimpleName + "_output" | ||
val flattenedDF = H2OSchemaUtils.flattenDataFrame(dataset.toDF()) | ||
val relevantColumnNames = flattenedDF.columns.intersect(getInputCols()) | ||
val args = relevantColumnNames.map(flattenedDF(_)) | ||
flattenedDF | ||
.withColumn(outputColumnName, udfWrapper.mojoUdf(struct(args: _*))) | ||
.withColumns(outputCols, outputCols.zipWithIndex.map{ case (c, i) => col(outputColumnName)(i) as c }) | ||
.drop(outputColumnName) | ||
} | ||
|
||
override def copy(extra: ParamMap): H2OTargetEncoderMojoModel = defaultCopy(extra) | ||
|
||
} | ||
|
||
/** | ||
* The class holds all necessary dependencies of udf that needs to be serialized. | ||
*/ | ||
case class H2OTargetEncoderMojoUdfWrapper(mojoData: Array[Byte], outputCols: Array[String]) { | ||
@transient private lazy val mojoModel = ModelSerializationSupport | ||
.getMojoModel(mojoData) | ||
.asInstanceOf[TargetEncoderMojoModel] | ||
|
||
val mojoUdf = udf[Array[Option[Double]], Row] { r: Row => | ||
val inputRowData = RowConverter.toH2ORowData(r) | ||
try { | ||
val outputRawData = mojoModel.transform0(inputRowData) | ||
outputCols.map(c => Option(outputRawData.get(c).asInstanceOf[Double])) | ||
} catch { | ||
case _: Throwable => outputCols.map(_ => None) | ||
} | ||
} | ||
} | ||
|
||
object H2OTargetEncoderMojoModel extends H2OMOJOReadable[H2OTargetEncoderMojoModel] |
47 changes: 0 additions & 47 deletions
47
ml/src/main/scala/org/apache/spark/ml/h2o/models/H2OTargetEncoderTrainingModel.scala
This file was deleted.
Oops, something went wrong.
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
36 changes: 36 additions & 0 deletions
36
ml/src/main/scala/org/apache/spark/ml/h2o/param/CaseClassParam.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,36 @@ | ||
/* | ||
* 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.h2o.param | ||
|
||
import org.apache.spark.ml.param.{Param, Params} | ||
import org.json4s._ | ||
import org.json4s.jackson.Serialization.{read, write} | ||
|
||
class CaseClassParam[T <: AnyRef with Product : Manifest](parent: Params, name: String, doc: String, isValid: T => Boolean) | ||
extends Param[T](parent, name, doc, isValid) { | ||
|
||
def this(parent: Params, name: String, doc: String) = this(parent, name, doc, _ => true) | ||
|
||
@transient private implicit val formats = DefaultFormats | ||
|
||
override def jsonEncode(value: T): String = write[T](value) | ||
|
||
override def jsonDecode(json: String): T = { | ||
if (json == null) null.asInstanceOf[T] else read[T](json) | ||
} | ||
} |
Oops, something went wrong.