Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove hyperparmeter tuning code #454

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ import org.apache.spark.sql.{DataFrame, SparkSession}
import org.apache.spark.storage.StorageLevel

import com.linkedin.photon.ml._
import com.linkedin.photon.ml.HyperparameterTunerName.HyperparameterTunerName
import com.linkedin.photon.ml.HyperparameterTuningMode.HyperparameterTuningMode
import com.linkedin.photon.ml.hyperparameter.HyperparameterTuningMode.HyperparameterTuningMode
import com.linkedin.photon.ml.TaskType.TaskType
import com.linkedin.photon.ml.Types._
import com.linkedin.photon.ml.cli.game.GameDriver
import com.linkedin.photon.ml.data.{DataValidators, FixedEffectDataConfiguration, InputColumnsNames, RandomEffectDataConfiguration}
import com.linkedin.photon.ml.data.avro.{AvroDataReader, ModelProcessingUtils}
import com.linkedin.photon.ml.estimators.GameEstimator.GameOptimizationConfiguration
import com.linkedin.photon.ml.estimators.{GameEstimator, GameEstimatorEvaluationFunction}
import com.linkedin.photon.ml.estimators.GameEstimator
import com.linkedin.photon.ml.hyperparameter.HyperparameterTuningMode
import com.linkedin.photon.ml.hyperparameter.evaluation.GameEstimatorEvaluationFunction
import com.linkedin.photon.ml.hyperparameter.tuner.HyperparameterTunerFactory
import com.linkedin.photon.ml.index.{IndexMap, IndexMapLoader}
import com.linkedin.photon.ml.io.{CoordinateConfiguration, ModelOutputMode, RandomEffectCoordinateConfiguration}
Expand Down Expand Up @@ -145,7 +146,7 @@ object GameTrainingDriver extends GameDriver {
"Suggested depth for tree aggregation.",
ParamValidators.gt[Int](0.0))

val hyperParameterTunerName: Param[HyperparameterTunerName] = ParamUtils.createParam[HyperparameterTunerName](
val hyperParameterTunerName: Param[String] = ParamUtils.createParam[String](
"hyper parameter tuner",
"Package name of hyperparameter tuner."
)
Expand Down Expand Up @@ -217,7 +218,6 @@ object GameTrainingDriver extends GameDriver {
setDefault(outputMode, ModelOutputMode.BEST)
setDefault(overrideOutputDirectory, false)
setDefault(normalization, NormalizationType.NONE)
setDefault(hyperParameterTunerName, HyperparameterTunerName.DUMMY)
setDefault(hyperParameterTuning, HyperparameterTuningMode.NONE)
setDefault(varianceComputationType, VarianceComputationType.NONE)
setDefault(dataValidation, DataValidationType.VALIDATE_DISABLED)
Expand Down Expand Up @@ -329,13 +329,25 @@ object GameTrainingDriver extends GameDriver {
case _ =>
}

// If hyperparameter tuning is enabled, need to specify the number of tuning iterations
// If hyperparameter tuning is enabled...
hyperParameterTuningMode match {
case HyperparameterTuningMode.BAYESIAN | HyperparameterTuningMode.RANDOM =>
require(

// ... need to specify the hyperparameter tuner
require(
paramMap.get(hyperParameterTunerName).isDefined,
"Hyperparameter tuning enabled, but tuner not specified.")

// ... need to specify the number of tuning iterations
require(
paramMap.get(hyperParameterTuningIter).isDefined,
"Hyperparameter tuning enabled, but number of iterations unspecified.")

// ... validation data must be provided
require(
paramMap.get(validationDataDirectories).isDefined,
"Hyperparameter tuning enabled, but no validation data provided")

case _ =>
}

Expand Down Expand Up @@ -493,10 +505,16 @@ object GameTrainingDriver extends GameDriver {
gameEstimator.fit(trainingData, validationData, gameOptimizationConfigs)
}

val tunedModels = Timed("Tune hyperparameters") {
// Disable warm start for autotuning
gameEstimator.setUseWarmStart(false)
runHyperparameterTuning(gameEstimator, trainingData, validationData, explicitModels)
val tunedModels = getOrDefault(hyperParameterTuning) match {
case HyperparameterTuningMode.NONE =>
Seq()

case _ =>
Timed("Tune hyperparameters") {
// Disable warm start for autotuning
gameEstimator.setUseWarmStart(false)
runHyperparameterTuning(gameEstimator, trainingData, validationData, explicitModels)
}
}

trainingData.unpersist()
Expand Down Expand Up @@ -678,45 +696,42 @@ object GameTrainingDriver extends GameDriver {
estimator: GameEstimator,
trainingData: DataFrame,
validationData: Option[DataFrame],
models: Seq[GameEstimator.GameResult]): Seq[GameEstimator.GameResult] =

validationData match {
case Some(testData) if getOrDefault(hyperParameterTuning) != HyperparameterTuningMode.NONE =>

val (_, baseConfig, evaluationResults) = models.head

val iteration = getOrDefault(hyperParameterTuningIter)

val dimension = baseConfig.toSeq.map {
case (_, config: GLMOptimizationConfiguration) =>
config.regularizationContext.regularizationType match {
case RegularizationType.ELASTIC_NET => 2
case RegularizationType.L2 => 1
case RegularizationType.L1 => 1
case RegularizationType.NONE => 0
}
case _ => throw new IllegalArgumentException(s"Unknown optimization config!")
}.sum

val mode = getOrDefault(hyperParameterTuning)

val evaluator = evaluationResults.get.primaryEvaluator
val isOptMax = evaluator.betterThan(1.0, 0.0)
val evaluationFunction = new GameEstimatorEvaluationFunction(
estimator,
baseConfig,
trainingData,
testData,
isOptMax)
models: Seq[GameEstimator.GameResult]): Seq[GameEstimator.GameResult] = {

val (_, baseConfig, evaluationResults) = models.head

val iteration = getOrDefault(hyperParameterTuningIter)
val dimension = baseConfig
.toSeq
.map {
case (_, config: GLMOptimizationConfiguration) =>
config.regularizationContext.regularizationType match {
case RegularizationType.ELASTIC_NET => 2
case RegularizationType.L2 => 1
case RegularizationType.L1 => 1
case RegularizationType.NONE => 0
}
case _ =>
throw new IllegalArgumentException(s"Unknown optimization config!")
}
.sum
val mode = getOrDefault(hyperParameterTuning)

val observations = evaluationFunction.convertObservations(models)
val evaluator = evaluationResults.get.primaryEvaluator
val isOptMax = evaluator.betterThan(1.0, 0.0)
val evaluationFunction = new GameEstimatorEvaluationFunction(
estimator,
baseConfig,
trainingData,
validationData.get,
isOptMax)

val hyperparameterTuner = HyperparameterTunerFactory[GameEstimator.GameResult](getOrDefault(hyperParameterTunerName))
val observations = evaluationFunction.convertObservations(models)

hyperparameterTuner.search(iteration, dimension, mode, evaluationFunction, observations)
val hyperparameterTuner = HyperparameterTunerFactory(getOrDefault(hyperParameterTunerName))

case _ => Seq()
}
hyperparameterTuner.search(iteration, dimension, mode, evaluationFunction, observations)
}

/**
* Select which models will be output to HDFS.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 LinkedIn Corp. All rights reserved.
* Copyright 2020 LinkedIn Corp. All rights reserved.
* Licensed 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
Expand All @@ -12,12 +12,14 @@
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.linkedin.photon.ml
package com.linkedin.photon.ml.hyperparameter

/**
* Supported options for hyperparameter tuning mode
*/
object HyperparameterTuningMode extends Enumeration {

type HyperparameterTuningMode = Value

val BAYESIAN, RANDOM, NONE = Value
}
Loading