File tree Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Expand file tree Collapse file tree 2 files changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ Spark with Ask-and-Tell
3+
4+ An example showing how to use Optuna's ask-and-tell interface with Apache Spark
5+ to distribute the evaluation of trials.
6+
7+ This script uses PySpark's RDD to parallelize a simple quadratic function.
8+ """
9+
10+ import optuna
11+ from pyspark .sql import SparkSession
12+
13+
14+ def evaluate (x ):
15+ return (x - 2 ) ** 2 # Simulate a trial evaluation on a Spark worker node
16+
17+
18+ if __name__ == "__main__" :
19+ spark = SparkSession .builder .appName ("OptunaSparkExample" ).getOrCreate ()
20+ study = optuna .create_study ()
21+
22+ for i in range (20 ):
23+ trial = study .ask ()
24+ x = trial .suggest_float ("x" , - 10 , 10 )
25+ rdd = spark .sparkContext .parallelize ([x ])
26+ result = rdd .map (evaluate ).collect ()[0 ]
27+ study .tell (trial , result )
28+ print (f"Trial#{ trial .number } : { x = :.4e} , { result = :.4e} " )
29+
30+ print ("\n Best trial:" )
31+ print (f"\t Value: { study .best_value } " )
32+ print (f"\t Params: { study .best_trial .params } " )
33+ spark .stop ()
Original file line number Diff line number Diff line change 1+ optuna
2+ pyspark # used for SparkSession and RDD parallelism
You can’t perform that action at this time.
0 commit comments