spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From MLnick <...@git.apache.org>
Subject [GitHub] spark pull request #16774: [SPARK-19357][ML] Adding parallel model evaluatio...
Date Mon, 10 Apr 2017 09:34:28 GMT
Github user MLnick commented on a diff in the pull request:

    https://github.com/apache/spark/pull/16774#discussion_r110610484
  
    --- Diff: mllib/src/main/scala/org/apache/spark/ml/tuning/CrossValidator.scala ---
    @@ -100,31 +108,60 @@ class CrossValidator @Since("1.2.0") (@Since("1.4.0") override val
uid: String)
         val eval = $(evaluator)
         val epm = $(estimatorParamMaps)
         val numModels = epm.length
    -    val metrics = new Array[Double](epm.length)
    +
    +    // Create execution context, run in serial if numParallelEval is 1
    +    val executionContext = $(numParallelEval) match {
    +      case 1 =>
    +        ThreadUtils.sameThread
    +      case n =>
    +        ExecutionContext.fromExecutorService(executorServiceFactory(n))
    +    }
     
         val instr = Instrumentation.create(this, dataset)
         instr.logParams(numFolds, seed)
         logTuningParams(instr)
     
    +    // Compute metrics for each model over each split
    +    logDebug(s"Running cross-validation with level of parallelism: $numParallelEval.")
         val splits = MLUtils.kFold(dataset.toDF.rdd, $(numFolds), $(seed))
    -    splits.zipWithIndex.foreach { case ((training, validation), splitIndex) =>
    +    val metrics = splits.zipWithIndex.map { case ((training, validation), splitIndex)
=>
           val trainingDataset = sparkSession.createDataFrame(training, schema).cache()
           val validationDataset = sparkSession.createDataFrame(validation, schema).cache()
    -      // multi-model training
           logDebug(s"Train split $splitIndex with multiple sets of parameters.")
    -      val models = est.fit(trainingDataset, epm).asInstanceOf[Seq[Model[_]]]
    -      trainingDataset.unpersist()
    -      var i = 0
    -      while (i < numModels) {
    -        // TODO: duplicate evaluator to take extra params from input
    -        val metric = eval.evaluate(models(i).transform(validationDataset, epm(i)))
    -        logDebug(s"Got metric $metric for model trained with ${epm(i)}.")
    -        metrics(i) += metric
    -        i += 1
    +
    +      // Fit models in a Future with thread-pool size determined by '$numParallelEval'
    +      val models = epm.map { paramMap =>
    +        Future[Model[_]] {
    +          val model = est.fit(trainingDataset, paramMap)
    +          model.asInstanceOf[Model[_]]
    +        } (executionContext)
           }
    +
    +      Future.sequence[Model[_], Iterable](models)(implicitly, executionContext).onComplete
{ _ =>
    +        trainingDataset.unpersist()
    +      } (executionContext)
    +
    +      // Evaluate models in a Future with thread-pool size determined by '$numParallelEval'
    +      val foldMetricFutures = models.zip(epm).map { case (modelFuture, paramMap) =>
    +        modelFuture.flatMap { model =>
    --- End diff --
    
    *Note* we could use `for` comprehension here. But I tried it and it doesn't really make
it all that much simpler, and the explicit `flatMap` and `Future` here makes it a bit clearer.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message