Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions mllib/src/main/scala/org/apache/spark/ml/Estimator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
package org.apache.spark.ml

import scala.annotation.varargs
import scala.concurrent.{ExecutionContext, Future}
import scala.concurrent.duration.Duration

import org.apache.spark.annotation.{DeveloperApi, Since}
import org.apache.spark.ml.param.{ParamMap, ParamPair}
import org.apache.spark.sql.Dataset
import org.apache.spark.util.ThreadUtils

/**
* :: DeveloperApi ::
Expand Down Expand Up @@ -82,5 +85,32 @@ abstract class Estimator[M <: Model[M]] extends PipelineStage {
paramMaps.map(fit(dataset, _))
}

@Since("2.3.0")
def fit(dataset: Dataset[_], paramMaps: Array[ParamMap],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add doc for this interface later. But it can be reviewed first, currently this interface looks a little ugly.

unpersistDatasetAfterFitting: Boolean, executionContext: ExecutionContext,
modelCallback: (Model[_], ParamMap, Int) => Unit
): Unit = {
// Fit models in a Future for training in parallel
val modelFutures = paramMaps.map { paramMap =>
Future[Model[_]] {
fit(dataset, paramMap).asInstanceOf[Model[_]]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How will this work in a pipeline?

If the Estimator in CV is a Pipeline, then here it will call fit(dataset, paramMap) on the Pipeline which will in turn fit on each stage with that paramMap. This is what the current parallel CV is doing.

But if we have a stage with model-specific optimization (let's say for arguments sake a LinearRegression that can internally optimize maxIter) then its fit will be called with only a single paramMap arg.

So that pushing the parallel fit into Estimator nullifies any benefit from model-specific optimizations?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MLnick Oh, the design is still under discussion on JIRA and will be changed I think. I should mark this WIP. thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} (executionContext)
}

if (unpersistDatasetAfterFitting) {
// Unpersist training data only when all models have trained
Future.sequence[Model[_], Iterable](modelFutures)(implicitly, executionContext)
.onComplete { _ => dataset.unpersist() }(executionContext)
}

val modelCallbackFutures = modelFutures.zipWithIndex.map {
case (modelFuture, paramMapIndex) =>
modelFuture.map { model =>
modelCallback(model, paramMaps(paramMapIndex), paramMapIndex)
}(executionContext)
}
modelCallbackFutures.map(ThreadUtils.awaitResult(_, Duration.Inf))
}

override def copy(extra: ParamMap): Estimator[M]
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,30 +124,16 @@ class CrossValidator @Since("1.2.0") (@Since("1.4.0") override val uid: String)
val validationDataset = sparkSession.createDataFrame(validation, schema).cache()
logDebug(s"Train split $splitIndex with multiple sets of parameters.")

// Fit models in a Future for training in parallel
val modelFutures = epm.map { paramMap =>
Future[Model[_]] {
val model = est.fit(trainingDataset, paramMap)
model.asInstanceOf[Model[_]]
} (executionContext)
}

// Unpersist training data only when all models have trained
Future.sequence[Model[_], Iterable](modelFutures)(implicitly, executionContext)
.onComplete { _ => trainingDataset.unpersist() } (executionContext)

// Evaluate models in a Future that will calulate a metric and allow model to be cleaned up
val foldMetricFutures = modelFutures.zip(epm).map { case (modelFuture, paramMap) =>
modelFuture.map { model =>
val foldMetrics = new Array[Double](epm.length)
est.fit(trainingDataset, epm, true, executionContext,
(model: Model[_], paramMap: ParamMap, paramMapIndex: Int) => {
// TODO: duplicate evaluator to take extra params from input
val metric = eval.evaluate(model.transform(validationDataset, paramMap))
logDebug(s"Got metric $metric for model trained with $paramMap.")
metric
} (executionContext)
}
foldMetrics(paramMapIndex) = metric
}
)

// Wait for metrics to be calculated before unpersisting validation dataset
val foldMetrics = foldMetricFutures.map(ThreadUtils.awaitResult(_, Duration.Inf))
validationDataset.unpersist()
foldMetrics
}.transpose.map(_.sum / $(numFolds)) // Calculate average metric over all splits
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,29 +123,16 @@ class TrainValidationSplit @Since("1.5.0") (@Since("1.5.0") override val uid: St

// Fit models in a Future for training in parallel
logDebug(s"Train split with multiple sets of parameters.")
val modelFutures = epm.map { paramMap =>
Future[Model[_]] {
val model = est.fit(trainingDataset, paramMap)
model.asInstanceOf[Model[_]]
} (executionContext)
}

// Unpersist training data only when all models have trained
Future.sequence[Model[_], Iterable](modelFutures)(implicitly, executionContext)
.onComplete { _ => trainingDataset.unpersist() } (executionContext)

// Evaluate models in a Future that will calulate a metric and allow model to be cleaned up
val metricFutures = modelFutures.zip(epm).map { case (modelFuture, paramMap) =>
modelFuture.map { model =>
val metrics = new Array[Double](epm.length)
est.fit(trainingDataset, epm, true, executionContext,
(model: Model[_], paramMap: ParamMap, paramMapIndex: Int) => {
// TODO: duplicate evaluator to take extra params from input
val metric = eval.evaluate(model.transform(validationDataset, paramMap))
logDebug(s"Got metric $metric for model trained with $paramMap.")
metric
} (executionContext)
}

// Wait for all metrics to be calculated
val metrics = metricFutures.map(ThreadUtils.awaitResult(_, Duration.Inf))
metrics(paramMapIndex) = metric
}
)

// Unpersist validation set once all metrics have been produced
validationDataset.unpersist()
Expand Down