Skip to content

Commit a2b06a4

Browse files
authored
Merge pull request #328 from dhyeyinf/main
Add Spark example using ask-and-tell interface
2 parents ea3271e + e9f5162 commit a2b06a4

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

spark/ask_and_tell_spark.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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("\nBest trial:")
31+
print(f"\tValue: {study.best_value}")
32+
print(f"\tParams: {study.best_trial.params}")
33+
spark.stop()

spark/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
optuna
2+
pyspark # used for SparkSession and RDD parallelism

0 commit comments

Comments
 (0)