0

我们有一个要求,我们想要生成我们模型的分数,其中一些随机值在 0-1 之间。

为此,我们希望有一个自定义转换器,它将在没有任何输入字段的情况下生成随机数。

那么我们可以在mleap中生成一个没有输入字段的转换器吗?

就像通常我们创建如下:

import ml.combust.mleap.core.Model
import ml.combust.mleap.core.types._

case class RandomNumberModel() extends Model {
  private val rnd = scala.util.Random

  def apply(): Double = rnd.nextFloat

  override def inputSchema: StructType = StructType("input" -> ScalarType.String).get

  override def outputSchema: StructType = StructType("output" -> ScalarType.Double ).get

}

如何使其作为输入模式无需放置?

4

1 回答 1

0

我从未尝试过,但考虑到我是如何实现具有多个输入字段的自定义转换器的......

package org.apache.spark.ml.feature.mleap

import ml.combust.mleap.core.Model
import ml.combust.mleap.core.types._
import org.apache.spark.ml.linalg._

case class PropertyGroupAggregatorBaseModel (props: Array[String],
                                        aggFunc: String) extends Model {
  val outputSize = props.size

  //having multiple inputs, you will have apply with a parameter Seq[Any]
  def apply(features: Seq[Any]): Vector = {
    val properties = features(0).asInstanceOf[Seq[String]]
    val values = features(1).asInstanceOf[Seq[Double]]
    val mapping = properties.zip(values)
    val histogram = props.foldLeft(Array.empty[Double]){
      (acc, property) =>
        val newValues = mapping.filter(x => x._1 == property).map(x => x._2)
        val newAggregate = aggFunc match {
          case "sum" => newValues.sum.toDouble
          case "count" => newValues.size.toDouble
          case "avg" => (newValues.sum / Math.max(newValues.size, 1)).toDouble
        }
        acc :+ newAggregate
    }

    Vectors.dense(histogram)
  }

  override def inputSchema: StructType =  {
    //here you define the input 
    val inputFields = Seq(
      StructField("input1" -> ListType(BasicType.String)),
      StructField("input2" -> ListType(BasicType.Double))
    )
    StructType(inputFields).get
  }

  override def outputSchema: StructType = StructType(StructField("output" -> TensorType.Double(outputSize))).get
}

我的建议是,该应用程序可能已经对您有用。我想如果你定义inputSchema如下,它可能会起作用:

override def inputSchema: StructType =  {
    //here you define the input 
    val inputFields = Seq.empty[StructField]
    StructType(inputFields).get
  }
于 2019-03-22T14:34:38.683 回答